Background

記錄一下裝 Jupyter notebook 的過程。Jupyter notebook 蠻適合當簡易 Console & Demo site的。剛好公司有機會用到就記錄一下。

Set Up Python and Basic Environment

$ sudo apt-get update
$ sudo apt install python3-pip python3-dev
$ mkdir ~/jupyter
$ cd jupyter
$ virtualenv .venv
$ source .venv/bin/activate

Installation

$ pip3 install jupyter
$ jupyter notebook
[I 19:00:06.165 NotebookApp] Writing notebook server cookie secret to /run/user/1000/jupyter/notebook_cookie_secret
[I 19:00:07.315 NotebookApp] Serving notebooks from local directory: /home/ubuntu/jupyter
[I 19:00:07.315 NotebookApp] The Jupyter Notebook is running at:
[I 19:00:07.316 NotebookApp] http://localhost:8888/?token=d726c482470c2e88db39d8023e918a3646afbb742bff14ad
[I 19:00:07.316 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 19:00:07.319 NotebookApp] No web browser found: could not locate runnable browser.
[C 19:00:07.319 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///run/user/1000/jupyter/nbserver-4049-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=d726c482470c2e88db39d8023e918a3646afbb742bff14ad

看一下 jupyter 有沒有正常啟動 如果有的話我們就先按 ctrl + c 取消它。

Config

$ jupyter notebook --generate-config

jupyter_notebook_config.py

$ c.NotebookApp.allow_remote_access = True
c.NotebookApp.ip = 'localhost'
c.NotebookApp.notebook_dir = '/home/ubuntu/jupyter'
c.NotebookApp.open_browser = False
c.NotebookApp.password = 'sha1:1fc1dd64fae1:3f5a8b523778ae324af4252caf4ac12db44ebf42'
c.NotebookApp.port = 8888

Password 由下面的程式產生,進入到 Python Shell 執行即可。

$ from notebook.auth import passwd
$ passwd()
Enter password:
Verify password:
Out[2]: 'sha1:1fc1dd64fae1:3f5a8b523778ae324af4252caf4ac12db44ebf42'

Nginx

server {
    listen 80;
    server_name jupyter.pgmcsi.com;

    location / {
        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;
    }
}
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl restart nginx

P.S 記得要建 soft link

Supervisor

[program:jupyter]
command = /path/to/jupyter/.venv/bin/jupyter notebook --no-browser --config=/path/to/config_file
directory = /path/to/jupyter/folder
user = ubuntu
autostart = true
autorestart = true

stderr_logfile=/var/log/supervisor/juypter.err.log
stdout_logfile=/var/log/supervisor/juypter.out.log

stdout_logfile_maxbytes=10MB
stdout_logfile_backups=5
stdout_capture_maxbytes=10MB

stderr_logfile_maxbytes=10MB
stderr_logfile_backups=5
stderr_capture_maxbytes=10MB
$ sudo supervisorctl reread
$ sudo supervisorctl update
$ sudo supervisorctl status
jupyter  RUNNING   pid 8287, uptime 0:00:05

Reference