
之前是使用docker安装的JupyterLab来进行学习,但是有时候需要安装新的包,就不太方便。而且原来是部署在内网的服务器上,连接外网还被限制。所以无奈之下,我只能把它转移到云服务器上,并且进行直接的安装。由于这个云服务器没有域名备案,所以我只能使用公网IP。
bashcd projects/jupyter/
python3 -m venv .venv
source .venv/bin/activate
pip install jupyterlab
默认生成的配置文件在用户的家目录下~/.jupyter/jupyter_lab_config.py。
设置完密码会存在~/.jupyter/jupyter_server_config.json。将里面的argon2的哈希拷贝出来,下面会用到。
bashjupyter lab --generate-config jupyter lab password
将下面的配置拷贝到jupyter_lab_config.py的最后面,最后一行的密码哈希就是上面拷贝出来的argon2的哈希。
bashc.ServerApp.ip = '0.0.0.0'
c.ServerApp.port = 8888
c.ServerApp.open_browser = False
c.ServerApp.base_url = '/jupyter/'
c.ServerApp.root_dir = '/home/ubuntu/projects/jupyter/notebooks'
c.ServerApp.allow_origin = '*'
c.ServerApp.allow_remote_access = True
c.ServerApp.trust_xheaders = True
# Password authentication
c.PasswordIdentityProvider.hashed_password = 'argon2:$argon2id$v=19$m=10240,t=10,p=HASH'
配置文件在nginx的conf.d文件夹下,具体取决于你容器映射到的本地文件夹在哪。
我的Nginx是使用Docker跑起来的,给其它容器做反代用。但是这里是反代到宿主机上,所以proxy_pass使用的是docker0的地址。
这个是JupyterLab的Nginx配置文件,使用的是子路径。
nginxserver { listen 443 ssl; server_name _; ssl_certificate /etc/nginx/certs/nginx-selfsigned.crt; ssl_certificate_key /etc/nginx/certs/nginx-selfsigned.key; client_max_body_size 100M; location /jupyter/ { proxy_pass http://172.17.0.1:8888; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; # Prevent double compression/mismatch proxy_set_header Accept-Encoding ""; # Disable buffering to avoid truncation issues proxy_buffering off; proxy_request_buffering off; proxy_cache off; # Timeouts for large files and long kernels proxy_connect_timeout 60s; proxy_send_timeout 86400; proxy_read_timeout 86400; # Optional: keep upstream responses in memory proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_max_temp_file_size 0; } }
原来还有一个自动Http到Https的配置文件。
nginxserver { listen 80; server_name _; return 301 https://$host$request_uri; }
最后在重启Nginx使新配置生效。
新建/etc/systemd/system/jupyterlab.service:
bash[Unit]
Description=JupyterLab Server
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/projects/jupyter/notebooks
# Use full path to jupyter inside your venv
ExecStart=/home/ubuntu/projects/jupyter/.venv/bin/jupyter lab --config=/home/ubuntu/.jupyter/jupyter_lab_config.py
# Environment variables
Environment="PATH=/home/ubuntu/projects/jupyter/.venv/bin:/usr/local/bin:/usr/bin:/bin"
Environment="VIRTUAL_ENV=/home/ubuntu/projects/jupyter/.venv"
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
重启Systemd加载新的配置。
bashsudo systemctl daemon-reload
sudo systemctl enable jupyterlab
sudo systemctl start jupyterlab
sudo systemctl status jupyterlab
应该可以打开https://IP/jupyter/,并要求输入密码。然后就可以愉快地测试了。
本文作者:潘晓可
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!