本页内容

  • 准备工作
  • 开始之前
  • 安装 Python
  • 安装 Jupyter Notebook
  • 生成 Jupyter Notebook 密码
  • 为 Jupyter Notebook 创建 Systemd 服务文件
  • 将 Nginx 配置为 Jupyter Notebook 的反向代理
  • 访问 Jupyter Notebook
  • 总结

Jupyter Notebook 是一个免费、开源且基于 Web 的交互式计算平台,允许用户通过 Web 浏览器编辑和运行文档。它基于 Python,允许您创建和共享包含实时代码、公式以及可视化内容的文档。其目标是开发用于多种编程语言交互式计算的开源软件、开放标准和服务。

在本教程中,我将向您展示如何在 Ubuntu 22.04 上安装 Jupyter Notebook 软件。

准备工作

  • 一台运行 Ubuntu 22.04 的服务器。
  • 一个已指向您服务器 IP 的有效域名。
  • 服务器上已配置 root 密码。

开始之前

首先,建议将您所有的软件包更新并升级到最新版本。您可以通过运行以下命令来更新所有软件包:

apt update -y
apt upgrade -y

所有软件包更新完成后,您可以继续进行下一步:

安装 Python

首先,您需要在服务器上安装 Python 及其他依赖项。您可以通过运行以下命令来安装它们:

apt-get install python3 python3-pip -y

所有软件包安装完成后,您可以使用以下命令验证 Python 版本:

python3 --version

您应该会看到如下输出:

Python 3.10.6

接下来,使用以下命令将 PIP 更新到最新版本:

pip3 install --upgrade pip

接下来,使用以下命令验证 PIP 版本:

pip3 --version

您应该会看到如下输出:

pip 22.2.2 from /usr/local/lib/python3.10/dist-packages/pip (python 3.10)

接下来,使用以下命令安装 Python 虚拟环境包:

pip3 install virtualenv

完成后,您可以继续进行下一步。

安装 Jupyter Notebook

首先,使用以下命令创建一个用于存储 Notebook 的目录:

mkdir ~/project

接下来,使用以下命令进入项目目录并创建一个 Python 虚拟环境:

cd ~/project
virtualenv notebookenv

接下来,使用以下命令激活虚拟环境:

source notebookenv/bin/activate

接下来,使用以下命令安装 Jupyter Notebook:

pip install jupyter

Jupyter Notebook 安装完成后,使用以下命令运行 Jupyter Notebook:

jupyter notebook --allow-root

如果一切正常,您将得到如下输出:

[I 14:23:26.729 NotebookApp] Serving notebooks from local directory: /root/project
[I 14:23:26.729 NotebookApp] Jupyter Notebook 6.4.12 is running at:
[I 14:23:26.729 NotebookApp] http://localhost:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625
[I 14:23:26.729 NotebookApp]  or http://127.0.0.1:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625
[I 14:23:26.730 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 14:23:26.734 NotebookApp] No web browser found: could not locate runnable browser.
[C 14:23:26.734 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///root/.local/share/jupyter/runtime/nbserver-3214-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625
     or http://127.0.0.1:8888/?token=5dbdfdbf2e2dc72ccdaaa7361db8c55877ecfbc45676e625

按 CTRL+C 停止 Jupyter Notebook。

完成后,您可以继续进行下一步。

生成 Jupyter Notebook 密码

建议使用密码保护 Jupyter Notebook。首先,使用以下命令生成 Jupyter Notebook 配置文件:

jupyter notebook --generate-config

您应该会看到如下输出:

Writing default config to: /root/.jupyter/jupyter_notebook_config.py

接下来,使用以下命令设置 Jupyter Notebook 密码:

jupyter notebook password

如下所示设置密码:

Enter password: 
Verify password: 
[NotebookPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_notebook_config.json

接下来,使用以下命令退出 Python 虚拟环境:

deactivate

完成后,您可以继续进行下一步。

为 Jupyter Notebook 创建 Systemd 服务文件

接下来,您需要创建一个 systemd 服务文件来管理 Jupyter Notebook。您可以使用以下命令创建它:

nano /etc/systemd/system/jupyter.service

添加以下代码:

[Unit]
Description=Jupyter Notebook
 
[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/root/project/notebookenv/bin/jupyter-notebook --config=/root/.jupyter/jupyter_notebook_config.py --allow-root
User=root
Group=root
WorkingDirectory=/root/project/notebookenv
Restart=always
RestartSec=10
 
[Install]
WantedBy=multi-user.target

保存并关闭文件,然后使用以下命令重新加载 systemd 守护进程:

systemctl daemon-reload

接下来,使用以下命令启动 Jupyter Notebook 并使其在系统重启时自动启动:

systemctl start jupyter
systemctl enable jupyter

要验证 Jupyter Notebook 的状态,请运行以下命令:

systemctl status jupyter

您应该会看到如下输出:

? jupyter.service - Jupyter Notebook
     Loaded: loaded (/etc/systemd/system/jupyter.service; disabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-10-11 14:27:01 UTC; 4s ago
   Main PID: 3649 (jupyter-noteboo)
      Tasks: 1 (limit: 2242)
     Memory: 53.4M
        CPU: 980ms
     CGroup: /system.slice/jupyter.service
             ??3649 /root/project/notebookenv/bin/python /root/project/notebookenv/bin/jupyter-notebook --config=/root/.jupyter/jupyter_noteb>

Oct 11 14:27:01 ubuntu2204 systemd[1]: Started Jupyter Notebook.
Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [I 14:27:02.288 NotebookApp] Serving notebooks from local directory: /root/project/noteboo>
Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [I 14:27:02.289 NotebookApp] Jupyter Notebook 6.4.12 is running at:
Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [I 14:27:02.289 NotebookApp] http://localhost:8888/
Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [I 14:27:02.290 NotebookApp] Use Control-C to stop this server and shut down all kernels (>
Oct 11 14:27:02 ubuntu2204 jupyter-notebook[3649]: [W 14:27:02.293 NotebookApp] No web browser found: could not locate runnable browser.

完成后,您可以继续进行下一步。

将 Nginx 配置为 Jupyter Notebook 的反向代理

此时,Jupyter Notebook 已启动并运行在 8888 端口上。现在,您需要将 Nginx 配置为反向代理,以便通过 80 端口访问 Jupyter Notebook。

首先,使用以下命令安装 Nginx Web 服务器软件包:

apt-get install nginx -y

Nginx 安装完成后,使用以下命令创建一个 Nginx 虚拟主机配置文件:

nano /etc/nginx/conf.d/jupyter.conf

添加以下配置:

upstream notebook {
server 127.0.0.1:8888;
}
 
server {
listen 80;
server_name jupyter.example.com;
 
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
 
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;
     }
}

保存并关闭文件,然后使用以下命令检查 Nginx 是否存在语法错误:

nginx -t

您应该会看到如下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

接下来,重启 Nginx 服务以应用更改:

systemctl restart nginx

您也可以使用以下命令检查 Nginx 状态:

systemctl status nginx

您将得到如下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-10-11 14:28:53 UTC; 54s ago
       Docs: man:nginx(8)
    Process: 4193 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 4194 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 4195 (nginx)
      Tasks: 2 (limit: 2242)
     Memory: 2.6M
        CPU: 40ms
     CGroup: /system.slice/nginx.service
             ??4195 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??4196 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Oct 11 14:28:53 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 11 14:28:53 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

接下来,使用以下命令将 root 用户添加到 www-data 组:

usermod -g www-data root

接下来,编辑 Jupyter Notebook 配置文件并启用远程访问:

nano /root/.jupyter/jupyter_notebook_config.py

修改以下行:

c.NotebookApp.allow_remote_access = True

保存并关闭文件,然后重启 Jupyter Notebook 服务以应用更改:

systemctl restart jupyter

完成后,您可以继续进行下一步。

访问 Jupyter Notebook

现在,打开您的 Web 浏览器并使用 URL http://jupyter.example.com 访问 Jupyter Notebook。在以下屏幕上,系统会要求您输入 Jupyter Notebook 密码:

在 Ubuntu 系统上从零安装配置 Jupyter 交互式笔记本

输入您的密码并点击 Login 按钮。在以下屏幕上,您应该会看到 Jupyter Notebook 仪表盘:

在 Ubuntu 系统上从零安装配置 Jupyter 交互式笔记本

总结

恭喜!您已成功在 Ubuntu 22.04 上使用 Nginx 作为反向代理安装了 Jupyter Notebook。现在您可以通过互联网创建、共享并运行您的 Notebook。如果您有任何问题,欢迎随时向我提问。