ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

搭建 jupyter notebook 服务(基于 systemd 和 nginx)

2020-11-21 11:04:36  阅读:292  来源: 互联网

标签:systemd jupyter sudo lab nginx notebook proxy


写在开头

在自己的服务器上搭建 jupyter notebook 服务是很方便的,可以直接在网页上修改代码、连接终端。每次重启服务器都要重新打开 notebook,很麻烦,我们可以使用 systemd 来让它开机自启。如果不想每次访问都加上端口号,那么就要使用 nginx 进行端口转发,这样我们就可以通过子域名或者子目录来访问 notebook。

笔者记录了一下整个配置的步骤,过程中还是会出现一些小坑的。配置环境为 Ubuntu 20.04,Python 3.8.5。

安装和配置 Jupyter

安装 pip3

sudo apt install python3-pip

安装 jupyter,国内的服务器可以先添加镜像源,加快速度

sudo pip3 install jupyter --user

设置密码

jupyter notebook password

之后就可以手动打开 jupyter 了

jupyter notebook --no-browser --ip 0.0.0.0

使用 Systemd 管理 Jupyter

向 /etc/systemd/system/jupyter.service 中添加如下内容,注意将用户名 admin 改成自己的;如果刚才 pip3 安装时没有加 --user,那就还要更改 jupyter 的启动路径

[Unit]
Description=Jupyter
After=syslog.target network.target

[Service]
User=admin
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/admin/.local/bin"
WorkingDirectory=/home/admin/lab/
ExecStart=/home/admin/.local/bin/jupyter notebook --ip=0.0.0.0 --no-browser

Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

重新载入服务

sudo systemctl daemon-reload

启动服务

sudo systemctl start jupyter

查看状态

sudo systemctl status jupyter

设为开机自启

sudo systemctl enable jupyter

安装和配置 Nginx

安装 nginx

sudo apt install nginx

下一步就是配置 nginx 文件,我这里是通过子域名(lab)来访问 notebook,端口号使用默认的。因为 jupyter notebook 中有连接终端的功能,所以要使用下面的 nginx 配置文件,否则远程终端会用不了。

向 /etc/nginx/sites-available/lab 中写入,注意将 example.com 更换为你自己的域名

server {
        listen 80;

        server_name lab.example.com;

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

保存退出后创建软连接

sudo ln -s /etc/nginx/sites-available/lab /etc/nginx/sites-enabled/lab

重新载入 nginx 配置

sudo nginx -s reload

这样我们就能通过访问 lab.example.com 来进入自己的 notebook 了。

标签:systemd,jupyter,sudo,lab,nginx,notebook,proxy
来源: https://www.cnblogs.com/xkgeng/p/14014858.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有