Jiageng

Jiageng

github
x
email
bilibili

訪問伺服器 Jupyter Lab 或 Notebook 並使用 Nginx/Caddy 反向代理

本文以 Debian/Ubuntu 為例

Jupyter Notebook#

  1. 安裝

pip install notebook

  1. 配置

生成預設配置 jupyter notebook --generate-config

打開 vim .jupyter/jupyter_notebook_config.py 配置文件

修改並取消註釋(可以用 vim 搜索關鍵詞)

c.NotebookApp.allow_root = True
c.NotebookApp.base_url = '/notebook'
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.notebook_dir = 'JupyterLab'
c.NotebookApp.open_browser = False
c.NotebookApp.password = '<上面保存的sha1值>'
c.NotebookApp.port = 8888
  1. 啟動

python3 -m notebook

  1. 自啟動

vim /etc/systemd/system/jupyter-notebook.service

寫入如下代碼

[Unit]
Description=Jupyter Notebook
After=network.target
[Service]
Type=simple
ExecStart=/home/aaa/.local/bin/jupyter-notebook --config=/home/aaa/.jupyter/jupyter_notebook_config.py
User=aaa
WorkingDirectory=/home/aaa/
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

將 aaa 改為自己的用戶名,路徑等自行修改

啟動 Jupyter Notebook

systemctl enable jupyter-notebook
systemctl start jupyter-notebook
  1. 反向代理參考 https 訪問章節

Jupyter Lab#

安裝 JupyterLab#

  1. 安裝 pip 與 JupyterLab
sudo apt update
sudo apt install python3
sudo apt install pip3
pip3 install jupyterlab
  1. 添加 PATH

export PATH=$PATH:/path/to/jupyterlab

echo $PATH

安裝較新版本 nodejs#

  1. 添加 apt 源文件並更新源:

curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

  1. 安裝 nodejs 與 npm

sudo apt install nodejs

  1. 查看 nodejs 版本

npm --version

配置 JupyterLab#

  1. 生成配置文件

jupyter lab --generate-config

  1. 創建哈希密碼

利用ipython創建密碼

from jupyter_server.auth import passwd
passwd(algorithm='sha1')
# 輸入兩次密碼並保存sha1值
  1. 修改 JupyterLab 配置文件

mkdir ~/JupyterLab

打開配置文件

vim ~/.jupyter/jupyter_lab_config.py

修改並取消註釋(可以用 vim 搜索關鍵詞)

c.ServerApp.allow_root = True
c.ServerApp.base_url = '/jupyter'
c.ServerApp.ip = '0.0.0.0'
c.ServerApp.notebook_dir = 'JupyterLab'
c.ServerApp.open_browser = False
c.ServerApp.password = '<上面保存的sha1值>'
c.ServerApp.port = 8080

:wq 保存並退出

構建 JupyterLab 並啟動#

jupyter lab build --dev-build=False --minimize=False

nohup jupyter lab &

到現在為止已經可以用 http:// 公網 ip:8080 訪問 JupyterLab,後文介紹使用 https 訪問以及開機自啟動

https 訪問#

Nginx#

將下列代碼添加到 nginx 配置文件 (/etc/nginx/nginx.conf)

client_max_body_size 1G;
location /jupyter {
        proxy_pass   http://127.0.0.1:8080;
        proxy_connect_timeout 3s;
        proxy_read_timeout 5s;
        proxy_send_timeout 3s;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
    };
location /notebook {
        proxy_pass   http://127.0.0.1:8888;
        proxy_connect_timeout 3s;
        proxy_read_timeout 5s;
        proxy_send_timeout 3s;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
    };

Caddy#

將下列代碼添加到 Caddyfile

your_domain_name

reverse_proxy /jupyter/* 127.0.0.1:8080
reverse_proxy /notebook/* 127.0.0.1:8888

重新讀取 Caddyfile 並啟動 Caddy

caddy reload && caddy start

開機自啟動#

使用 systemctl 管理

vim /etc/systemd/system/jupyter.service

寫入如下代碼

[Unit]
Description=Jupyter Lab
After=network.target
[Service]
Type=simple
ExecStart=/home/aaa/.local/bin/jupyter-lab --config=/home/aaa/.jupyter/jupyter_lab_config.py
User=aaa
WorkingDirectory=/home/aaa/
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

將 aaa 改為自己的用戶名,路徑等自行修改

啟動 JupyterLab

systemctl enable jupyter
systemctl start jupyter

參考資料和推薦閱讀#

添加 swap 空間

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。