如何在 Ubuntu 18.04 上设置 JupyterLab 环境

作者选择了 联合国基金会作为 写给捐赠计划的一部分接受捐赠。

介绍

JupyterLab是一个高度功能丰富的用户界面,使用户,特别是在数据科学和人工智能领域,可以轻松地完成他们的任务。JupyterLab环境为Jupyter Notebook(https://jupyter-notebook.readthedocs.io/en/stable/index.html)提供一个以生产力为重点的重新设计。它引入了内置HTML浏览器和CSV浏览器等工具,以及在同一屏幕上统一Jupyter Notebooks的几个分散功能的功能。

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

前提条件

为了完成这个教程,你需要:

使用此 初始服务器设置指南sudo特权的非根用户帐户的 Ubuntu 18.04 服务器。 在您的服务器上安装了 Python Anaconda Distribution. 您可以使用 How To Install the Anaconda Python Distribution on Ubuntu 18.04 教程。 注册的域名或子域名,您可以访问编辑 DNS 记录。 本教程将使用整个域名。 您可以在 Namecheap上购买域名,在 Freenom获得免费域名,或与您选择的任何公共注册商注册一个新域名。 以下 DNS 记录为您的域名设置: _M

如何创建,编辑和删除 DNS 记录的文档可以帮助您设置这些记录。

步骤1 - 设置您的密码

在此步骤中,您将在 JupyterLab 安装中设置密码,因为您的实例将可公开访问,所以您需要有一个密码。

首先,确保您的 Anaconda 环境被激活,根据前提教程,环境被称为base

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

1conda activate base

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

1sammy@your_server:~$

本教程中的所有未来的命令都将在基础环境中运行。

随着 Anaconda 环境的激活,您准备在您的服务器上为 JupyterLab 设置密码。

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

1jupyter notebook --generate-config

您将收到以下输出:

1[secondary_label Output]
2Writing default config to: /home/sammy/.jupyter/jupyter_notebook_config.py

JupyterLab 和 Jupyter 笔记本都共享相同的配置文件。

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

1jupyter notebook password

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

1[secondary_label Output]
2Enter password:
3Verify password:
4[NotebookPasswordApp] Wrote hashed password to /home/sammy/.jupyter/jupyter_notebook_config.json

Jupyter 将密码存储在 /home/sammy/.jupyter/jupyter_notebook_config.json 格式中。

最后,使用由上一个命令生成的文件上的cat命令查看哈希密码:

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

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

1[label /home/sammy/.jupyter/jupyter_notebook_config.json]
2{
3  "NotebookApp": {
4    "password": "sha1:your_hashed_password"
5  }
6}

将值复制到 JSON密码密钥中,并暂时存储。

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

步骤 2 - 配置 Let's Encrypt

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

首先,你会将Certbot安装到你的服务器上,开始通过将其存储库添加到 apt 源:

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

在执行命令时,您将被要求按ENTER,以完成添加PPA:

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

ENTER,继续添加PPA。

一旦命令完成执行,请使用apt update命令更新 apt 源:

1sudo apt update

接下来,您将安装Certbot:

1sudo apt install certbot

在您开始运行 Certbot 为您的实例生成证书之前,您将允许访问您的服务器的端口 :80 和端口 433,以便 Certbot 可以使用这些端口来验证您的域名。 端口 :80 对服务器的http请求进行检查,而端口 43 用于https请求。

首先,允许访问端口 :80:

1sudo ufw allow 80

您将获得以下输出:

1[secondary_label Output]
2Rule added
3Rule added (v6)

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

1sudo ufw allow 443
1[secondary_label Output]
2Rule added
3Rule added (v6)

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

1sudo certbot certonly --standalone

独立的旗帜指示certbot在 verfication 过程的持续时间内运行临时服务器。

它会提示你你的电子邮件:

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

输入工作电子邮件并按进入

接下来,它会要求您审查并同意Certbot和Let's Encrypt的服务条款。

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

它现在会提示您与电子边境基金会分享您的电子邮件。输入您的答案并按ENTER:

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

最后,您将被要求输入您的域名,输入您的域名,而无需任何协议规格:

 1[secondary_label Output]
 2Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
 3to cancel): your_domain
 4Obtaining a new certificate
 5Performing the following challenges:
 6http-01 challenge for your_domain
 7Waiting for verification...
 8Cleaning up challenges
 9
10IMPORTANT NOTES:
11 - Congratulations! Your certificate and chain have been saved at:
12   /etc/letsencrypt/live/your_domain/fullchain.pem
13   Your key file has been saved at:
14   /etc/letsencrypt/live/your_domain/privkey.pem
15   Your cert will expire on 2020-09-28. To obtain a new or tweaked
16   version of this certificate in the future, simply run certbot
17   again. To non-interactively renew *all* of your certificates, run
18   "certbot renew"
19 - Your account credentials have been saved in your Certbot
20   configuration directory at /etc/letsencrypt. You should make a
21   secure backup of this folder now. This configuration directory will
22   also contain certificates and private keys obtained by Certbot so
23   making regular backups of this folder is ideal.
24 - If you like Certbot, please consider supporting our work by:
25
26   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
27   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 证书。

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

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

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

1[label /home/sammy/.jupyter/jupyter_notebook_config.py]
2...
3## The full path to an SSL/TLS certificate file.
4c.NotebookApp.certfile = '/etc/letsencrypt/live/your_domain/fullchain.pem'
5...

接下来,找到c.NotebookApp.keyfile变量,并按下列方式设置:

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

c.NotebookApp.certfilec.NotebookApp.keyfile 是指 SSL 证书,当您试图使用https 协议远程访问您的服务器时,将其提供。

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

1[label /home/sammy/.jupyter/jupyter_notebook_config.py]
2...
3## The IP address the notebook server will listen on.
4c.NotebookApp.ip = '*'
5...

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

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

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

默认情况下,JupyterLab 试图在启动浏览器时自动启动浏览器会话. 由于我们没有远程服务器上的浏览器,因此需要关闭此功能以避免错误。

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

 1[label /home/sammy/.jupyter/jupyter_notebook_config.py]
 2...
 3## Hashed password to use for web authentication.
 4#
 5#  To generate, type in a python/IPython shell:
 6#
 7#    from notebook.auth import passwd; passwd()
 8#
 9#  The string should be of the form type:salt:hashed-password.
10c.NotebookApp.password = 'your_hashed_password'
11...

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

最后,继续浏览文件并更新c.NotebookApp.port的条目:

1[label /home/sammy/.jupyter/jupyter_notebook_config.py]
2...
3## The port the notebook server will listen on.
4c.NotebookApp.port = 9000
5...

c.NotebookApp.port 设置了一个固定端口来访问您的 JupyterLab 运行时间. 这样,您可以允许从 `ufw' 防火墙访问仅一个端口。

一旦完成,保存并退出文件。

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

1sudo ufw allow 9000

您将收到以下输出:

1[secondary_label Output]
2Rule added
3Rule added (v6)

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

步骤4 - 运行JupyterLab

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

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

1cd ~

现在,修改证书文件的访问权限,以允许JupyterLab访问它们。

1sudo chmod 750 -R /etc/letsencrypt
2sudo chown sammy:sammy -R /etc/letsencrypt

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

1jupyter lab

此命令接受多个配置参数. 但是,由于我们已经在配置文件中做过这些更改,我们不需要在这里明确提供它们。

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

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

JupyterLab interface after login

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

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

步骤 6 – 设置系统d 服务

在此步骤中,您将创建一个systemd服务,允许JupyterLab在终端窗口退出时也能继续运行。

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

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

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

 1[label /etc/systemd/system/jupyterlab.service]
 2[Unit]
 3Description=Jupyter Lab Server
 4
 5[Service]
 6User=sammy
 7Group=sammy
 8Type=simple
 9WorkingDirectory=/home/sammy/
10ExecStart=/home/sammy/anaconda3/bin/jupyter-lab --config=/home/sammy/.jupyter/jupyter_notebook_config.py
11StandardOutput=null
12Restart=always
13RestartSec=10
14
15[Install]
16WantedBy=multi-user.target

保存和退出编辑器,一旦完成。

该服务文件会自动在系统中注册为daemon,但默认情况下并不运行。

使用systemctl命令来启动服务:

1sudo systemctl start jupyterlab

这将启动JupyterLab服务器在背景中. 您可以使用以下命令检查服务器是否已启动:

1sudo systemctl status jupyterlab

您将收到以下输出:

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

Q来退出服务状态输出。

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

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

在此最后一步中,您将配置 Let's Encrypt 提供的 SSL 证书,以便在每 90 天过期时自动更新,然后重新启动服务器来加载新证书。

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

您需要编辑 /etc/letsencrypt/renewal/your_domain.conf 文件,并在配置文件的末尾添加一个 renew_hook

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

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

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

1[label /etc/letsencrypt/renewal/your_domain.conf]
2...
3renew_hook = systemctl reload jupyterlab

保存和退出文件。

最后,运行一个干燥的更新过程,以验证您的配置文件是否有效:

1sudo certbot renew --dry-run

如果命令运行没有任何错误,您的 Certbot 更新已成功设置,并且在证书接近期限时自动更新和重新启动您的服务器。

结论

在本文中,您在您的服务器上设置了JupyterLab环境,并使其远程可用,现在您可以从任何浏览器访问您的机器学习或数据科学项目,并确保所有交换都通过SSL加密进行。

Published At
Categories with 技术
comments powered by Disqus