基于Pip搭建公网访问的JupyterLab(可选CDN加速)
何为Jupyter?
The Jupyter Notebook
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.
Jupyter笔记本是一个开源的web应用程序,允许您创建和共享包含实时代码、公式、可视化和叙述文本的文档。用途包括:数据清洗和转换、数值模拟、统计建模、数据可视化、机器学习等,深受师生等数据分析者喜爱。

JupyterLab: Jupyter’s Next-Generation Notebook Interface
JupyterLab is a web-based interactive development environment for Jupyter notebooks, code, and data. JupyterLab is flexible: configure and arrange the user interface to support a wide range of workflows in data science, scientific computing, and machine learning. JupyterLab is extensible and modular: write plugins that add new components and integrate with existing ones.
JupyterLab是Jupyter主打的最新数据科学生产工具,某种意义上,它的出现是为了取代Jupyter Notebook。据官方所言,当Jupyter Lab迭代到v1.0.0正式版时,便会取代现有的Notebook。
JupyterLab作为一种基于web的集成开发环境,你可以使用它编写notebook、操作终端、编辑markdown文本、打开交互模式、查看csv文件及图片等功能。
你可以把JupyterLab当作一种究极进化版的Jupyter Notebook。原来的单兵作战,现在是“海陆空”联合协作。
总之,JupyterLab有以下特点:
交互模式:Python交互式模式可以直接输入代码,然后执行,并立刻得到结果,因此Python交互模式主要是为了调试Python代码用的
内核支持的文档:使你可以在可以在Jupyter内核中运行的任何文本文件(Markdown,Python,R等)中启用代码
模块化界面:可以在同一个窗口同时打开好几个notebook或文件(HTML, TXT, Markdown等等),都以标签的形式展示,更像是一个IDE
镜像notebook输出:让你可以轻易地创建仪表板
同一文档多视图:使你能够实时同步编辑文档并查看结果
支持多种数据格式:你可以查看并处理多种数据格式,也能进行丰富的可视化输出或者Markdown形式输出
云服务:使用Jupyter Lab连接Google Drive等服务,极大得提升生产力
统计专业学生 Dwzb
为什么发布此教程?
为方便在校Python学习,我于近期搭建(折腾)了JupyterLab,发现互联网现行的种种教程大多为复制粘贴,把我引到一个个坑里。
又鉴于Pip安装服务端的少见性,与Jupyter为进行浏览器与服务器双向通信所采用的Websocket技术的特殊性,众多用户在配置守护进程、反向代理与CDN网络时遇到了极大的问题,我决定重新梳理教程,采用现行最新版本JupyterLab与pip3,写一篇最简单、最可靠、最稳定的JupyterLab安装教程。
基于pip3安装JupyterLab
根据官网,安装JupyterLab有Anaconda与pip3两种方案,我认为使用大多数人采用的pip3便可以简单顺利的完成任务,省去再度配置Anaconda与系统多Python环境等种种问题的困扰。
进行下述操作前,请你准备一台干净的Linux服务器。我采用的是Ubuntu Server 20.04 LTS + 自带Python3.8.5 + Apache环境。若你打算将服务器专职运行JupyterLab,那么无需其它操作。若你的服务器还要部署其它web服务,请先准备好Web服务器环境并掌握简单配置知识(0基础建议直接采用宝塔面板)
备注:本教程采用Linux普通用户(非root)安装并配置所有软件、服务
#安装pip3
sudo apt install python3-pip
#更新pip3,执行此操作后会在/home/你的用户名/.local/bin下重新安装pip3
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
#卸载系统中的pip3(可选,防止混淆),刚才更新的仍会保留
sudo apt remove python3-pip
#配置环境变量
cd
nano .bashrc
#此时在其中加入
export PATH="$PATH:/home/你的用户名/.local/bin"
#随后按Ctrl+X保存退出
#使其生效
source .bashrc
#建议重启
sudo reboot
#设置pip3为清华源
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
#正式安装Jupyter
pip3 install jupyterlab
#创建个文件夹备用
mkdir ~/jupyter
这时,未配置的JupyterLab已经被安装到~/.local/bin了(~为/home/你的用户名/,下同),同时,~/.jupyter/文件夹生成,这是默认配置文件所在地
完成包括密码设置在内的JupyterLab配置
因为在JupyterLab中可以随意执行代码乃至进入bash,所以在公网环境中使用必须设置访问密码,否则会带来极大的安全隐患。生成密码非常简单,直接运行
jupyter lab password
然后根据提示输入密码,这会在~/.jupyter/jupyter_server_config.json
文件中写入密码的哈希值,这样以后在新浏览器登陆时,就需要输入密码。
同时,请你执行下述操作,把像下面标红的部分一样的字符串复制到一旁备用
“password”:
“argon2:$argon2huhf73yfhskjdbfjWQ”
cat ~/.jupyter/jupyter_server_config.json | grep "password"
下面生成标准配置文件
jupyter lab --generate-config
默认生成了 ~/.jupyter/jupyter_lab_config.py
下面修改配置文件,在最底部增加
#同样还是nano ~/.jupyter/jupyter_lab_config.py
#在底部增加这些内容,然后Ctrl+x保存退出
c.ServerApp.token = ''
#因为公网访问且设置了密码,所以要去掉访问鉴权,以免受到影响
c.ServerApp.allow_root = True
#允许root运行,因为我们是普通用户配置的所以可加可不加,为了普适性还是加吧
c.ServerApp.ip = '0.0.0.0'
#配置监听,这样配置就可以
c.LabServerApp.open_browser = False
#去掉每次启动执行打开浏览器,服务器哪里来的浏览器呢?
c.ServerApp.notebook_dir = '/home/ubuntu/jupyter'
#设置notebook存放位置
c.ServerApp.password = u'argon2:$rgon2huhf73yfhskjdbfjWQ'
#把u后面(引号里面)换成上面的红色部分让你复制的,双引号换成单引号,像我这样),这是配置密码
c.ServerApp.port = 7601
#设置监听端口,你随意啦,为了方便我就按照这个了
c.ServerApp.allow_remote_access = True
#允许远程访问
然后我们就完成配置工作啦
配置supervisor守护进程,让Jupyter一直运行
sudo apt install supervisor
#包管理安装supervisor
因为大家系统各异,所以我只能帮到这里了,下面是必要的配置文件供你们参考
在supervisor配置文件目录下添加如下配置文件(如jupyter.conf)
[program:jupyter]
directory=/home/你的系统普通用户名/.local/bin/
command=/home/你的系统普通用户名/.local/bin/jupyter lab
autostart=true
autorestart=true
startsecs=10
stdout_logfile=/var/log/jupyter/stdout.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/var/log/jupyter/stderr.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
user = 你的系统普通用户名
environment = HOME="/home/你的系统普通用户名", USER="你的系统普通用户名"
一定要用sudo mkdir把logfile的绝对路径都创建!用touch把stdout.log,stderr.log在绝对位置创建!
#重新加载supervisor配置文件
sudo supervisorctl reload
这时,你已经可以用IP地址:7601(设置的端口,注意放开防火墙),访问JupyterLab了
配置反向代理到80端口
因为Websocket,所以需要特殊配置
Nginx配置文件如下
server {
server_name xx.xx.xx xx.xx.xxx.xx; # 服务器域名和 IP 地址
listen 80;
location / {
proxy_pass http://127.0.0.1:7601/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# jupyter 使用了 websocket 协议,所以需要配置支持 websocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 120s;
proxy_next_upstream error;
proxy_redirect off;
}
}
Apache配置文件如下
<VirtualHost *:80>
ServerName xx.xx.xx
ProxyRequests off
<Location />
ProxyPass http://localhost:7601/
ProxyPassReverse http://localhost:7601/
ProxyPassReverseCookieDomain localhost xx.xx.xx
RequestHeader set Origin "http://localhost:7601"
</Location>
<Location /api/kernels/>
ProxyPass ws://localhost:7601/api/kernels/
ProxyPassReverse ws://localhost:7601/api/kernels/
</Location>
<Location /terminals/websocket/>
ProxyPass ws://localhost:7601/terminals/websocket/
ProxyPassReverse ws://localhost:7601/terminals/websocket/
</Location>
</VirtualHost>
CDN加速(如需)
因为Websocket,所以需要选择支持Websocket的CDN,或专门的websocket CDN,我建议使用又拍云,配置简单哦。
总结
密码配置简单,反向代理也很好配置,主要是若配置不当,ws协议的请求都会失败。
因此需要对websocket请求处理一下,不能直接使用顶层的配置,不然会转成http请求。
这个问题还困扰了挺久的。

至此,本教程结束,快进入你的JupyterLab体验数据分析吧!
除非注明,文章均为学霸时光机原创,转载请遵循下文,注明出处URL
本站文章许可遵循《“知识共享”国际许可协议4.0》
本文地址:https://www.syf.ink/archives/1473
评论请前往留言板,为您推荐以下文章~