How to set up a jupyterlab in vps

The Post Created(Updated) On 04/26/2020,Please note the timeliness of the article!

cause

I’m very sad that i left my laptop at school,but I can’t have fun all the time,So I rent a vps which have high performance than I have and set up a jupyterlab on it,It’s very nice that I can play some funny things on it.I have tried to use a Cloud Studio on Tencent,it will let us thought is my data safe enough?

install

install jupyterlab

pip3 install jupyterlab

setup login password

  • create a python document named a.py,write as follows:

    from notebook.auth import passwd
    passwd()
  • you will get a hash password when you input your password as the a.py runs

    python3 a.py
  • generate a config,your will get the config address when it finished

    jupyter notebook --generate-config
  • change the value in config as follows,you can ingore the run script –allow-root If you set allow_root up is true there;

    c.NotebookApp.allow_root = True
    c.NotebookApp.open_browser = False
    c.NotebookApp.password = 'your hash password you generate!'

config nginx

You can ignore this steps if you wanna use ip to access your service,I just show the important things here,for my vps security!

    location / {
        proxy_pass http://127.0.0.1:999; # you can change your port if it was used by another service !
        proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IPremote_addr;
        proxy_set_header Host http_host;
        proxy_http_version 1.1;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header Upgradehttp_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400;
    }

run

You can change some value if you don’t wanna use in your run shell.

value function
ip run in localhost
port same with nginx config
allow-root run with root authority
nohup jupyter lab --ip=127.0.0.1 --port=999 --allow-root >> jupyter.log  2>&1 &

protect process

It’s very nice if you need to ensure jupyterlab is always run in vps

#!/bin/sh
#Process Name
PRO_NAME=jupyter

while true ; do
    T=(date)
    #get process informatinon if some wrong happens!you can put it into a log for your check!
    Process=`ps -ef | grep "{PRO_NAME}" | grep -v grep | grep -v 'jupyter.sh' | grep -v '[{PRO_NAME}]'`
#     echo "T : Process"
#get the running process number    NUM=`ps -ef | grep  "{PRO_NAME}" | grep -v grep | grep -v 'jupyter.sh' | grep -v '[{PRO_NAME}]' | wc -l `
#    echo "T : It has started !"
#      echo "{NUM}"
    #It will start if it small than 1
    if [ "{NUM}" -lt "1" ];then
        echo "T :Process"
        echo "T :{PRO_NAME} was killed"
        # jupyterlab running DIR
        cd /home/lab
        # you should change for your condition
        nohup jupyter lab --ip=127.0.0.1 --port=999 --allow-root >> jupyter.log  2>&1 &
        echo "T :{PRO_NAME} restart ! "
        sleep 10s
    #it will kill all same process and restart if it lager than 1
    elif [ "{NUM}" -gt "1" ];then
        echo "T : Process"
        echo "T : more than 1 {PRO_NAME},killall{PRO_NAME}"
        ps -ef | grep PRO_NAME | grep -v grep | grep -v 'jupyter.sh' | cut -c 9-15 | xargs kill -9
        cd /home/lab
        nohup jupyter lab --ip=127.0.0.1 --port=999 --allow-root >> jupyter.log  2>&1 &
        echo "T : killall and restart!"
        sleep 10s
    fi
# it will take some time to run jupyterlab,so we shoud deny time to get right number
    sleep 30s
done
exit 0

Copyright

Unless otherwise noted, all work on this blog is licensed under a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) License. Reprinted with permission from -
https://blog.emperinter.info/2020/04/26/how-to-set-up-a-jupyterlab-in-vps


CouPon

alibaba cloud20 dollars
Vultr10 Dollars
Bandwagon HostMaybe some Coupon?
Domain | namesiloemperinter 1Dollars