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')
# パスワードを2回入力し、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

参考資料と推奨読書#

スワップスペースの追加

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。