一、準備環境
- Centos 7.6
- Python 3.7
- Nginx
注意:Jupyter部署是在非root用戶下(這裏是abc用戶)
二、步驟
1. 安裝
pip3 install jupyter
安裝完成可以先測試:
jupyter notebook --no-browser
2. 生成配置文件
生成配置文件:
jupyter-notebook --generate-config
配置文件是默認是生成在當前用戶家目錄下的.jupyter目錄的
我這裏的配置文件地址
/home/abc/.jupyter/jupyter_notebook_config.py
注意:生成配置文件的時候可能會報錯:
PermissionError: [Errno 13] Permission denied: '/home/abc/.jupyter'
原因:這是因爲當前用戶權限不足,無法創建/home/abc/.jupyter
解決方式:需要在root用戶下
在/home/abc/下新建.jupyter目錄,並且賦予當前用戶該目錄的操作權限
3. 創建Jupyter默認工作目錄
這裏設置爲:
/home/abc/jupyter_dir
mkdir -p /home/abc/jupyter_dir
chown -R abc:abc /home/abc/jupyter_dir/
注意:當前用戶需要有操作該目錄的權限
4. 修改配置文件
修改配置文件
vim /home/abc/.jupyter/jupyter_notebook_config.py
修改爲:
c.NotebookApp.ip = '0.0.0.0' # 注意:不想要其他機器訪問,可以設置爲127.0.0.1
c.NotebookApp.password = '' # 密碼(加salt)
c.NotebookApp.open_browser = False # 啓動不打開瀏覽器
c.NotebookApp.port = 8888 # 對外提供訪問的端口
c.NotebookApp.notebook_dir = '/home/abc/jupyter_dir' # 默認工作目錄
c.NotebookApp.allow_root = True # 允許root用戶執行,root用戶下必須
注意:如果不使用密碼的話,那默認是使用token進行登錄的
注意:如果要指定password的話,需要在python/IPython的shell下生成密碼
from notebook.auth import passwd
passwd()
輸入密碼,就會生成加salt的密碼串,然後填寫到c.NotebookApp.password裏面去
5. 運行
jupyter notebook
後臺運行(需要提前創建好/tmp/jupyter目錄,並且當前用戶具有該目錄的操作權限)
nohup jupyter notebook > /tmp/jupyter/jupyter.log 2>&1 &
注意:運行的時候,可能會有報錯:
PermissionError: [Errno 13] Permission denied: '/home/abc/.local'
原因:當前用戶權限不足
解決方法:需要在/home/abc下新建.local目錄,並且當前用戶具有該目錄的操作權限
注意:如果別的機器訪問不通的話,可能是防火牆的問題,需要設置防火牆或者是關閉防火牆
6. Systemd接管Jupyter
爲了更好的監控Jupyter運行情況以及能夠失敗重啓,所以需要使用監控軟件來啓動Jupyter,這裏使用系統自帶的Systemd
我這裏是寫了Jupyter的啓動腳本,然後Systemd來接管該啓動腳本,爲什麼不直接用Systemd來接管Jupter呢,這是因爲Jupyter啓動的時候的過程無法記錄到日誌中
先寫個啓動腳本:
vim /home/abc/bin/jupyter.sh
#!/bin/bash
/usr/local/python3/bin/jupyter-notebook --no-browser > /tmp/jupyter/jupyter.log 2>&1
並且腳本需要賦予執行權限
chmod +x /home/abc/bin/jupyter.sh
創建一條service
vim /usr/lib/systemd/system/jupyter.service
[Unit]
Description=Jupyter-Notebook
[Service]
User=abc
Group=abc
Type=simple
ExecStart=/usr/bin/nohup /home/abc/bin/jupyter.sh &
[Install]
WantedBy=multi-user.target
Restart=on-failure
RestartSec=10
加載新的service,開機啓動並且啓動jupyter
systemctl daemon-reload
systemctl enable jupyter
systemctl start jupyter
到這裏之後,遠程機器是可以訪問該Linux的Jupyter的,但是爲了安全,最好是前面掛一個Nginx然後走域名的方式
7. Nginx
參考:https://segmentfault.com/a/1190000016627630
注意:Jupyter 使用了 websocket 協議,所以需要Nginx配置支持 websocket。
新建一個conf
vim /etc/nginx/conf.d/jupyter.test.com.conf
server {
server_name jupyter.test.com *.jupyter.test.com;
listen 80;
location / {
proxy_pass http://127.0.0.1:8888;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 120s;
proxy_next_upstream error;
}
}
刷新Nginx
nginx -s reload
外部機器設置hosts之後即可瀏覽器訪問jupyter.test.com
9. 安裝插件
安裝常用的Jupyter插件
pip3 install RISE
pip3 install autopep8
jupyter-nbextension install rise --py --sys-prefix
jupyter-nbextension enable rise --py --sys-prefix
在jupyter notebook裏面勾選:
Hinterland
Nbextensions edit menu item
Split Cells Notebook
Autopep8 # 需要 pip install autopep8
Nbextensions dashboard tab
Snippets
Table of Contents (2)
RISE # 需要 pip install RISE