Centos8 部署 Jupyter Notebook
- 前言
- 一、环境调查
- 1.1 系统当前安装的全部软件包
- 1.2 PYTHON 版本
- 二、安装
- 2.1 第一次尝试
- 2.2 报错:Running pip as the 'root' ...
- 2.3 创建虚拟环境并再次安装
- 三、配置
- 3.1 生成默认配置文件
- 3.2 生成登录密码文本
- 3.3 编辑默认配置文件
- 四、启动
- 4.1 第一次尝试
- 4.2 创建用户并指定工作目录
- 4.3 再次启动服务
- 五、扩展部分
- 5.1 LINUX 查看后台任务
- 5.2 jupyter 切换到前台并关闭服务
- 5.3 编写的 jupyter.sh
- 5.4 指定 jupyter 工作目录
- 5.5 jupyter 用户的 .bash_profile
作者:高玉涵
时间:2022-10-10
环境:Linux centos8 5.13.6-1.el8.elrepo.x86_64 #1 SMP Tue Jul 27 09:43:50 EDT 2021 x86_64 x86_64 x86_64 GNU/Linux
前言
Jupyter(https://jupyter.org)项目是一个非营利性开源项目,于 2014 年由 IPython 项目中诞生,它能支持所有编程语言的交互式数据科学和科学计算。它的特点是能够在网页上直接执行编写的代码,同时支持动态交互,在做数据可视化时尤其方便。
为了能随时随地调试稍纵即失的灵感代码,我专门在网上购买了一台云主机并部署了 Jupyter Notebook 。有了它我甚至可在手机上做一些有意思的实验。下面我将此次部署过程分享出来,希望对有类式需求的朋友有所帮助。
一、环境调查
1.1 系统当前安装的全部软件包
[root@centos8 ~]# yum grouplist
Last metadata expiration check: 1:37:08 ago on Mon 10 Oct 2022 01:50:05 PM CST.
Available Environment Groups:
Server with GUI
Minimal Install
Workstation
Custom Operating System
Virtualization Host
Installed Environment Groups:
Server
Installed Groups:
Container Management
Development Tools
Headless Management
Available Groups:
Legacy UNIX Compatibility
.NET Core Development
Graphical Administration Tools
Network Servers
RPM Development Tools
Scientific Support
Security Tools
Smart Card Support
System Tools
1.2 PYTHON 版本
[root@centos8 ~]# python3
Python 3.6.8 (default, Sep 10 2021, 09:13:53)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
二、安装
2.1 第一次尝试
开始时,想着这台云主机就我一个人使用,根据经验也为了省事就以 root 用户执行安装命令,没想到事与愿违,期间摔了不大不小的坑(下面的操作如未注明,均在 root 用户下操作)。
pip install jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple
注:由于 pip 命令中存储库文件的服务器在境外,有时候会比较慢,可以在命令后增加-i
参数,使用国内的镜像源,这里的 https://pypi.tuna.tsinghua.edu.cn/simple
使用的是清华大学的镜像源。
2.2 报错:Running pip as the ‘root’ …
WARNING: Running pip as the ‘root’ user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
- 原因:根据警告提示以
root
用户运行pip
可能会导致权限破坏和与系统包管理器的行为冲突。建议使用虚拟环境: 虚拟环境。
2.3 创建虚拟环境并再次安装
cd /data
mkdir jupyter
cd jupyter
python3 -m venv jupyter-env
source jupyter-env/bin/activate
pip install jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple
(...提示需需更新 pip)
(jupyter-env) [root@centos8 jupyter]# pip install --upgrade pip
Cache entry deserialization failed, entry ignored
Collecting pip
Using cached https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 9.0.3
Uninstalling pip-9.0.3:
Successfully uninstalled pip-9.0.3
Successfully installed pip-21.3.1
You are using pip version 21.3.1, however version 22.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(jupyter-env) [root@centos8 jupyter]# pip install --upgrade pip
Requirement already satisfied: pip in ./jupyter-env/lib/python3.6/site-package
s (21.3.1)
pip install jupyter -i https://pypi.tuna.tsinghua.edu.cn/simple
(...成功...)
三、配置
3.1 生成默认配置文件
jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py
3.2 生成登录密码文本
如果不配置登录密码,将难以登录和使用。
(jupyter-env) [root@centos8 jupyter]# python
Python 3.6.8 (default, Sep 10 2021, 09:13:53)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from notebook.auth import passwd
>>> passwd()
Enter password:
Verify password:
'系统生成的密文,保存下来,后面将用到。'
>>>
3.3 编辑默认配置文件
vim ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.ip='*' #设置所有 ip 皆可访问
c.NotebookApp.password = 'xxxxxxx......' # 复制刚才生成的那个密文'
c.NotebookApp.open_browser = False # 禁止自动打开浏览器
c.NotebookApp.port = 8888 # 指定你授权防火墙通的的端口
四、启动
4.1 第一次尝试
jupyter-env) [root@centos8 jupyter]# jupyter-notebook
[I 11:09:54.365 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[W 11:09:54.645 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[C 11:09:54.646 NotebookApp] Running as root is not recommended. Use --allow-root to bypass.
- 报错:Running as root is not recommended…
Running as root is not recommended. Use --allow-root to bypass.(不建议以root用户运行。使用——allow-root来绕过。)
当看到这条提示后,考虑到服务要挂在网上,为了安全起见,决定单独为 jupyter 创建一个普通用户。
4.2 创建用户并指定工作目录
useradd jupyter
usermod -d /data/jupyter jupyter
chown jupyter:jupyter jupyter
passwd jupyter
4.3 再次启动服务
启动前,将先前 3.1 生成默认配置文件 小节以 root 用户生成和修改后的配置文件移到 jupyter 用户目录下并修改它的权限。
mv .jupyter /data/jupyter
cd /data
chown -R jupyter:jupyter .jupyter
再次启动。
su - jupyter(切换用户)
source jupyter-env/bin/activate(切换虚拟环境)
jupyter-notebook
(jupyter-env) [jupyter@centos8 ~]$ jupyter-notebook
[W 12:05:24.237 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 12:05:24.239 NotebookApp] Serving notebooks from local directory: /data/jupyter
[I 12:05:24.239 NotebookApp] Jupyter Notebook 6.4.10 is running at:
[I 12:05:24.239 NotebookApp] http://centos8:8888/
[I 12:05:24.240 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
^C[I 12:05:34.474 NotebookApp] interrupted
Serving notebooks from local directory: /data/jupyter
0 active kernels
Jupyter Notebook 6.4.10 is running at:
http://centos8:8888/
Shutdown this notebook server (y/[n])? y
[C 12:05:35.748 NotebookApp] Shutdown confirmed
[I 12:05:35.748 NotebookApp] Shutting down 0 kernels
[I 12:05:35.749 NotebookApp] Shutting down 0 terminals
或者后台启动。
nohup jupyter-notebook &
至此,jupyter notebook 部署完成并成功的启动了服务(界面如下)。
五、扩展部分
5.1 LINUX 查看后台任务
jobs
[1]+ Running jupyter-notebook &
5.2 jupyter 切换到前台并关闭服务
(jupyter-env) [jupyter@centos8 ~]$ fg
jupyter-notebook
^C[I 12:09:50.031 NotebookApp] interrupted
Serving notebooks from local directory: /data/jupyter
0 active kernels
Jupyter Notebook 6.4.10 is running at:
http://centos8:8888/
Shutdown this notebook server (y/[n])? y
[C 12:09:50.804 NotebookApp] Shutdown confirmed
[I 12:09:50.805 NotebookApp] Shutting down 0 kernels
[I 12:09:50.806 NotebookApp] Shutting down 0 terminals
5.3 编写的 jupyter.sh
为了启动 jupyter 时少输入些字符,在用户工作目录下创建了 bin/
目录用于存放 jupyter.sh 脚本文件,内容也很简单启动时,重定向了标准输出和错误,并以后台方式启动服务。
#!/bin/bash
DAY=`date +'%Y-%m-%d'`
jupyter-notebook >>~/log/jupyter_${DAY}.log 2>>~/log/jupyter_${DAY}.err &
5.4 指定 jupyter 工作目录
为进一步增加安全,通过 WEB 访问 jupyter 时,将用户限制在特定目录下。在用户目录下创建 jupyter-work/
目录并修改以下配置。
vi .jupyter/jupyter_notebook_config.py
c.NotebookApp.notebook_dir = 'jupyter-work/'
5.5 jupyter 用户的 .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
LC_ALL="en_US.UTF-8"
PATH=$PATH:$HOME/bin
export LC_ALL PATH