如何在 Ubuntu 18.04 上设置 JupyterLab 环境

作者选择联合国基金会接受捐赠,作为Write for DOnations计划的一部分。

介绍

JupyterLab是一个功能丰富的用户界面,它使用户(尤其是数据科学和人工智能领域的用户)能够轻松地执行任务。JupyterLab 环境提供了以生产力为中心的Jupyter Notebook重新设计它引入了诸如内置 HTML 查看器和 CSV 查看器等工具,以及将 Jupyter Notebooks 的几个独立功能统一到同一屏幕上的功能。

在本教程中,您将在 Ubuntu 18.04 服务器上安装和设置 JupyterLab。您还将配置您的服务器,以便能够使用域名安全地从任何 Web 浏览器远程连接到 JupyterLab 实例。

先决条件

为了完成本教程,您需要:

  • 一个 Ubuntu 18.04 服务器,具有sudo使用此初始服务器设置指南的特权的非 root 用户帐户
  • 在您的服务器上安装 Python Anaconda Distribution。您可以使用如何在 Ubuntu 18.04 上安装 Anaconda Python 发行版教程。
  • 您有权编辑 DNS 记录的注册域名或子域。本教程将your_domain贯穿始终。您可以在购买域Namecheap,在得到一个免费域名Freenom,或与您选择的任何注册商注册一个新域名。
  • 为您的域设置的以下 DNS 记录:

    • your_domain指向您服务器的公共 IP 地址的 A 记录
    • 指向您服务器的公共 IP 地址的 A 记录如何创建、编辑和删除 DNS 记录文档可以帮助您设置这些记录。www.your_domain

第 1 步 – 设置您的密码

在此步骤中,您将在 JupyterLab 安装上设置密码。设置密码很重要,因为您的实例将可以公开访问。

首先,确保您的 Anaconda 环境已激活。根据先决条件教程,环境被称为base.

要激活环境,请使用以下命令:

  • conda activate base

您的提示将在终端中更改以反映默认的 Anaconda 环境base

  • sammy@your_server:~$

本教程中的所有未来命令都将在该base环境中运行

激活 Anaconda 环境后,您就可以在服务器上为 JupyterLab 设置密码了。

首先,让我们为 Jupyter 生成一个配置文件:

  • jupyter notebook --generate-config

您将收到以下输出:

Output
Writing default config to: /home/sammy/.jupyter/jupyter_notebook_config.py

JupyterLab 和 Jupyter Notebook 共享相同的配置文件。

现在,使用以下命令设置远程访问 JupyterLab 实例的密码:

  • jupyter notebook password

Jupyter 将提示您提供您选择的密码:

Output
Enter password: Verify password: [NotebookPasswordApp] Wrote hashed password to /home/sammy/.jupyter/jupyter_notebook_config.json

Jupyter 将密码以散列格式存储在. 将来您将需要此散列值。/home/sammy/.jupyter/jupyter_notebook_config.json

最后,在cat上一条命令生成的文件上使用命令查看散列后的密码:

  • cat /home/sammy/.jupyter/jupyter_notebook_config.json

您将收到类似于以下内容的输出:

/home/sammy/.jupyter/jupyter_notebook_config.json
{
  "NotebookApp": {
    "password": "sha1:your_hashed_password"
  }
}

复制JSONpassword键中的值并临时存储。

您已经为 JupyterLab 实例设置了密码。在下一步中,您将为服务器创建 Let’s Encrypt 证书。

第 2 步 – 配置 Let’s Encrypt

在此步骤中,您将为您的域创建一个 Let’s Encrypt 证书。当您从浏览器访问 JupyterLab 环境时,这将保护您的数据。

首先,您将 Certbot 安装到您的服务器。首先将其存储库添加到 apt 源:

  • sudo add-apt-repository ppa:certbot/certbot

在执行命令时,系统会要求您按下ENTER以完成添加 PPA:

Output
This is the PPA for packages prepared by Debian Let's Encrypt Team and backported for Ubuntu. Note: Packages are only provided for currently supported Ubuntu releases. More info: https://launchpad.net/~certbot/+archive/ubuntu/certbot Press [ENTER] to continue or Ctrl-c to cancel adding it.

ENTER继续添加 PPA。

命令执行完毕后,使用以下apt update命令刷新 apt 源

  • sudo apt update

接下来,您将安装 Certbot:

  • sudo apt install certbot

在开始运行 Certbot 为您的实例生成证书之前,您需要允许访问您服务器的端口:80和端口:443,以便 Certbot 可以使用这些端口来验证您的域名。端口:80用于检查对http服务器的请求,而端口:443用于https请求。Certbot 将首先发出http请求,然后在获得您的服务器的证书后,将发出https请求,该请求将通过端口代理到:443侦听:80端口的进程这将验证您的证书安装是否成功。

首先,允许访问端口:80

  • sudo ufw allow 80

您将收到以下输出:

Output
Rule added Rule added (v6)

接下来,允许访问端口:443

  • sudo ufw allow 443
Output
Rule added Rule added (v6)

最后,使用以下命令运行 C​​ertbot 为您的实例生成证书:

  • sudo certbot certonly --standalone

standalone标志指示certbot在验证过程期间运行临时服务器。

它会提示您输入电子邮件:

Output
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator standalone, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): your_email

输入工作电子邮件并按ENTER

接下来,它会要求您查看并同意 Certbot 和 Let’s Encrypt 的服务条款。查看条款,A如果您接受,请键入,然后按ENTER

Output
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A

它现在会提示您与电子前沿基金会共享您的电子邮件。输入您的答案并按ENTER

Output
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y/N

最后,系统会要求您输入域名。在没有任何协议规范的情况下输入您的域名:

Output
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c' to cancel): your_domain Obtaining a new certificate Performing the following challenges: http-01 challenge for your_domain Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/your_domain/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/your_domain/privkey.pem Your cert will expire on 2020-09-28. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

Certbot 将执行域验证并为您的域生成证书和密钥并将它们存储在./etc/letsencrypt/live/your_domain

现在您已经设置了 Let’s Encrypt 证书,您将更新 JupyterLab 配置文件。

第 3 步 – 配置 JupyterLab

在此步骤中,您将编辑 JupyterLab 配置以确保它使用您在步骤 2 中生成的 Let’s Encrypt 证书。您还将使用您在步骤 1 中设置的密码使其可访问。

首先,您需要在以下位置编辑 JupyterLab 配置文件/home/sammy/.jupyter/jupyter_notebook_config.py

  • nano /home/sammy/.jupyter/jupyter_notebook_config.py

现在,导航到定义值的行c.NotebookApp.certfile并按如下方式更新它:

/home/sammy/.jupyter/jupyter_notebook_config.py
...
## The full path to an SSL/TLS certificate file.
c.NotebookApp.certfile = '/etc/letsencrypt/live/your_domain/fullchain.pem'
...

接下来,找到c.NotebookApp.keyfile变量并将其设置为如下所示:

/home/sammy/.jupyter/jupyter_notebook_config.py
...
## The full path to a private key file for usage with SSL/TLS.
c.NotebookApp.keyfile = '/etc/letsencrypt/live/your_domain/privkey.pem'
...

c.NotebookApp.certfilec.NotebookApp.keyfile参考 SSL 证书,当您尝试使用该https协议远程访问您的服务器时将提供该证书

接下来,导航到定义c.NotebookApp.ip变量的行并按如下方式更新:

/home/sammy/.jupyter/jupyter_notebook_config.py
...
## The IP address the notebook server will listen on.
c.NotebookApp.ip = '*'
...

c.NotebookApp.ip定义可以访问运行服务器的 JupyterLab 的 IP。您将其设置为*通配符以允许从您需要访问 JupyterLab 的任何计算机进行访问。

接下来,找到c.NotebookApp.open_browser配置并更新如下:

/home/sammy/.jupyter/jupyter_notebook_config.py
...
## Whether to open in a browser after starting. The specific browser used is
#  platform dependent and determined by the python standard library `webbrowser`
#  module, unless it is overridden using the --browser (NotebookApp.browser)
#  configuration option.
c.NotebookApp.open_browser = False
...

默认情况下,JupyterLab 在开始运行时会尝试自动启动浏览器会话。由于我们在远程服务器上没有浏览器,因此有必要将其关闭以避免错误。

接下来,向下导航到c.NotebookApp.password变量并更改为以下内容:

/home/sammy/.jupyter/jupyter_notebook_config.py
...
## Hashed password to use for web authentication.
#
#  To generate, type in a python/IPython shell:
#
#    from notebook.auth import passwd; passwd()
#
#  The string should be of the form type:salt:hashed-password.
c.NotebookApp.password = 'your_hashed_password'
...

JupyterLab 将使用此散列密码配置来检查您在浏览器中输入的访问密码。

最后,进一步浏览文件并更新以下条目c.NotebookApp.port

/home/sammy/.jupyter/jupyter_notebook_config.py
...
## The port the notebook server will listen on.
c.NotebookApp.port = 9000
...

c.NotebookApp.port设置用于访问 JupyterLab 运行时的固定端口。这样,您可以只允许从ufw防火墙访问一个端口

完成后,保存并退出文件。

最后,允许9000端口上的流量

  • sudo ufw allow 9000

您将收到以下输出:

Output
Rule added Rule added (v6)

现在您已经设置了所有配置,您将运行 JupyterLab。

第 4 步 – 运行 JupyterLab

在此步骤中,您将对 JupyterLab 实例执行测试运行。

首先,将您当前的工作目录更改为用户的主目录:

  • cd ~

现在,修改证书文件的访问权限以允许 JupyterLab 访问它们。/etc/letsencrypt文件夹的权限更改为以下内容:

  • sudo chmod 750 -R /etc/letsencrypt
  • sudo chown sammy:sammy -R /etc/letsencrypt

然后,调用您的 JupyterLab 实例以开始使用以下命令:

  • jupyter lab

此命令接受多个配置参数。但是,由于我们已经在配置文件中进行了这些更改,因此我们无需在此处明确提供它们。您可以将它们作为此命令的参数提供以覆盖配置文件中的值。

您现在可以导航到检查您是否收到 JupyterLab 的登录屏幕。https://your_domain:9000

如果您使用在步骤 2 中为 JupyterLab 设置的密码登录,您将看到 JupyterLab 界面。

登录后的 JupyterLab 界面

最后,按CTRL+C两次停止 JupyterLab 服务器。

在下一步中,您将设置一个系统服务,以便 JupyterLab 服务器可以在后台连续运行。

步骤 6 — 设置 systemd 服务

在这一步中,您将创建一个systemd服务,即使退出终端窗口,JupyterLab 也能继续运行。您可以systemd本指南中了解有关 systemd 要点的更多服务和单元的信息

首先,您必须.service使用以下命令创建一个文件:

  • sudo nano /etc/systemd/system/jupyterlab.service

将以下内容添加到/etc/systemd/system/jupyterlab.service文件中:

/etc/systemd/system/jupyterlab.service
[Unit]
Description=Jupyter Lab Server

[Service]
User=sammy
Group=sammy
Type=simple
WorkingDirectory=/home/sammy/
ExecStart=/home/sammy/anaconda3/bin/jupyter-lab --config=/home/sammy/.jupyter/jupyter_notebook_config.py
StandardOutput=null
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

完成后保存并退出编辑器。

服务文件在系统中自动注册为守护进程。但是,它默认不运行。

使用systemctl命令启动服务:

  • sudo systemctl start jupyterlab

这会在后台启动 JupyterLab 服务器。您可以使用以下命令检查服务器是否已启动:

  • sudo systemctl status jupyterlab

您将收到以下输出:

Output
● jupyterlab.service - Jupyter Lab Server Loaded: loaded (/etc/systemd/system/jupyterlab.service; disabled; vendor preset: enabled) Active: active (running) since Sun 2020-04-26 20:58:29 UTC; 5s ago Main PID: 5654 (jupyter-lab) Tasks: 1 (limit: 1152) CGroup: /system.slice/jupyterlab.service └─5654 /home/sammy/anaconda3/bin/python3.7 /home/sammy/anaconda3/bin/jupyter-lab --config=/home/

Q退出服务状态输出。

您现在可以使用您选择的任何浏览器,提供您在第 2 步中设置的密码,然后访问在您的服务器上运行的 JupyterLab 环境。https://your_domain:9000

第 7 步 – 配置更新您的 Let’s Encrypt 证书

在这最后一步中,您将配置 Let’s Encrypt 提供的 SSL 证书以在它们每 90 天到期时自动续订,然后重新启动服务器以加载新证书。

虽然 Certbot 会为您的安装更新证书,但它不会自动重新启动服务器。要将服务器配置为使用新证书重新启动,您必须renew_hook为服务器的 Certbot 配置提供一个

您需要编辑该文件并将 a 添加到配置文件的末尾。/etc/letsencrypt/renewal/your_domain.confrenew_hook

首先,使用以下命令在编辑器中打开文件:/etc/letsencrypt/renewal/your_domain.conf

  • sudo nano /etc/letsencrypt/renewal/your_domain.conf

然后,在此文件的底部,添加以下行:

/etc/letsencrypt/renewal/your_domain.conf
...
renew_hook = systemctl reload jupyterlab

保存并退出文件。

最后,运行续订过程的试运行以验证您的配置文件是否有效:

  • sudo certbot renew --dry-run

如果该命令运行没有任何错误,则您的 Certbot 续订已成功设置,并将在证书临近到期日期时自动续订并重新启动您的服务器。

结论

在本文中,您在服务器上设置了 JupyterLab 环境并使其可远程访问。现在,您可以从任何浏览器访问您的机器学习或数据科学项目,并确保所有交换都在使用 SSL 加密的情况下进行。除此之外,您的环境具有基于云的服务器的所有优势。

觉得文章有用?

点个广告表达一下你的爱意吧 !😁