如何在服务器上凑一个 JupyterLab
Mar 25, 2021
在个人服务器上安装一个 Jupyer Lab
可能可以更折腾方便的学习和使用 Python
和 R
。
此次安装环境为 Debian 10 (buster), 64-bit PC。
Step 1 下载 conda
安装包并安装
conda
是一种通用包管理系统,旨在构建和管理任何语言和任何类型的软件。 Anaconda
则是一个打包的集合,里面预装好了 conda
、某个版本的 python
、众多 packages
、科学计算工具等等。 Miniconda
,顾名思义,它只包含最基本的内容 ——python
与 conda
,以及相关的必须依赖项。对于空间要求严格的用户,Miniconda
是一种选择。
apt update && apt upgrade -y
# 视具体情况确定下载链接
wget <https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh>
bash Miniconda3-latest-Linux-x86_64.sh
初始化当前的 bash
,使 conda
的系统变量生效:
source .bashrc
Step 2 安装 Jupyter Notebook
如果上一步安装的是 Anaconda
,则 Jupyter Lab
已经预装至系统中了。如果上一步安装的是 Miniconda
,则需要通过 conda
安装 Jupyter Lab
:
conda install jupyterlab
Step 3 Jupyter Notebook
的配置和运行
生成 Jupyter Lab
配置文件,配置文件默认路径为 /root/.jupyter/jupyter_lab_config.py
:
jupyter lab --allow-root --generate-config
为 Jupyter Lab
生成密码,密码文件默认路径为 /root/.jupyter/jupyter_lab_config.py
,复制密码文件中的哈希格式密码备用:
jupyter lab password
编辑配置文件,修改如下项:
c.ServerApp.allow_password_change = True ## 允许后期修改密码
c.ServerApp.allow_remote_access = True ## 允许远程访问
c.ServerApp.allow_root = False ## 允许以 root 权限运行
c.ServerApp.ip = '0.0.0.0' ## 访问 ip
c.ServerApp.root_dir = '/root' ## Jupyter Lab 根目录
c.LabServerApp.open_browser = False
c.ServerApp.password = 'xxxx' ## 哈希值格式密码
c.ServerApp.port = 8888 ## Jupyter Lab 端口
此时即可运行 Jupyter Lab
:
jupyter lab
默认界面是英文。可以安装简体中文语言包:
conda install jupyterlab-language-pack-zh-CN
Step 4 设置系统服务(随系统启动)
完成 Step 3 之后 Jupyter Lab
已经可以正常运行,通过浏览器访问 http://yout_server_ip:port
即可访问。但此时如果关闭 SSH 连接 Jupyter Notebook
进程也会被杀掉。可以通过将 Jupyter Lab
注册为系统服务而达到后台运行和随系统启动的目的。
在 /etc/systemd/system/
目录下创建 jupyter.service
文件:
[Unit]
Description=Jupyter Lab
[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/root/miniconda3/bin/jupyter-lab --config=/root/.jupyter/jupyter_lab_config.py
User=root
Group=root
WorkingDirectory=/root/jupyter_lab
Restart=always
RestartSec=10
#KillMode=mixed
[Install]
WantedBy=multi-user.target
即可设置 Jupyter Lab
随系统运行:
systemctl enable jupyter
systemctl restart jupyter
Step 5 设置 R 环境
Miniconda 默认的是 Python 环境。但我等医学狗当然是离不开 R 环境的,所以还需要在 Jupyter Lab 中安装 R 环境并且和 Jupyter Lab 集成。
启用 conda-forge 源:
conda config --add channels conda-forge
conda config --set channel_priority strict
安装 R-base:
conda install -c conda-forge r-base=4.1.3 # 指定所需版本
which R
安装 IRkernel 模块并激活。
install.packages('IRkernel')
IRkernel::installspec()
额外配置:域名访问
记忆 IP 终究不太容易也不方便,可以考虑使用反向代理来通过域名访问远端的 Jupyuter Lab
。
可以使用 Nginx
作为反代服务器,注意配置文件:
upstream notebook {
server ip:port;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name your_domain;
ssl_certificate /etc/nginx/cert/your.crt;
ssl_certificate_key /etc/nginx/cert/your.key;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m;
ssl_session_tickets off;
ssl_protocols TLSv1.3;
ssl_prefer_server_ciphers on;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-Xss-Protection 1;
add_header Strict-Transport-Security "max-age=63072000" always;
ssl_stapling on;
ssl_stapling_verify on;
location / {
proxy_pass <http://notebook>;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
location ~ /api/kernels/ {
proxy_pass <http://notebook>;
proxy_set_header Host $host;
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
location ~ /terminals/ {
proxy_pass <http://notebook>;
proxy_set_header Host $host;
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
proxy_read_timeout 86400;
}
}
完工。 🎉