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
posted @ 2020-12-02 13:29  milliele  阅读(248)  评论(0编辑  收藏  举报