How to Install Jupyter Notebook with Nginx Proxy on Debian 12

Jupyter is a free and open-source web application for interactive computing and data science. Jupyter supports all programming languages and provides multiple software, such as JupyetrLab, which provides a feature-rich and tabbed multi-notebook editing environment, Notebook as a lightweight and simplified notebook authoring, Qtconsole, and many more.

In this tutorial, you'll learn how to install Jupyter and configure Jupyter Notebook on the Debian 12 server. You'll also secure Jupyter Notebook with a password and run Jupyter Notebook as a systemd service. Lastly, you'll install and configure Nginx as a reverse proxy for Jupyter Notebook installation.

Prerequisites

Before you begin, make sure you have the following:

  • A Debian 12 server
  • A non-root user with administrator privileges
  • A domain name pointed to server IP address

Installing Python Packages

Before installing Jupyter, you must install Python, Pip Python package manager, venv for creating a Python virtual environment, and git. In this section, you'll install those packages with the APT package manager.

To start, run the command below to update your Debian package index.

sudo apt update

update repo

Now install Python and dependencies such as Pip, venv, and Python Dev package. Enter 'Y' to confirm the installation.

sudo apt install python3 python3-pip python3-dev python3-venv git

install packages

After the installation is finished, run the 'pip3' command below to upgrade your Pip version.

pip3 install --break-system-package --upgrade pip

Now check the Python and Pip version with the command below.

python3 --version
pip3 --version

You can see below that Python 3.11 and Pip 24.3 are installed.

check pip and python

Setting up Python Virtual Environment

After you've installed Python and other dependencies, you'll create a new Python virtual environment for the Jupyter installation. With this, your Jupyter installation will be isolated in the virtual environment. Also, you need a dedicated Linux user, so ensure you've your user ready.

Log in to your user with the command below.

su - username

Run the 'python3' command below to create a new Python Virtual Environment 'venv'. This will install Python and Pip in your virtual environment, which is separate from your system.

python3 -m venv venv

Activate the 'venv' Python virtual environment. Once activated, your shell prompt will become such as '(venv) username@host ...'.

source venv/bin/activate

create venv

If you want to deactivate 'venv', execute the 'deactivate' command below.

deactivate

Installing Jupyter

Now that you've created and activated the Python virtual environment, you can start the Jupyter installation through the Pip Python package manager.

To install Jupyter, run the 'pip3' command below.

pip3 install jupyter

You can see below the installation of Jupyter in a virtual environment.

install juopyter

Once the installation is complete, check the Jupyter version with the following command.

jupyter --version

In the following output, you can see the version of each Jupyter component that is installed.

check version

Enable Authentication in Jupyter Notebook

After Jupyter you've installed Jupyter, you'll configure the Jupyter Notebook installation by enabling password authentication.

First, run the command below to generate the configuration for Jupyter Notebook. This will generate a new configuration to '~/.jupyter/jupyter_notebook_config.py'.

jupyter notebook --geenrate-config

Now set up the password for Jupyter Notebook using the command below. Enter your password when prompted and repeat.

jupyter notebook password

Lastly, execute the 'deactivate' command to log out from the 'venv' virtual environment.

deactivate

Running Jupyter Notebook as a Systemd Service

In this section, you'll create a new systemd service that will run Jupyter Notebook. With this, the Jupyter Notebook will be running in the background as a systemd service, and you can easily manage it with the 'systemctl' utility.

Create a new systemd service file '/etc/systemd/system/jupyter.service' with the following 'nano' editor.

sudo nano /etc/systemd/system/jupyter.service

Insert the configuration below to run Jupyter Notebook as a systemd service.

[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/alice/venv/bin/jupyter-notebook --config=/home/alice/.jupyter/jupyter_notebook_config.py --allow-root
User=root
Group=root
WorkingDirectory=/home/alice/venv
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Save the file and exit the editor.

Now run the 'systemctl' command below to restart the systemd manager and apply your changes.

sudo systemctl daemon-reload

jupyter systemd

Lastly, start and enable the 'jupyter' service with the command below.

sudo systemctl enable --now jupyter
sudo systemctl status jupyter

In the following output, you can see that the 'jupyter' service is running on your system.

jupyter status

Allowing remote access to Jupyter Notebook

In this section, you'll enable remote access to Jupyter. This must be done if you want to set up a reverse proxy in front of your Jupyter installation.

Log in to your user and open the Jupyter configuration '~/.jupyter/jupyter_notebook_config.py' with the following 'nano' editor.

su - username
nano ~/.jupyter/jupyter_notebook_config.py

To enable remote access, uncomment the 'c.ServerApp.allow_remote_access' option and change the value to 'True'.

c.ServerApp.allow_remote_access = True

Save and exit the file when finished.

Next, run the 'systemctl' command below to restart the 'jupyter' service and apply your changes. With this, the new token will be generated and can be found in the log file.

sudo systemctl restart jupyter

Lastly, check the 'jupyter' service status with the following command.

sudo systemctl status jupyter

Take a look at the bottom of the message, and copy the generated token for Jupyter Notebook.

token

Setting Nginx as a reverse proxy

Now that Jupyter Notebook is running as a service, the next step is to install Nginx and configure it as a reverse proxy for Jupyter Notebook. This way, you can easily secure Jupyter Notebook with HTTPS.

Install the 'nginx' package with the 'apt' command below.

sudo apt install nginx -y

install nginx

After the installation is complete, create a new Nginx server block configuration '/etc/nginx/sites-available/jupyter' with the following 'nano' editor.

sudo nano /etc/nginx/sites-available/jupyter

Insert the configuration below to set up Nginx as a reverse proxy for the Jupyter Notebook. Make sure to change the 'server_name' parameter with your domain name.

server {
listen 80;
server_name lab.howtoforge.local;

access_log /var/log/nginx/howtoforge.local.access.log;
error_log /var/log/nginx/howtoforge.local.error.log;

location / {
proxy_pass http://127.0.0.1:8888;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
}

Save the file and exit the editor.

Next, run the command below to activate the 'jupyter' server block and verify your Nginx configuration.

sudo ln -s /etc/nginx/sites-available/jupyter /etc/nginx/sites-enabled/
sudo nginx -t

If you've proper Nginx settings, you'll see an output such as 'syntax is ok - test is successful'.

setup nginx

Lastly, run the 'systemctl' command below to restart the Nginx web server and check your Nginx status.

sudo systemctl restart nginx
sudo systemctl status nginx

If Nginx is running, you can see the output like the following:

check nginx

Accessing Jupyter Notebook

Open your web browser and visit the domain name of your Jupyter Notebook installation such as http://lab.howtoforge.local/. If your installation is successful, you'll be prompted with the Jupyter password authentication.

Enter your password and click 'Log In'.

login

Now you'll see the Jupyter Notebook dashboard like the following:

dashboard

notebook

Conclusion

Congratulations! You've completed the installation of Jupyter Notebook on the Debian 12 server. You've installed Jupyter Notebook and secure with authentication. You've also configured Nginx as a reverse proxy for Jupyter Notebook. For the next step, you want to add SSL certificates to secure Jupyter Notebook and install some extensions to extend Jupyter Notebook functionality.

Share this page:

0 Comment(s)