概述
Jupyter Notebook 是数据科学和机器学习领域最常用的交互式编程工具。把它部署到远程服务器上,意味着你可以用一台便宜的VPS跑Python代码,通过浏览器随时访问。不用管本地环境配置,也不用担心笔记本性能不够。
准备工作
你需要以下条件:
- 一台Linux服务器(Ubuntu 20.04+ 或 Debian 11+)
- Python 3.8 或以上版本
- 一个域名(可选,用于HTTPS)
- SSH访问权限
步骤
第一步:安装Python和pip
登录到你的服务器,先更新系统包:
sudo apt update && sudo apt upgrade -y
安装Python和相关依赖:
sudo apt install -y python3 python3-pip python3-venv git curl
确认版本:
python3 --version
# 输出类似:Python 3.10.12
第二步:创建虚拟环境并安装Jupyter
在项目目录创建虚拟环境:
mkdir -p ~/jupyter-workspace && cd ~/jupyter-workspace
python3 -m venv jupyter-env
source jupyter-env/bin/activate
激活后,你会看到命令行前面多了一个 (jupyter-env) 前缀。接下来安装Jupyter:
pip install jupyterlab notebook
JupyterLab 是新版界面,比传统Notebook更强大。如果你只需要旧版界面,装 notebook 就够了。
第三步:生成配置文件
jupyter lab --generate-config
这会在家目录生成 .jupyter/jupyter_lab_config.py。
第四步:设置密码
进入Python交互环境,生成密码哈希:
python3 -c "from jupyter_server.auth import passwd; print(passwd('your_password_here', algorithm='sha256'))"
把输出的哈希字符串记下来,比如 sha256:abc123def456...。
编辑配置文件:
nano ~/.jupyter/jupyter_lab_config.py
找到并修改以下行:
c.ServerApp.password = 'sha256:abc123def456...'
c.ServerApp.ip = '0.0.0.0'
c.ServerApp.port = 8888
c.ServerApp.open_browser = False
c.ServerApp.allow_remote_access = True
保存退出。
第五步:防火墙放行端口
如果用UFW防火墙,放行8888端口:
sudo ufw allow 8888/tcp
sudo ufw reload
第六步:后台运行Jupyter
推荐用screen或tmux保持会话:
tmux new -s jupyter
jupyter lab --no-browser --allow-root
按 Ctrl+B 然后 D 退出tmux会话。Jupyter会继续在后台运行。
也可以用systemd服务管理:
sudo nano /etc/systemd/system/jupyter.service
写入以下内容:
[Unit]
Description=Jupyter Lab Server
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/jupyter-workspace
ExecStart=/root/jupyter-workspace/jupyter-env/bin/jupyter lab --no-browser --allow-root --port=8888
Restart=on-failure
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl enable jupyter
sudo systemctl start jupyter
验证
浏览器访问 http://你的服务器IP:8888,输入刚才设置的密码,能看到JupyterLab界面就说明成功了。
检查服务状态:
sudo systemctl status jupyter
应该显示绿色的 active (running)。
常见问题
Q: 忘记密码怎么办?
重新生成密码哈希,替换配置文件中的password值,然后重启服务:
jupyter lab password
sudo systemctl restart jupyter
Q: 页面加载很慢或超时?
可能是服务器带宽不足。也可以加一个反向代理提升体验。用Nginx做反向代理:
sudo apt install -y nginx
sudo nano /etc/nginx/sites-available/jupyter
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
启用站点并重启Nginx:
sudo ln -s /etc/nginx/sites-available/jupyter /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Q: 想加HTTPS怎么办?
用Certbot申请免费证书:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
Certbot会自动帮你配置Nginx的SSL证书。
Q: 虚拟环境每次都要手动激活?
创建alias写入 ~/.bashrc:
echo 'source ~/jupyter-workspace/jupyter-env/bin/activate' >> ~/.bashrc
source ~/.bashrc
现在每次SSH登录都会自动激活环境。
部署完Jupyter之后,你就可以在云端跑Python脚本、分析数据、训练小模型了。配合VS Code Remote或者GitHub Codespaces,开发体验更完整。你平时用什么工具做数据分析?