原文:https://zhuanlan.zhihu.com/p/340249373

搭建环境的前期准备

  • 一台服务器
  • 一个备案的域名
  • ssl证书(在阿里云搜索ssl,可以免费申请)

Jupyter 环境的简单安装

  • 我使用的ubuntu 20.04系统

安装anconda

安装 Jupyter Notebook

  • 切换到python环境下:
1
(base)$ conda install jupyter notebook

  • 输入python,配置jupyter密码
1
2
3
4
(base)$ python
>>> from notebook.auth import passwd
>>> passwd() # 这一步之后输入你自己的密码并重复输入一次。会输出一长串加密后的东西,将其复制下来后面要用!!!
>>> quit() # 退出 Python 环境

  • 上传SSL认证文件

ssl申请后,点击下载,获得一个 pem 格式的证书文件和一个 key 格式的文件,并上传到服务器的文件夹里后面要用。

  • 配置jupyter
1
2
3
(base)$ jupyter notebook --generate-config   创建配置文件
(base)$ cd ~/.jupyter # 进入 jupyter 配置目录
(base)$ vim jupyter_notebook_config.py # 打开配置文件

  • 打开配置文件后,在最后添加
1
2
3
4
5
6
7
8
9
c.NotebookApp.allow_origin = '*'
c.NotebookApp.allow_remote_access = True
c.NotebookApp.certfile = '/xxx/xx.com.pem' # 这里是 pem 证书文件的路径
c.NotebookApp.ip = '127.0.0.1' # 除非 IP 裸连否则不要改成 '*'
c.NotebookApp.keyfile = '/xxx/xx.com.key' # 这里是 key 文件的路径
c.NotebookApp.notebook_dir = '/xxx/jupyter' # 这里是你想让 Jupyter 放项目的文件夹
c.NotebookApp.password = 'xxxxxxxx'# 这里放你刚刚设置密码的时候生成的那一长串东西
c.NotebookApp.open_browser = False # 服务器没有浏览器 开什么开
c.NotebookApp.port = 1111 # 这里是 Jupyter 的端口,自己选个就行,反正后面要做端口转发

  • 完成了jupyter安装,运行测试(要先切换到对应的Python环境)
1
(base)$ jupyter notebook --allow-root

jupyter通过域名访问

  • 进入nginx目录

(base)$ cd /www/server/nginx/config #直接进入nginx的配置目录

最初的配置目录没有jupyter.conf,要把jupyter的配置信息写入到jupyter.conf后,再让nginx.conf调用

  • 创建jupyter.conf
1
(base)$ vim jupyter.conf

写入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
server {
# 将服务转发到 443 端口,通过 https 访问
listen 443 ssl;

# 这里填写你的域名
server_name xxx.com;

# ssl 配置,填写你的 pem 和 key 路径
ssl on;
ssl_certificate /home/ubuntu/.jupyter/xxx.pem;
ssl_certificate_key /home/ubuntu/.jupyter/xxx.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

# 下面这段是 Websocket 配置
location / {
# 注意这里 8888 改成你自己用的端口,更注意这里放 https 而不是 http
proxy_pass https://127.0.0.1:8888;
index dashboard index;

#websocket额外配置开始
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 连接超时时间,不能设置太长会浪费连接资源
proxy_connect_timeout 60s;
# 读超时时间
proxy_read_timeout 500s;
# 写超时时间
proxy_send_timeout 500s;
#websocket额外配置结束

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

# 日志路径设置
access_log /home/ubuntu/.jupyter/jupyter.log;
error_log /home/ubuntu/.jupyter/error.log;
}

  • **(重要)引用jupyter.conf:**在nginx.conf的http开头的括号内,添加include jupyter.conf  来
1
2
3
4
(base) $ vim nginx.conf
#在http括号内添加{
include jupyter.conf
}

  • 重启nginx服务,如果期间遇到问题,百度查询下报错的信息,基本能解决

  • 开启jupyter:后台启动jupyter并保持运行,把运行信息写入jupyter.log
1
(base) ubuntu$ nohup jupyter notebook --allow-root > jupyter.log 2>&1 & # 后台运行 Jupyter

相关推荐

jupyter插件