jupyter notebook server

[TOC]

如何安装一台jupyter notebook server,可以远程访问使用

需求

  • 远程访问
  • 必须使用https
  • notebook 用来学习测试chatgpt,所以需要在外网

环境准备

  • 一台Google Cloud VM (ubuntu server 18.4)
    • VPC network 需要增加防火墙rule,允许访问8443 端口(此端口为jupyter server绑定的端口)
  • 域名和DNS 设置:xxx.excelsre.com
  • HTTPS 证书
    • 使用certbot生成证书

搭建jupyter server

远程SSH 连接GCP VM

1
ssh -i /Users/<username>/.ssh/id_rsa <your-user-name-for-ssh>@<public-ip or hostname>

安装jupyter notebook

  1. Ubuntu 安装jupyter,有2种方法

    • 直接用apt 安装

      1
      sudo apt-get install jupyter notebook
    • 先安装pip3管理器,再给予python3进行jupyter的安装

      1
      2
      3
      4
      5
      6
      apt-get install -y python3-pip

      # 升级到最新版本
      pip3 install --upgrade pip

      pip3 install jupyter
  2. 生成HTTPS 证书

    • 使用certbot 生成证书

      1
      sudo certbot certonly --standalone

      证书默认生成在 /etc/letsencrypt/live/<YOUR.DOMAIN.COM/> 目录下,这个证书90天后就过期了,所以,需要使用一个 cron job 来定期更新, 使用命令 crontab -e 来编辑定时任务

      1
      2
      3
      0 0 1 * * /usr/bin/certbot renew --force-renewal

      # 还需要添加一个任务来重启jupyter notebook service,这里有一个脚本可供参考https://github.com/libuliduobuqiuqiu/Jupyter_Control/blob/master/jupyter.sh
    • 生成的证书,默认owner是root。如果后续用当前user 启动jupyter server,需要修改owner 权限,不然会报错

      1
      2
      sudo chown <user>:<user> cert.pem
      sudo chown <user>:<user> privkey.pem
  3. 配置文件

    • 创建配置文件

      1
      jupyter notebook --generate-config
    • 创建密码

      1
      jupyter notebook password
    • 修改配置文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      vi ~/.jupyter/jupyter_notebook_config.py

      # 修改的参数如下:

      # 绑定地址和端口
      c.NotebookApp.ip = '0.0.0.0'
      c.NotebookApp.port = 8443

      # 允许远程访问
      c.NotebookApp.allow_remote_access = True

      # HTTPS 证书
      c.NotebookApp.certfile = '/etc/letsencrypt/live/<your-domain-name>/cert.pem'
      c.NotebookApp.keyfile = '/etc/letsencrypt/live/<your-domain-name>/privkey.pem'

      # 启动notbook,不打开浏览器
      c.NotebookApp.open_browser = False
    • 查看配置

      1
      grep -v '^#' ~/.jupyter/jupyter_notebook_config.py | sed '/^\s*$/d'

启动jupyter notebook server

  1. 新建工作目录(这个目录就是后续notebook 打开之后的默认目录)

    1
    2
    mkdir jupyter_workspace
    cd jupyter_workspace/
  2. 后台启动jupyter,一定要用root 启动,因为生成的证书owner 也是root 权限。如果不用root 启动,需要先修改证书owner 为当前user

    1
    sudo nohup jupyter notebook --allow-root > ../log 2>&1 &

    除了后台启动,还有其他启动方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # 普通运行
    jupyter notebook

    # 无后台窗口运行, >后面的路径是我的日志路径,可自行做匹配修改
    nohup jupyter notebook >/usr/local/JupyterNotebook/jupyterLog/jupyter.log 2>&1 &

    # 使用root权限运行
    jupyter notebook ---allow-root

    # 指定参数运行
    nohup jupyter notebook --ip=0.0.0.0 --port=8080 --no-browser --allow-root >~/usr/local/JupyterNotebook/jupyterLog/jupyter.log 2>&1 &

    # 使用无后台运行时,关闭服务可以使用
    ps -ef |grep jupyter 查看进程

    kill -9 "进程号" 杀死进程

访问jupyter notebook server

https://domain-name:8443

image-20230609163901711

jupyter notebook server
https://blog.excelsre.com/2023/06/09/jupyter-notebook-on-cloud/
作者
Felix Yang
发布于
2023年6月9日
许可协议