发布于 

开发工具-004-搭建jupyter远程服务

1、安装jupyter

conda create -n jupyter python=3.10
pip install jupyter

2、配置jupyter

设置密码,生成秘钥文件:

jupyter notebook password
Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /your_home/.jupyter/jupyter_notebook_config.json

查看秘钥:

$ cat ~/.jupyter/jupyter_notebook_config.json
{
"NotebookApp": {
"nbserver_extensions": {
"jupyter_nbextensions_configurator": true
},
"password": "argon2:$argon2id$v=19$m=10240,t=10,p=8$ADrvxc7AsirraAJFXTnNsg$I7KwbhJ+FXGRBW/lv3dfYw"
}
}

生成默认的Jupyter配置文件,用于启动服务。

jupyter notebook --generate-config

修复配置文件选项:

# 文件:~/.jupyter/jupyter_notebook_config.py

# 改成False,不允许从UI修改登录密码
c.NotebookApp.allow_password_change = False

# 改成你自己~/.jupyter/jupyter_notebook_config.json中的password内容
c.NotebookApp.password = 'argon2:$argon2id$v=19$m=10240,t=10,p=8$ADrvxc7AsirraAJFXTnNsg$I7KwbhJ+FXGRBW/lv3dfYw'

# 改成你自己服务器的IP地址!
c.KernelManager.ip = '192.168.3.10'

# 改成你需要的端口号,默认是8888
c.NotebookApp.port = 8888

# 设置为True,支持远程访问
c.NotebookApp.allow_remote_access = True

3、安装插件

pip install jupyter_contrib_nbextensions
jupyter contrib nbextensions install --user

pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

4、部署成systemd服务

在miniconda目录新建一个脚本,封装jupyter启动命令:

$ cat run_jupyter.sh
#!/bin/bash

rootDir=$(cd $(dirname $0); pwd)
source ${rootDir}/bin/activate jupyter
jupyter notebook /path/to/your_notebook_dir/

新建systemd服务配置文件

# 文件:~/.config/systemd/user/jupyter.service 
[Unit]
Description=jupyter daemon

[Service]
ExecStart=/usr/bin/bash /home/<user>/miniconda3/run_jupyter.sh

[Install]
WantedBy=default.target

启动systemd

# 使能服务
systemctl enable --user jupyter

# 启动服务
systemctl start --user jupyter

# 查看服务状态
systemctl status --user jupyter

参考

[1] 通过 conda 安装 jupyterlab 并配置为 systemd 服务