搭建 Jupyter Notebook 服务器
2017-06-19 19:05
876 查看
服务器端 Jupyter Notebook 的安装
原材料:- Linux 服务器;
- 已配置好的 Python 环境,且已经安装
jupyter和
ipykernel等必需的包。推荐使用 Anaconda。
打开 IPython,输入
from notebook.auth import passwd passwd()
程序会要求输入密码并确认,随后会输出此密码对应的 hash(格式为
'type:salt:hashed-password'),比如
'sha1:9305015210b4:32cad364c85e7f7a13b9efa137cfadf633cc62de'。记下此 hash 字串,随后会用到。
查看用户目录
~/.jupyter路径下是否存在
jupyter_notebook_config.py文件。若不存在,使用
jupyter notebook --generate-config
产生此文件。编辑此文件,写入
c.NotebookApp.ip = '*' # 允许访问此服务器的 IP,星号表示任意 IP c.NotebookApp.password = u'sha1:xxx:xxx' # 之前生成的密码 hash 字串 c.NotebookApp.open_browser = False # 运行时不打开本机浏览器 c.NotebookApp.port = 12035 # 使用的端口 c.NotebookApp.enable_mathjax = True # 启用 MathJax
然后在某个路径下执行
jupyter notebook
即可启动 Jupyter Notebook,执行命令的路径成为 Jupyter Notebook 的默认根路径,使用
http://IP:PORT的方式即可使用。
配置域名
这里我们使用 Nginx 作为服务器程序。按照上面的方法配置 Jupyter Notebook,如果仅仅对端口号进行代理转发,会出现 terminal 可以正常创建而 notebook 无法创建的情况。这是因为 Jupyter 会对 http 请求进行判断,所以反向代理时需要设置正确的信息。正确配置 nginx 反向代理的方式如下:server { server_name DOMAIN IP_ADDRESS; # 服务器域名和 IP 地址 listen 80; location / { proxy_pass http://127.0.0.1:JUPYTER_PORT/; # JUPYTER_PORT 为 Jupyter 运行端口 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; } }
配置 SSL
使用 SSL 的好处是显而易见的,尤其是在目前免费的 SSL 证书没有任何申请门槛的情况下,几乎没有理由不为自己的站点实现全面的 https 化。给服务器端的 Jupyter Notebook 启用 SSL 的方法是,在~/.jupyter/jupyter_notebook_config.py文件中增加两行
c.NotebookApp.certfile = '/path/to/ssl.pem' # SSL 证书文件的路径 c.NotebookApp.keyfile = '/path/to/ssl.key' # SSL 密钥文件的路径
同时,如果配置了域名,则 Nginx 配置也应该增加 443 端口的监听
server { server_name DOMAIN IP_ADDRESS; # 服务器域名和 IP 地址 listen 80; location / { root /var/www/notebook_ssl; # 此路径下放置一个 index.html,将连接转移到 https } } server { server_name DOMAIN IP_ADDRESS; # 服务器域名和 IP 地址 listen 443; ssl on; ssl_certificate /path/to/ssl.pem; # SSL 证书文件的路径 ssl_certificate_key /path/to/ssl.key; # SSL 密钥文件的路径 ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { proxy_pass https://127.0.0.1:JUPYTER_PORT/; # JUPYTER_PORT 为 Jupyter 运行端口 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; } }
因为 Jupyter Notebook 开启 SSL 之后,http 连接就完全不能使用了,所以需要将所有的 http 连接请求重定向到 https。实现此目的的方法较多,这里借鉴了百度,使用一种简单高效的方法:利用 meta 的刷新作用,在 80 端口的根目录下放置一个
index.html文件,内容是
<html> <meta http-equiv="refresh" content="0;url=https://DOMAIN "> </html>
添加 Jupyter Notebook 为系统服务
将 Jupyter Notebook 设定为系统服务并且开机自动启动,这里以systemd下的设定为例,创建文件
/etc/systemd/system/jupyter.service文件,内容是
[Unit] Description=Jupyter Notebook After=network.target [Service] Type=simple PIDFile=/path/to/your/jupyter.pid ExecStart=/path/to/your/jupyter-notebook --config=/home/your_username/.jupyter/jupyter_notebook_config.py User=your_username Group=your_group WorkingDirectory=/path/to/your/notebook/working_directory/ Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
保存文件之后执行
systemctl enable jupyter
来使此服务自启动。此外还需要在
~/.jupyter/jupyter_notebook_config.py文件中设定 Jupyter Notebook 运行时默认的根目录
c.NotebookApp.notebook_dir = '/path/to/your/notebook/working_directory' # 运行时默认的根目录
相关文章推荐
- 使用ssh搭建jupyter notebook服务器
- 在服务器搭建Jupyter notebook
- 本地远程访问Ubuntu16.04.3服务器上的Jupyter notebook
- windows远程连接服务器中的jupyter notebook
- Mac下Jupyter(即IPython-notebook)的搭建及使用(更建议安装Anaconda环境直接自带jupyter notebook)
- 在centos7服务器上安装anaconda和jupyter notebook
- 使用pelican搭建一个Jupyter Notebook数据科学博客
- 深度学习环境搭建:linux下 Ubuntu16.04+cuda8.0+cudnn+anaconda+tensorflow并配置远程访问jupyter notebook
- 【手把手教你】如何在Linux系统搭建jupyter notebook
- 阿里云ECS搭建Jupyter Notebook
- jupyter notebook 架设在服务器 在windows 本地用浏览器的访问服务器上的jupyter
- Ubuntu 16.04 LTS 配置 Jupyter notebook 为服务器
- Docker: 使用jupyter notebook基础镜像搭建自己的 pytorch 开发环境
- 利用Anaconda搭建TensorFlow环境并在Jupyter Notebook使用
- centos7 和ubuntu下搭建 jupyter notebook,配置R环境
- jupyter notebook + pyspark 环境搭建
- mac 下在服务器上运行 Jupyter notebook
- 深度学习环境搭建:linux下 Ubuntu16.04+cuda8.0+cudnn+anaconda+tensorflow并配置远程访问jupyter notebook
- VPS搭建Tensorflow Jupyter Notebook 教程
- 在服务器上配置jupyter notebook