Jupyterlab服务器安装
一、Jupyterlab简介
JupyterLab是一个交互式的开发环境,是Jupyter Notebook的下一代产品,某种意义上,它的出现是为了取代Jupyter Notebook。不过不用担心Jupyter Notebook会消失,JupyterLab包含了Jupyter Notebook所有功能。JupyterLab作为一种基于web的集成开发环境,可以使用它编写Notebook、操作终端、编辑MarkDown文本、打开交互模式、查看csv文件及图片等功能。
JupyterLab是开发者们下一阶段更主流的开发环境。JupyterLab支持更加灵活和更加强大的项目操作方式,具有和Jupyter Notebooks一样的组件。
Jupyterlab的github网站:https://github.com/jupyterlab/jupyterlab
Jupyterlab说明文档:https://jupyterlab.readthedocs.io/en/stable/
Jupyter lab和Jupyter notebook的区别
- Jupyter Notebook 是一个款以网页为基础的交互计算环境,可以创建Jupyter的文档,支持多种语言,包括Python, Julia, R等等。广泛用于数据分析,数据可视化和其他的交互和探索性计算中。
- JupyterLab 是包括了Notebook的下一代用户界面。有模块化的界面,可以在同一个窗口同时打开好几个notebook或文件(HTML, TXT, Markdown等等),都以标签的形式展示,于是就更像是一个IDE。
JupyterLab有以下特点:
- 交互模式:Python交互式模式可以直接输入代码,然后执行,并立刻得到结果,因此Python交互模式主要是为了调试Python代码用的
- 内核支持的文档:使你可以在可以在Jupyter内核中运行的任何文本文件(Markdown,Python,R等)中启用代码
- 模块化界面:可以在同一个窗口同时打开好几个notebook或文件(HTML, TXT, Markdown等等),都以标签的形式展示,更像是一个IDE
- 镜像notebook输出:让你可以轻易地创建仪表板
- 同一文档多视图:使你能够实时同步编辑文档并查看结果
- 支持多种数据格式:你可以查看并处理多种数据格式,也能进行丰富的可视化输出或者Markdown形式输出
- 云服务:使用Jupyter Lab连接Google Drive等服务,极大得提升生产力
优先支持的浏览器
- Firefox
- Chrome
- Safari
安装的目的是在服务器运行JupyterLab,然后在本地浏览器访问,操作
1.1 Jupyter lab安装
JupyterLab可以通过conda, mamba, pip, pipenv or docke安装。
安装anaconda
wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
bash Anaconda3-2021.05-Linux-x86_64.sh
anaconda安装在/home/yourname/anaconda3。安装过程一路yes即可。
安装mamba
Mamba是用C++重新实现的conda包管理器,支持多线程与并行下载,并且依赖解析速度大幅提升。在已有conda的情况下只需要一行命令安装
conda install -n base -c conda-forge mamba
然后就可以将原来命令里的conda直接替换成mamba。mamba的设计目标就是与conda兼容。
使用anaconda创建一个新的虚拟环境,并在其中安装nodejs和jupyterlab,强烈推荐通过mamba安装,速度快很多:
conda create -p ~/env/Jupyter
conda activate ~/env/Jupyter/
mamba install -c conda-forge nodejs
mamba install -c conda-forge jupyterlab
生成Jupyterlab的配置文件:
jupyter-lab --generate-config
Writing default config to: /home/yourName/.jupyter/jupyter_lab_config.py
进入Jupyter虚拟环境中python,生成Jupyterlab密码哈希值(以密码123456为例):
from jupyter_server.auth import passwd
passwd()
Enter password:
Verify password:
'argon2:$argon2id$v=19$m=10240,t=10,p=8$3GmksmJCRj/N/whpQE24lQ$8FmVHwfJYAUmtK5BHcQ4Cg'
生成的字符串便是经过哈希加密后的密码,将其复制并黏贴到Jupyterlab配置文件中,修改JupyterLab配置文件:
vim /home/yourName/.jupyter/jupyter_lab_config.py
# password
c.ServerApp.password = u'argon2:$argon2id$v=19$m=10240,t=10,p=8$bKDhVGXZbLQDpvzXV7NESQ$gBSXW9cxTMYMSLiL8jVOhA'
c.ServerApp.password_required = True
c.ServerApp.allow_root = True
# 若反向代理开启并且没有挂在根目录下,则需要修改这一项
c.ServerApp.allow_origin = '*'
# 反向代理路径,两者保持一致
c.ServerApp.base_url = '/jupyter/'
c.ServerApp.custom_display_url = '/jupyter/'
# ip与端口,需与nginx代理配置一致 ,'XXXX'需要替换成服务器ip
c.ServerApp.ip = 'XXXX'
c.ServerApp.port = 8788
# 默认路径
c.ServerApp.notebook_dir = u'/home/yourName/Jupyter'
# 远程访问
c.ServerApp.allow_remote_access = True
c.ServerApp.open_browser = False #不打开浏览器
安装扩展:
jupyter serverextension enable --py jupyterlab --user
# 安装一个生成目录的插件
jupyter labextension install @jupyterlab/toc
# 可以查看一下安装的插件
jupyter labextension list
安装好后在xshell输入命令jupyter lab打开测试一下。
jupyter lab
打开本地浏览器查看yourServer_IP:8788/jupter
image (2).png
yourServer_IP:8788/jupyter
输入上面设置的密码:123456
image.png
上面的方式,如果xshell断连,Jupyterlab启动不了,改成Jupyterlab开机自启模式。
进程守护开启Jupyterlab
使用傻狗先生的教程配置文件的时候在服务器上出现一些小问题,应该是我的配置文件设置不对,
image.png
尝试的是以下方法:
最终采用的方法是,下面的配置是重点
vim /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter notebook server
After=network.target
#StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=yourName
Group=yourName
ExecStart=/home/yourName/env/Jupyter/bin/jupyter lab
Environment="PATH=/home/yourName/anaconda3/bin:/home/yourName/anaconda3"
[Install]
WantedBy=multi-user.target
#----------------------------------
chmod a+x /etc/systemd/system/jupyter.service
更新配置
systemctl daemon-reload
# 服务立即启动 下次不启动
systemctl start jupyter.service
# 停止服务
systemctl stop jupyter.service
# 重启服务
systemctl restart jupyter.service
# 查看状态
systemctl status jupyter.service
# 将服务设置为每次开机启动
systemctl enable jupyter.service
image.png
image.png
跟之前的界面一样
参考:
1.https://medium.com/@desjoerdhaan/manage-your-jupyter-server-with-systemd-9914ff39049d
3.https://www.ishelo.com/archives/331
2.https://blog.mrdoge.cool/index.php/2021/06/08/tech/nginx_for_rstudio_jupyterlab
二、nginx代理
2.1 nginx简介
nginx网站:https://www.nginx.com/
Nginx 是一个高性能的网络服务器软件。 它是一个比 Apache HTTP Server 更加灵活和轻量级的程序。
nginx与apache,tomcat的区别
image.png
2.2 nginx安装
在CentOS 使用yum安装软件的时候,出现报错no more mirrors to try,导致软件无法继续安装,可能是网络问,放弃此方法。
failure: repodata/a3546ceff5c3fded9f99f918376a5e5922d554433032e539ee187de4219aef0d-primary.sqlite.bz2 from updates: [Errno 256] No more mirrors to try.
改用手动安装
# 下载 Nginx
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
# 进入安装包目录
cd nginx-1.8.0
# 编译安装
./configure --prefix=/home/yourName/software/nginx
make
make install
# 查看nginx版本
/home/yourName/software/nginx/sbin/nginx -v
# nginx version: nginx/1.8.0
打开nginx配置文件/etc/nginx/nginx.conf,修改http部分:
vim /home/yourName/software/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 8220;
server_name yourServer_IP;
charset koi8-r;
location / {
rewrite / yourServer_IP permanent;
}
# the jupyter path, note to keep port and path agree with your jupyterlab config
location /jupyter/ {
proxy_pass http://yourServer_IP:8790/jupyter/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
测试配置文件是否正确:
/home/yourName/software/nginx/sbin/nginx -s reopen
/home/yourName/software/nginx/sbin/nginx -s reload
/home/yourName/software/nginx/sbin/nginx -t
nginx: the configuration file /home/yourName/software/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/yourName/software/nginx/conf/nginx.conf test is successful
image.png
http://yourServer_IP:8220/jupyter
image.png
如有报错,查看nginx日志
位置/home/yourName/software/nginx/logs/error.log
安装过程会遇到一些小问题,跟自身服务器的相关配置有关系,多查询问题和查看日志报错。