下面以 CentOS 7 / 8 为例,说明如何在 CentOS 上安装并配置 Jupyter Notebook / JupyterLab,包括远程访问配置。
CentOS 一般自带 Python 2,需要 Python 3:
sudo yum install -y python3 python3-pip
验证:
python3 --version
pip3 --version
pip3 install jupyterlab
pip3 install notebook
jupyter notebook --generate-config
默认生成路径:
~/.jupyter/jupyter_notebook_config.py
jupyter notebook password
输入密码后,会生成:
~/.jupyter/jupyter_server_config.json
编辑配置文件:
vi ~/.jupyter/jupyter_notebook_config.py
添加或修改以下内容:
c.ServerApp.ip = '0.0.0.0' # 允许所有 IP 访问
c.ServerApp.open_browser = False # 不自动打开浏览器
c.ServerApp.port = 8888 # 端口
c.ServerApp.root_dir = '/home/youruser/jupyter' # 工作目录
如果是老版本(notebook < 6.0):
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888
sudo firewall-cmd --permanent --add-port=8888/tcp
sudo firewall-cmd --reload
如果是云服务器,还需在安全组放行 8888 端口。
jupyter lab
# 或
jupyter notebook
nohup jupyter lab > jupyter.log 2>&1 &
创建服务文件:
sudo vi /etc/systemd/system/jupyter.service
内容示例:
[Unit]
Description=Jupyter Notebook
After=network.target
[Service]
User=youruser
WorkingDirectory=/home/youruser
ExecStart=/usr/local/bin/jupyter lab
Restart=always
[Install]
WantedBy=multi-user.target
启用并启动:
sudo systemctl daemon-reload
sudo systemctl enable jupyter
sudo systemctl start jupyter
如需通过 http://jupyter.example.com 访问,可配置 Nginx 反向代理(可后续补充)。
如果你告诉我:
我可以给你一份完全适配的配置方案。