1. 程式人生 > 實用技巧 >Ubuntu 16.04遠端配置Jupyter Notebook

Ubuntu 16.04遠端配置Jupyter Notebook

安裝和配置Jupyter Notebook

安裝jupyter notebook

conda

conda install -c conda-forge notebook

pip

pip install notebook

安裝好之後,執行:

jupyter notebook

看是否能成功執行

配置Jupyter notebook

  1. 首先生成配置檔案:sudo jupyter notebook --generate-config
  2. 然後修改位於~/.jupyter/jupyter_notebook_config.py處的配置檔案:
    1. notebook檔案根目錄:
      ## The directory to use for notebooks and kernels.
      #  Default: ''
      c.NotebookApp.notebook_dir = '<PATH_TO_DIR>'
      
    2. notebook檔案根目錄:
      ## The port the notebook server will listen on (env: JUPYTER_PORT).
      #  Default: 8888
      c.NotebookApp.port = 51234
      
  3. 執行命令:
    jupyter notebook --no-browser --config=<ABSPATH_TO_CONFIG>/.jupyter/jupyter_notebook_config.py
    

把jupyter notebook的啟動合成到一個命令

為啟動notebook的命令設定別名:

alias jlremote='jupyter notebook --no-browser --config=<ABSPATH_TO_CONFIG>/.jupyter/jupyter_notebook_config.py'

然後將這句命令新增到~/.bashrc~/.bash_profile,如果是Mac OS則新增到~/.zshrc
之後執行:

jlremote

即可。為了使notebook能在後臺執行,可以用screen命令為其單開一個screen執行。

把jupyter notebook作為linux service執行

sudo vim /etc/systemd/system/jupyter.service

在這個檔案裡填入

[Unit]
Description=Jupyter Notebook

[Service]
WorkingDirectory=<PATH_TO_DIR>
ExecStart=<ABSPATH_TO_CMD>/jupyter notebook --no-browser --config=<ABSPATH_TO_CONFIG>/.jupyter/jupyter_notebook_config.py
Restart=always
RestartSec=10
SyslogIdentifier=jupyter-notebook
User=<USER NAME>

[Install]
WantedBy=multi-user.target

然後安裝並執行該服務:

sudo systemctl enable jupyter.service
sudo systemctl start jupyter.service
sudo systemctl status jupyter.service

遠端連線jupyter

使用SSH隧道

~/.bashrc~/.bash_profile,如果是Mac OS則在~/.zshrc新增:

function jllocal {
  port=YOUR PORT 
  remote_username=USERNAME
  remote_hostname=HOSTNAME
  url="http://localhost:$port" 
  echo "Opening $url"
  open "$url"
  cmd="ssh -CNL localhost:"$port":localhost:"$port" $remote_username@$remote_hostname" 
  echo "Running '$cmd'"
  eval "$cmd"
}

在本地機裡直接執行jllocal即可。

使用nginx反向代理

首先直接安裝nginx:

sudo apt install nginx

我們需要把本地訪問URL<YOUR DOMAIN NAME/IP>:<YOUR PORT>/jupyter時重定向到遠端伺服器訪問localhost:<YOUR_PORT>/jupyter

配置檔案在:/etc/nginx裡,可以在conf.d資料夾下新建jupyter.conf,寫入配置:

upstream notebook {
    server localhost:<YOUR PORT>;
}
server{
listen 80;
server_name <YOUR DOMAIN NAME/IP>;
location /jupyter {
        proxy_pass            http://notebook;
        proxy_set_header      Host $host;
}

location ~ /jupyter/api/kernels/ {
        proxy_pass            http://notebook;
        proxy_set_header      Host $host;
        # websocket support
        proxy_http_version    1.1;
        proxy_set_header      Upgrade "websocket";
        proxy_set_header      Connection "Upgrade";
        proxy_read_timeout    86400;
    }
location ~ /jupyter/terminals/ {
        proxy_pass            http://notebook;
        proxy_set_header      Host $host;
        # websocket support
        proxy_http_version    1.1;
        proxy_set_header      Upgrade "websocket";
        proxy_set_header      Connection "Upgrade";
        proxy_read_timeout    86400;
}
}

然後也需要修改jupyter的配置~/.jupyter/jupyter_notebook_config.py

#  Local IP addresses (such as 127.0.0.1 and ::1) are allowed as local, along
#  with hostnames configured in local_hostnames.
#  Default: False
c.NotebookApp.allow_remote_access = True

## DEPRECATED use base_url
#  Default: '/'
c.NotebookApp.base_project_url = '/jupyter/'

修改完配置之後重啟兩個服務:

sudo systemctl restart jupyter.service
sudo systemctl restart nginx.service

然後可以在本地瀏覽器裡輸入<YOUR DOMAIN NAME/IP>:<YOUR PORT>/jupyter訪問notebook server,第一次可能需要登入token。
如果把jupyter放到服務裡執行的話,token可以從以下log裡找到:

sudo systemctl status jupyter.service