在服务器上安装jupyter lab

68C1EC55-981E-4318-A76C-0B0623AA151A

缘由

自己电脑扔学校了,自己就租了一个好一点的服务器来用配置了一个jupyterlab,用python来练习练习手感,也尝试玩一些好玩的东西 !感觉这种模式也挺好的,只要有一个可以上网的东西就可以运行一些简单的东西,感觉nice!之前尝试过腾讯的cloud studio,感觉也不错,但把自己的东西放到人家那里总感觉不爽!

安装

安装jupyterlab

pip3 install jupyterlab

配置jupyterlab登陆密码

  • 新建一个py文件运行(内容如下,如名字为a.py)

    from notebook.auth import passwd 
    passwd()  
  • 运行后按提示输入密码后会得到一个哈希密码;

    python3 a.py
  • 生成配置文件,会给出配置文件地址;

    jupyter notebook --generate-config  
  • 打开配置文件,把下面的设置如下;第一个如设置了后面的启动则可以不加–allow-root

    c.NotebookApp.allow_root = True
    c.NotebookApp.open_browser = False
    c.NotebookApp.password = '刚才复制的输出粘贴到这里来' 

配置nginx

可通过网址登陆,不配置则通过IP运行,为保障服务器配置及安全等等,只提供关键点,其它自己按实际需求更改!

    location / {
        proxy_pass http://127.0.0.1:999;
        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;
    }

运行

以下配置也可在生成的配置文件中改,但我自己按网上配置更改后无法通过网站访问,就放弃更改文件方式了!现在想想也可能是nignx没配置好,但自己就不想折腾了!

附加项 功能
ip 本地
port 运行端口(和nginx的配置端口一样)
allow-root 可root运行
nohup jupyter lab --ip=127.0.0.1 --port=999 --allow-root >> jupyter.log  2>&1 &

守候进程

自己拿来练习shell脚本的,感觉还不错,自己可以去试试!

#!/bin/sh
#进程名字可修
PRO_NAME=jupyter

#打开特定工作目录
#cd /home/lab/

while true ; do
    T=$(date)
    #用ps获取$PRO_NAME进程数量
    Process=ps -ef | grep "${PRO_NAME}" | grep -v grep | grep -v 'jupyter.sh' | grep -v '[${PRO_NAME}]'
#     echo "$T : $Process"
    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}"

    #少于1,重启进程
    if [ "${NUM}" -lt "1" ];then
        echo "$T : $Process"
        echo "$T : ${PRO_NAME} was killed"
        cd /home/lab
        nohup jupyter lab --ip=127.0.0.1 --port=999 --allow-root >> jupyter.log  2>&1 &
        echo "$T : ${PRO_NAME} restart ! "
        sleep 10s
    #大于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
# 设置较长时间来给jupyter lab足够的启动时间,以防止重复启动
    sleep 30s
done

exit 0

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *