0. 概述

在以前,我都是在本地开发机上运行 jupyter,但是,这在重启机器的时候和在不同开发机的情况下显得有点尴尬,为了方便,于是乎我就将 jupyter 架设在远程服务器上,本文就介绍一下我的架设过程。

1. 创建运行环境

  1. [root@liqiang.io]# python36 -m virtualenv venv36
  2. Using base prefix '/usr'
  3. New python executable in /root/soft/jupyter/venv36/bin/python36
  4. Also creating executable in /root/soft/jupyter/venv36/bin/python
  5. Installing setuptools, pip, wheel...done.
  6. [root@liqiang.io]# source venv36/bin/activate
  7. [root@liqiang.io]# pip install jupyter

2. 配置 jupyter

  1. [root@liqiang.io]# jupyter notebook --generate-config
  2. Writing default config to: /root/.jupyter/jupyter_notebook_config.py
  3. [root@liqiang.io]# python -c "from notebook.auth import passwd; print(passwd())"
  4. Enter password:
  5. Verify password:
  6. sha1:sdfasd:abcsdfasdfasdfasdf
  7. [root@liqiang.io]# vim /root/.jupyter/jupyter_notebook_config.py
图 1:设置 jupyter 密码

3. 运行 jupyter

  1. [root@liqiang.io]# jupyter notebook
  2. [I 22:42:58.254 NotebookApp] Serving notebooks from local directory: /root/soft/jupyter
  3. [I 22:42:58.255 NotebookApp] The Jupyter Notebook is running at:
  4. [I 22:42:58.255 NotebookApp] http://localhost:8888/
  5. [I 22:42:58.255 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

将 jupyter 作为 systemd 服务

  1. [root@liqiang.io]# export NAME=jupyter
  2. [root@liqiang.io]# cat >/etc/systemd/system/${NAME}.service <<EOF
  3. [Unit]
  4. Description=${NAME}
  5. [Service]
  6. Type=simple
  7. ExecStart=/root/soft/venv/venv3/bin/python -m jupyter notebook --no-browser --allow-root
  8. WorkingDirectory=/root/src/python/jupyter
  9. User=root
  10. Group=root
  11. Restart=on-failure
  12. RestartSec=5s
  13. [Install]
  14. WantedBy=multi-user.target
  15. EOF
  16. [root@liqiang.io]# systemctl daemon-reload
  17. [root@liqiang.io]# systemctl enable jupyter
  18. [root@liqiang.io]# systemctl start jupyter

4. 配置 Nginx 代理

  1. [root@liqiang.io]# cat /root/.jupyter/jupyter_notebook_config.py | grep base
  2. ## The base URL for the notebook server.
  3. c.NotebookApp.base_url = '/jupyter'
  4. [root@liqiang.io]# cat /etc/nginx/jupyter.conf
  5. location /jupyter {
  6. proxy_pass http://127.0.0.1:8888;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header Host $host;
  9. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  10. proxy_http_version 1.1;
  11. proxy_set_header Upgrade $http_upgrade;
  12. proxy_set_header Connection "upgrade";
  13. proxy_redirect off;
  14. }

然后重新加载 Nginx 配置,Enjoy。

5. Ref