Jupyter Web App

We are going to install jupyter lab on ubuntu and expose it to the web using NGINX reverse proxy. Jupyter lab web access helps in running your codes on server. Premade codeblocks can be used to generate desired output, thus automating sundane tasks. The interface allso provides access to code files, output files. External files can also be uploaded for processing and storage.

Create Virtual Python Environment

First we creaete a pythion virtual environment in Ubuntu. For the same you need to have python installed. if not use blow command to install python

sudo apt install python

Now lets create a directory Documents in the userdirectory (home/user) using - mkdir Documents. Change to the directory cd Documents and run below command to create a virtual environment.

python -m venv /path/to/new/virtual/environment/venv

The virtual environment will be created in folder /Documents/venv To activatet the virtual environment

source /venv/bin/activate

the command prompt will change with a (venv) in the beginning. To deactivatet the virtual environment

deactivate

Installing Jupyter Lab

Now we install Jupyter Lab using pip

pip install jupyterlab

To launch juypter lab use below command

jupyter lab

This will launch the jupyter lab on the local host with port 8888 This instance is not secure as it can be accessed without a password on the browser. To add a password for login use below command

jupyter server password

Enter the new password when asked. Jupyter will create a jupyter_server_config.json file in user/username/.jupyter folder and srtore the password in hashed format.

Setting up NGINX reverse proxy

To expose the sever to a custom domain we need to add NGINX reverse proxy. More details are available at -

The server config file will look like this - replace example.com with your own domain name

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {

    server_name example.com;

    location / {
        # or whichever port you've set for your Jupyter
        proxy_pass http://localhost:8888;
        # $http_host is important for accessing Jupyter locally
        proxy_set_header Host $http_host;
        # http://nginx.org/en/docs/http/websocket.html
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;
    server_name example.com;
    return 404; # managed by Certbot
}

Ensure that your DNS provider has the A record for domain example.com, pointing at the public ip of server hosting Jupyter Server. For newer version of Jupyter Lab we need one more step to allow request coming from public adress of example.com We need to launch the Jupyter Lab instance with the option --ServerApp.allow_remote_access=true

jupyter lab --ServerApp.allow_remote_access=true

As we are not going to use a local browser for accessing the server we can launch Jupyter Lab using below command. Note the disown argument helps to release the command prompt once the server is launched.

jupyter lab --ServerApp.allow_remote_access=true --no-browser & disown

Creating Launch Script

To avoid long step of activating the python virtual environment and then lauching the jupyter lab using the above command, we create a bash script.

sudo nano jupyter_launch_script.sh

Copy the below content into the script file. Replace your username and file path where necessary

#!/bin/bash
cd /home/username/Documents/venv/Projects
source /home/username/Documents/venv/bin/activate
jupyter lab --ServerApp.allow_remote_access=true --no-browser & disown
echo  "run (pgrep jupyter) to find the PID of the process and then kill 1234, replacing 1234 with the PID you just found."

To launch script, navigate to the script directory and run

./jupyter_launch_script.sh

To find and kill a running instance

pgrep jupyter

This will show the process id of Jupyter Lab

kill processid

Last updated