0. 概述
在以前,我都是在本地开发机上运行 jupyter,但是,这在重启机器的时候和在不同开发机的情况下显得有点尴尬,为了方便,于是乎我就将 jupyter 架设在远程服务器上,本文就介绍一下我的架设过程。
1. 创建运行环境
[root@liqiang.io]# python36 -m virtualenv venv36
Using base prefix '/usr'
New python executable in /root/soft/jupyter/venv36/bin/python36
Also creating executable in /root/soft/jupyter/venv36/bin/python
Installing setuptools, pip, wheel...done.
[root@liqiang.io]# source venv36/bin/activate
[root@liqiang.io]# pip install jupyter
2. 配置 jupyter
[root@liqiang.io]# jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py
[root@liqiang.io]# python -c "from notebook.auth import passwd; print(passwd())"
Enter password:
Verify password:
sha1:sdfasd:abcsdfasdfasdfasdf
[root@liqiang.io]# vim /root/.jupyter/jupyter_notebook_config.py
图 1:设置 jupyter 密码 |
---|
3. 运行 jupyter
[root@liqiang.io]# jupyter notebook
[I 22:42:58.254 NotebookApp] Serving notebooks from local directory: /root/soft/jupyter
[I 22:42:58.255 NotebookApp] The Jupyter Notebook is running at:
[I 22:42:58.255 NotebookApp] http://localhost:8888/
[I 22:42:58.255 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
将 jupyter 作为 systemd 服务
[root@liqiang.io]# export NAME=jupyter
[root@liqiang.io]# cat >/etc/systemd/system/${NAME}.service <<EOF
[Unit]
Description=${NAME}
[Service]
Type=simple
ExecStart=/root/soft/venv/venv3/bin/python -m jupyter notebook --no-browser --allow-root
WorkingDirectory=/root/src/python/jupyter
User=root
Group=root
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
[root@liqiang.io]# systemctl daemon-reload
[root@liqiang.io]# systemctl enable jupyter
[root@liqiang.io]# systemctl start jupyter
4. 配置 Nginx 代理
[root@liqiang.io]# cat /root/.jupyter/jupyter_notebook_config.py | grep base
## The base URL for the notebook server.
c.NotebookApp.base_url = '/jupyter'
[root@liqiang.io]# cat /etc/nginx/jupyter.conf
location /jupyter {
proxy_pass http://127.0.0.1:8888;
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;
}
然后重新加载 Nginx 配置,Enjoy。