0. 概述

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

1. 创建运行环境

  1. [[email protected].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. [[email protected].io]# source venv36/bin/activate
  7. [[email protected].io]# pip install jupyter

2. 配置 jupyter

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

3. 运行 jupyter

  1. [[email protected].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).

4. 配置 Nginx 代理

  1. [[email protected].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. [[email protected].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