0%

jupyter 安装教程

jupyter 就是曾经的 ipython ,现如今支持很多种流行的编程语言。本文将主要介绍 jupyter 在 debian9 上的安装以及一些,为了我们更方便,更安全的使用,做的设置。

这篇教程 更适合 有 vps 的用户,因为如果我们把 jupyter 搭建在 云端上,用 nginx 开放网络。这样的话,我们在任何平台,只要有网络,支持现代的浏览器 都可以使用。包括 手机 电脑 平板。

安装 jupyter

更新软件源

sudo apt update

安装python3 个 pip3

sudo apt install python3 python3-pip

新建一个用户来运行 jupyter

sudo adduser jupyter

按提示设置好 密码详细信息后,让密码失效 :

sudo passwd -l jupyter

切换到 jupyter 用户

sudo su - jupyter

安装 jupyter

pip3 install jupyter

设置变量环境

vi ~/.profile

在最后新建一行 添加

export PATH="$PATH:$HOME/.local/bin"

保存退出后 刷新变量变量环境

source ~/.profile

到这里 jupyter 就安装完成了,接下来可以测试一下 jupyter 是否安装成功

jupyter notebook

如果出现类似这样的消息 就安装成功了

jupyter@Tokyo:~$ jupyter notebook
[I 19:57:23.229 NotebookApp] Serving notebooks from local directory: /home/jupyter
[I 19:57:23.230 NotebookApp] 0 active kernels
[I 19:57:23.230 NotebookApp] The Jupyter Notebook is running at:
[I 19:57:23.231 NotebookApp] http://localhost:8888/?token=8afb2489dbd872fd7d59cvbdc60bakkce610b1d2c5ecb
[I 19:57:23.231 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 19:57:23.235 NotebookApp] No web browser found: could not locate runnable browser.
[C 19:57:23.237 NotebookApp]

    Copy/paste this URL into your browser when you connect for the first time, to login with a token:
            http://localhost:8888/?token=8afb2489dbd872fd7d59cv640bdc60bakkce610b1d2c5ecb 

但是现在,我们还不能使用。因为 jupyter 默认监听的 127.0.0.1
接下来 按 CTRL + c 键结束 jupyter,再按 CTRL + d 键 退出 jupyter 用户

开放 jupyter 到公网

安装 nginx

apt install nginx

安装好够就需要在 nginx 设置 jupyter的 websocket了,设置方法分 需要域名不需要域名 两种操作方式,我主要讲
需要域名 的操作方式, 不需要域名 的操作方式我也讲解一下。

先讲解 不需要 域名的方式

在 /etc/nginx/conf.d/ 目录新建 jupyter.conf 文件,并添加以下内容:

server {
    listen 3000;

    access_log /var/log/nginx/jupyter.access.log;
    error_log /var/log/nginx/jupyter.error.log;

    charset utf-8;

    location / {
        proxy_hide_header    X-Powered-By;

        proxy_redirect off;
        proxy_pass http://localhost:8888; 
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
    }
}

刷新 nginx 配置

sudo nginx -s reload

然后我们 sudo - su jupyter 切换到 刚才新建的 jupyter 用户 运行 jupyter notebook 。然后终端会打印一大段信息,找到 类似这样的网址 http://localhost:8888/?token=8afb2489dbd872fd7d59cv640bdc60bakkce610b1d2c5ecb 复制进浏览器,并且改成你的 ip:3000 类似于这样 http//:3000/?token=8afb2489dbd872fd7d59cv640bdc60bakkce610b1d2c5ecb 就可以看看到实际运行效果了。
看完实际效果后,回到终端,按 CTRL + c 键结束进程,按 CTRL + d 键 退出 jupyter 用户。如果你不打算使用域名的话,可以跳过,接下来介绍域名这一段。

需要域名的方式

使用域名有诸多好处,比如不浪费标准端口,绿锁 SSL等。我个人更建议使用域名。
如果没有域名的话建议去注册一个,或者问朋友要一个 二级 或者 三级 域名。当前网上也有免费的域名注册商,不过我个人强烈不建议使用免费域名。
以下的列子 我会使用 jupyter.niconiconi.fun 这个 三级域名,请自行替换为你的二级或三级域名。

在 /etc/nginx/conf.d/ 目录新建 jupyter.conf 文件,并添加以下内容:

server {
    listen 80;
    server_name jupyter.niconiconi.fun;
    #上面这个域名,替换为你的域名

    access_log /var/log/nginx/jupyter.access.log;
    error_log /var/log/nginx/jupyter.error.log;

    charset utf-8;

    location / {
        proxy_hide_header X-Powered-By;

        proxy_redirect off;
        proxy_pass http://localhost:8888;
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
    }
}

刷新 nginx 配置

sudo nginx -s reload

然后我们 sudo - su jupyter 切换到 刚才新建的 jupyter 用户 运行 jupyter notebook 。然后
终端会打印一大段信息,找到 类似这样的网址 http://localhost:8888/?token=8afb2489dbd872fd7d59cv640bdc60bakkce610b1d2c5ecb 复制进浏览器,并且改成 你的 网址 类似于这样:http://jupyter.niconiconi.fun/?token=8afb2489dbd872fd7d59cv640bdc60bakkce610b1d2c5ecb 就可以看看到实际运行效果了。
看完实际效果后,回到终端,按 CTRL + c 键结束进程,按 CTRL + d 键 退出 jupyter用户。

因为使用 http 在公网传输,信息有被人截获的风险,所以我建议设置一下 ssl。

使用 Let’s Encrypt 设置ssl

首先需要把 域名解析到 本台 vps 的 ip 上,这一步非常重要,解析方法我就不多概括了。

安装 certbot

sudo apt install certbot

将 /etc/nginx/conf.d/jupyter.conf 的信息改为

server {
    listen 80;
    server_name jupyter.niconiconi.fun;
    #上面这个域名,替换为你的域名

    access_log /var/log/nginx/jupyter.access.log;
    error_log /var/log/nginx/jupyter.error.log;

    charset utf-8;
    root /var/www/jupyter;
}

新建申请证书需要的目录

sudo mkdir /var/www/jupyter

重启 nginx

sudo systemctl restart nginx

运行 certbot 申请证书

sudo certbot certonly

以下是设置时需要填的信息,我会把重要的地方标出来

root@219c9645d534:/# certbot certonly
Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate with the ACME CA?
-------------------------------------------------------------------------------
1: Place files in webroot directory (webroot)
2: Spin up a temporary webserver (standalone)
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
#选择第一种验证方式
Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel):[[email protected]](https://niconiconi.fun/cdn-cgi/l/email-protection)
#这里填你的邮箱
-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. 
You mustagree in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A
#是否同意用户条款,选 A 同意
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c' to cancel):jupyter.niconiconi.fun
# 这里输入你的域名
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for jupyter.niconiconi.fun

Select the webroot for jupyter.niconiconi.fun:
-------------------------------------------------------------------------------
1: Enter a new webroot
-------------------------------------------------------------------------------
Press 1 [enter] to confirm the selection (press 'c' to cancel): 1
# 选 1
Input the webroot for test.com: (Enter 'c' to cancel):/var/www/jupyter
#这里是验证时需要的网站目录
#到这里需要输入的就全部完成了,接下来回车执行

不出意外证书就申请好了

接下来 配置 nginx 以支持 ssl

将 /etc/nginx/conf.d/jupyter.conf 修改为

server {
    listen 80;
    server_name jupyter.niconiconi.fun;
    #上面这个域名,替换为你的域名

    rewrite / https://$host$uri permanent;
    # http 重定向 https
    access_log /var/log/nginx/jupyter.access.log;
    error_log /var/log/nginx/jupyter.error.log;

    charset utf-8;

    root /var/www/jupyter;
}

server {
    listen 443;
    server_name jupyter.niconiconi.fun;
    #上面这个域名,替换为你的域名

    access_log /var/log/nginx/jupyter.access.log;
    error_log /var/log/nginx/jupyter.error.log;

    charset utf-8;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/jupyter.niconiconi.fun/fullchain.pem;
    #替换为你的域名
    ssl_certificate_key /etc/letsencrypt/live/jupyter.niconi.fun/privkey.pem;
    #替换为你的域名
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_protocols TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES256;

    location / {
        proxy_hide_header X-Powered-By;

        proxy_redirect off;
        proxy_pass http://localhost:8888;
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
    }
}

重启 nginx

systemctl restart nginx

现在 web 相关的就设置完了

jupyter 设置

为了更方便的使用 jupyter,我们还需要对 jupyter进行一些设置

切换到 jupyter 用户

sudo su - jupyter

生成配置文件

jupyter notebook --generate-config

然后会在 ~/.jupyter 生成 jupyter_notebook_config.py 配置文件

设置密码

为防止未经授权,访问我们的 jupyter 我们需要设置一个密码。

在终端运行
python3 -c "import IPython;print(IPython.lib.passwd())"

然后输入你的密码,把获得的 sha1 保存一下。

编辑~/.jupyter/jupyter_notebook_config.py 找到 #c.NotebookApp.password = ‘’ 这一行,把刚才保存的 sha1 码 添加到’ ‘这两个中间并取消前面的注释类似于这样

c.NotebookApp.password = ‘sha1:39144af8e5f7:36eb385b99b8a08797f85a12aa22ce58208766e7

一些基本设置

c.NotebookApp.port = 8888 监听端口

c.NotebookApp.base_url = ‘/‘ 默认访问路径

c.NotebookApp.open_browser = True 建议改成 False 因为vps上一般是没有浏览器的

c.NotebookApp.ip = ‘localhost’ 绑定的 ip 地址

设置自启动

新建一个工作目录

mkdir ~/code

CTRL + d 键退出 jupyter 用户

新建 /etc/systemd/system/jupyter.service 并写入以下内容

[Unit]
Description=Jupyter Notebook

[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/home/jupyter/.local/bin/jupyter-notebook --config=/home/jupyter/.jupyter/jupyter_notebook_config.py
WorkingDirectory=/home/jupyter/code/
Restart=always
RestartSec=10
#KillMode=mixed
User=jupyter
Group=jupyter

[Install]
WantedBy=multi-user.target

开机自启动 jupyter

sudo systemctl enable jupyter.service

启动 jupyter

sudo systemctl start jupyter.service

到这里 jupyter 的安装就全部完成了,现在你可以在 浏览器 输入你的 ip 或者 你的 网址 访问了。

———最后祝你玩的开心 (*^_^*)喵