我的博客

在服务器上配置jupyter

目录
  1. 安装
  2. 配置
    1. 1.创建专用的用户(可选)
    2. 2.创建配置文件
    3. 3.创建密码
    4. 4.生成https使用的证书(可选,如果不用https可跳过)
    5. 5.修改配置文件
    6. 6.启动Jupyter Notebook
    7. 7.防火墙开放端口
    8. 8.如果开启了https,在本地浏览器添加证书信任
  3. 扩展 Jupyter Nbextensions
  4. 使用 nginx 转发 jupyter

有两种方案:

  1. 直接监听公网 ip 或域名,如需 ssl 和域名绑定,要在 jupyter 中配置证书和私钥
  2. 监听 localhost,再通过 nginx 转发,如需 ssl 和域名绑定,在 nginx 中配置

安装

1
pip install jupyter

一些新版本的Jupyter Notebook可能无法正常加载Jupyter Notebook Extensions。可以尝试安装低版本Jupyter Notebook。

1
pip install -U "notebook<6.0"

参考 Github Issue

配置

1.创建专用的用户(可选)

1
2
useradd -d /home/jupyter -m jupyter
su - jupyter

2.创建配置文件

1
jupyter notebook --generate-config

配置文件默认在用户 家目录下的 .jupyter/jupyter_notebook_config.py

3.创建密码

1
jupyter notebook password

4.生成https使用的证书(可选,如果不用https可跳过)

生成私钥,自签名证书

  1. 生成私钥

    1
    openssl genrsa -des3 -out ssl.key 1024

    这要求输入密码可以再使用 openssl rsa -in ssl.key -out jupyter.key

    输入一次密码,生成不用密码的ssl.key,再cp jupyter.key ssl.key覆盖原来的带密码的key

  2. 生成签名请求

    1
    openssl req -new -key ssl.key -out jupyter.csr

    提示输入的内容直接全点回车,或则自己根据情况输入

  3. 签发证书

    1
    openssl x509 -req -days 365 -in jupyter.csr -signkey jupyter.key -out jupyter.crt

5.修改配置文件

vi ~/.jupyter/jupyter_notebook_config.py

  1. 绑定IP或域名 c.NotebookApp.ip='你希望访问的ip或者域名'

而且应该设置允许远程访问

c.NotebookApp.allow_remote_access = True

我在4.4.0版本未设置这个地方,没有问题,但是4.5.0时会遇到错误

  1. 开启https(可选)

    1
    2
    c.NotebookApp.keyfile = '/home/jupyter/jupyter.key' 
    c.NotebookApp.certfile = '/home/jupyter/jupyter.crt'
  1. 不启动浏览器 c.NotebookApp.open_browser = False

6.启动Jupyter Notebook

新建一个目录,mkdir workspace; cd workspace

nohup jupyter notebook > ../log 2>&1 &

7.防火墙开放端口

可以先试一下能否访问到,如果访问不到可能是防火墙未开放相关端口(默认是8888端口),先使用firewall-cmd --state查看防火墙状态,确认防火墙是开启的。此时需要使用root身份或者用sudo。

1
2
firewall-cmd --permanent --zone=public --add-port=8888/tcp
firewall-cmd --reload

永久开放tcp 8888端口并重新载入。

8.如果开启了https,在本地浏览器添加证书信任

因为很多浏览器会阻止自签发的证书

另外需要注意如果你开启了https,就不要使用http访问

扩展 Jupyter Nbextensions

https://github.com/ipython-contrib/jupyter_contrib_nbextensions

https://github.com/Jupyter-contrib/jupyter_nbextensions_configurator

1
2
3
4
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

使用 nginx 转发 jupyter

需要额外配置 local_hostnames 和 allow_origin

1
2
3
c.NotebookApp.local_hostnames = ['xxx.codeplot.top']
# Takes precedence over allow_origin_pat.
c.NotebookApp.allow_origin = '*'

不必修改 NotebookApp.ip、NotebookApp.allow_remote_access、NotebookApp.keyfile、NotebookApp.certfile 的配置。

想开启 ssl 可以配置 nginx,详见 CentOS 7 下编译安装nginx并配置服务

评论无需登录,可以匿名,欢迎评论!