MENU

云计算平台Jupyterlab搭建指南

October 20, 2023 • 极意阅读设置

本文的意图是建立一个私有的云计算IDE,使用的是交互式计算的Jupyter软件,支持python,MM等等编程语言。Jupyter用久了确实已经离不开了,感觉基本是用python做科学计算的最佳IDE。

本文的实现方式是在服务器上部署Jupyterlab, 然后使用Nginx反向代理从而达到Https访问(Http不安全且无法拖拽文件),最后使用systemd保证文件在后台的正常运行。

原本还以为这种东西轮子应该很多了,结果搜出来的没几个能直接套用的,这确实让我有点意外,所以只能自己从头到尾重新弄了一遍,请参加下文,测试系统为Debian11。

### —— Conda安装配置Jupyterlab ——

首先安装Anaconda

# 安装依赖
sudo apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6

wget https://repo.anaconda.com/archive/Anaconda3-2021.11-Linux-x86_64.sh

bash Anaconda3-2021.11-Linux-x86_64.sh

把conda添加到系统环境

sudo nano /etc/profile
#把下面两行放在文件里面,记得修改你的user为自己的
#Anacondaexport 
export PATH=$PATH:/home/user/anaconda3/bin
#激活环境
source /etc/profile

现在你敲入conda -V应该就能正常显示版本号了

安装jupyterlab,并生成配置文件

conda install jupyterlab

# 输入密码并保存好(这样的权限比修改py配置文件要高,且不太会出问题)
jupyter lab password

# 生成配置文件
jupyter lab --generate-config

编辑刚才你生成的配置文件

c.ServerApp.password_required = True
c.ServerApp.port = 端口号,默认为8888,建议修改
c.ServerApp.notebook_dir = u'每次打开时的文件位置'
c.ServerApp.allow_remote_access = True

这里还要记得调整上文中文件位置的权限,确保user拥有对应位置的访问权限

chown -R 用户名:用户名 位置
chmod -R 755 位置
### —— Nginx反代(Https) ——

安装nginx并添加反代文件(域名和ssl密钥不在本文范围)

sudo apt install -y nginx
# 添加配置文件
sudo nano /etc/nginx/conf.d/jupyterlab.conf

把配置文件按下文模式修改

map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}

server {
        listen 80;
        server_name 你的域名;
        rewrite ^(.*)$ https://你的域名$1 permanent;
}
server {
        listen 443 ssl http2;
        server_name 你的域名;
        index index.html index.htm index.php default.html default.htm default.php;

        ssl_certificate        /etc/letsencrypt/live/你的密钥路径(域名)/fullchain.pem;
        ssl_certificate_key    /etc/letsencrypt/live/你的密钥路径(域名)/privkey.pem;
        ssl_session_timeout 10m;
        ssl_session_cache shared:SSL:10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_stapling on;
        ssl_prefer_server_ciphers on;



    location / {
        proxy_pass            http://localhost:上文自定义的端口;
        proxy_set_header      Host $http_host;
    }

    location /api/kernels/ {
        proxy_pass            http://localhost:上文自定义的端口;
        proxy_set_header      Host $http_host;
        # websocket support
        proxy_http_version    1.1;
        proxy_set_header      Upgrade "websocket";
        proxy_set_header      Connection "Upgrade";
        proxy_read_timeout    86400;
    }
}

测试并重启nginx

nginx -t
sudo systemctl reload nginx
### —— Systemd配置 ——

新建Systemd配置文件

sudo nano /etc/systemd/system/jupyterlab.service

修改为如下,记得修改下面的用户名为你自己的

[Unit]
Description=jupyterlab

[Service]
User=用户名
PIDFile=/run/jupyter.pid
ExecStart=/home/用户名/anaconda3/bin/python -m jupyter-lab  --no-browser User=user
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

重新加载systemd配置,并设置开机自启动

sudo systemctl daemon-reload
sudo systemctl enable jupyterlab.service
sudo systemctl enable nginx

启动服务或者重启即可正常访问,如有问题记得检查防火墙,还有问题的话用下面方式查看日志

journalctl -u jupyterlab.service --since "30 min ago"

感谢阅读,希望能对你有帮助~

### —— 另附:VPS简单处理与miniconda ——

另附一个miniconda的版本,顺带添加了很多vps的一些基础操作

创建子用户并赋予sudo权限

adduser 用户名
sudo usermod -aG sudo 用户名
#重复下面生成密钥
nano  /etc/ssh/sshd_config

设置密钥登陆并关闭ssh用户密码登陆,可参加下文

设置 SSH 通过密钥登录 | 菜鸟教程

编辑下面的ssh配置文件,关闭root用户登陆

nano /etc/ssh/sshd_config

安装zsh

sudo apt install zsh curl git
# oh my zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

#zsh-syntax-highlighting
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

#zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

nano ~/.zshrc
# 修改该行,添加自动建议和高亮
plugins=(git zsh-autosuggestions zsh-syntax-highlighting)

安装 miniconda

mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh

~/miniconda3/bin/conda init zsh

配制需要的环境,这里以爬虫环境为例子

conda create -n spider python=3.10
conda activate spider

#如果在安装过程中因为内存不足被杀进程了,请通过下面的脚本添加swap
wget https://www.moerats.com/usr/shell/swap.sh && sudo bash swap.sh

conda install -c conda-forge jupyterlab requests
pip install pyTelegramBotAPI
conda install -c conda-forge pyquery requests-toolbelt fake-useragent nest-asyncio
conda install -c conda-forge selenium chromedriver-autoinstaller webdriver-manager
conda install -c conda-forge numpy jupyterlab-lsp python-lsp-server
pip install requestium undetected-chromedriver

生成证书

sudo apt install certbot
sudo certbot certonly --standalone --agree-tos -d 你的域名

配制nginx反代和自动更新证书请参见上文。

另附另外一种syscmd配置,适配上文安装的非默认base环境

[Service]
Type=simple
User=ozonoo
Environment="PATH=/home/用户名/miniconda3/envs/spider/bin:/home/用户名/miniconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/games"
ExecStart=/home/用户名/ conda3/envs/spider/bin/jupyter-lab  --no-browser
WorkingDirectory=/home/ozonoo
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
Last Modified: February 18, 2024