Nginx - Jupyter + Nginx

在服务器上部署Jupyter,使Jupyter成为一个云笔记本


创建账户并安装Jupyter 和 Nginx

首先创建一个账户:

1
2
3
adduser ubuntu
# 添加到管理组
adduser ubuntu sudo

创建脚本,运行一下命令:

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

#!/bin/bash

sudo apt-get update -y

sudo apt-get install build-essential libssl-dev libpq-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip -y

sudo apt-get install supervisor -y

sudo apt-get install python3-pip python3-dev python3-venv -y

sudo apt-get install nano -y

sudo apt-get install git -y

sudo apt-get install nginx curl -y

sudo apt-get install ufw -y

sudo ufw allow 'Nginx Full'

sudo ufw allow ssh

sudo python3 -m pip install jupyter

sudo service supervisor start

sudo apt autoremove -y

通过ip访问主机: http://47.100.78.128 访问你的主机。应该会出现一个Nginx的初始界面。


进行Jupyter 配置

配置Jupyter config:

生成配置文件:

1
2
3
4
jupyter notebook --generate-config

# 生成哈希密码
ipython -c "from notebook.auth import passwd; passwd()"

更新配置文件:

1
sudo nano /home/ubuntu/.jupyter/jupyter_notebook_config.py

添加配置到config里面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
c = get_config()

# Kernel config
c.IPKernelApp.pylab = 'inline' # if you want plotting support always in your notebook

# Notebook config

c.NotebookApp.allow_origin = 'http://107.21.189.212' # put your public IP Address here
c.NotebookApp.ip = '*'
c.NotebookApp.allow_remote_access = True
c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:45e47cb75d9e:49dc0b09f4e671485b6113c1e2c5a13d7d37fa78'
c.NotebookApp.port = 8888

# For https & letsencrypt later
# c.NotebookApp.certfile = u'/your/cert/path/cert.pem'
# c.NotebookApp.keyfile = u'/your/cert/path/privkey.pem'

Nginx设置

这里安装的是Nginx,使用Apache2原理一样只是麻烦点。创建jupyter_config配置文件,名称可以任意:

1
sudo nano /etc/nginx/sites-available/jupyter_app.conf

配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
server_name jupyter_notebook;
listen 80;
listen [::]:80;

location / {
include proxy_params;
proxy_pass http://localhost:8888;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
}

如果您打算使用自定义域,请替换server_name jupyter_notebook;server_name your_custom_domain.com;

此外还需要在jupyter_notebook_config.py中更该为c.NotebookApp.allow_origin = '*'

启动配置文件。并重启服务器

1
2
3
4
5
6
7
sudo ln -s /etc/nginx/sites-available/jupyter_app.conf /etc/nginx/sites-enabled/jupyter_app.conf

sudo rm /etc/nginx/sites-enabled/default

systemctl daemon-reload

sudo systemctl reload nginx

创建后台运行Jupyter

创建配置文件:

1
sudo nano /etc/supervisor/conf.d/my_jupyter.conf

配置如下:

1
2
3
4
5
6
7
8
9

[program:my_jupyter]
user=ubuntu
directory=/home/ubuntu
command=jupyter notebook
autostart=true
autorestart=true
stdout_logfile=/var/log/my_jupyter/stdout.log
stderr_logfile=/var/log/my_jupyter/stderr.log

启动配置程序:

1
2
3
4
5
6
# 创建日志
sudo mkdir /var/log/my_jupyter

# 启动配置
sudo supervisorctl reread
sudo supervisorctl update

其他

  1. 此方法还可以使用JupyterLab和JupyterHub

  2. 其他想不起来了