ติดตั้ง Jupyter Notebook บน VPS สำหรับ Data Science
Jupyter Notebook บน VPS ให้พลังของ remote server (CPU, RAM, disk มากกว่า laptop) ผ่าน browser ทำให้รัน data processing ขนาดใหญ่ machine learning หรือ Python scripts ได้โดยไม่ต้องรอบนเครื่อง local บทความนี้ตั้ง Jupyter อย่างปลอดภัยพร้อม password และ SSL
ติดตั้ง Python และ Jupyter
sudo apt update
sudo apt install python3 python3-pip python3-venv -y
# สร้าง virtual environment (แนะนำ)
python3 -m venv ~/jupyter-env
source ~/jupyter-env/bin/activate
# ติดตั้ง Jupyter Lab (เวอร์ชัน modern กว่า Notebook)
pip install jupyterlab numpy pandas matplotlib scikit-learn
# หรือ Jupyter Notebook classic
pip install notebook
ตั้ง Password และ Config
# สร้าง config file
jupyter lab --generate-config
# ตั้ง password
jupyter lab password
# หรือ
python3 -c "from jupyter_server.auth import passwd; print(passwd())"
nano ~/.jupyter/jupyter_lab_config.py
# ตั้งค่าความปลอดภัย
c.ServerApp.ip = '127.0.0.1' # bind localhost เท่านั้น
c.ServerApp.port = 8888
c.ServerApp.open_browser = False
c.ServerApp.notebook_dir = '/home/ubuntu/notebooks'
c.ServerApp.password = 'argon2:...' # ค่าจาก passwd() command
เข้าใช้งานผ่าน SSH Tunnel (วิธีปลอดภัยที่สุด)
# รัน Jupyter บน server
jupyter lab --no-browser
# สร้าง tunnel จาก laptop (อีก terminal หนึ่ง)
ssh -L 8888:localhost:8888 ubuntu@YOUR_VPS_IP
# เปิด browser
# http://localhost:8888
ตั้งเป็น systemd Service
sudo nano /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter Lab
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu
ExecStart=/home/ubuntu/jupyter-env/bin/jupyter lab --config=/home/ubuntu/.jupyter/jupyter_lab_config.py
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now jupyter
เปิดสาธารณะด้วย Nginx + SSL (ถ้าต้องการ)
sudo nano /etc/nginx/sites-available/jupyter
server {
listen 443 ssl;
server_name jupyter.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/jupyter.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/jupyter.yourdomain.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade"; # สำหรับ WebSocket
proxy_set_header Host $host;
}
}
ตรวจสอบว่า Jupyter ทำงานถูกต้อง
# ตรวจ service
systemctl status jupyter
# ตรวจ port
ss -tlnp | grep 8888
# ทดสอบผ่าน SSH tunnel
ssh -L 8888:localhost:8888 ubuntu@VPS_IP
# เปิด http://localhost:8888
# ดู logs
journalctl -u jupyter -n 50
pip install tensorflow torch transformers seaborn plotly sqlalchemy ไม่กระทบ Python system ของ VPS
แก้ปัญหาที่พบบ่อย
Connection refused เมื่อเข้า http://localhost:8888
SSH tunnel ยังไม่ active หรือ Jupyter ไม่ได้รันบน VPS ตรวจสองฝั่ง:
# บน VPS ตรวจว่า Jupyter รัน
ps aux | grep jupyter
ss -tlnp | grep 8888
# บน local ตรวจว่า tunnel active
ssh -L 8888:localhost:8888 ubuntu@VPS_IP -v 2>&1 | grep "Entering interactive"
Jupyter kernel ตายกลางการทำงาน
Memory หมด หรือ Python process crash ตรวจ memory ของ VPS และเพิ่ม swap ถ้าจำเป็น:
# ดู memory ตอน kernel ทำงาน
free -h
# ดู kernel log
journalctl -u jupyter | grep -i "error\|killed\|oom"
# เพิ่ม swap ถ้า RAM ไม่พอ (ดูบทความ swap memory)
ไม่สามารถติดตั้ง package ใน kernel
Kernel ของ Jupyter อาจใช้ Python คนละตัวกับ virtualenv ต้องติดตั้งผ่าน kernel ตรงๆ:
# ใน notebook cell
import sys
!{sys.executable} -m pip install package-name
# หรือตรวจว่า kernel ใช้ Python ไหน
import sys
print(sys.executable)
WebSocket connection failed เมื่อเชื่อมต่อผ่าน Nginx
Nginx ต้องตั้ง upgrade header สำหรับ WebSocket:
location / {
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400; # ป้องกัน timeout ขณะ run long cell
}
คำถามที่พบบ่อย
Jupyter Notebook กับ JupyterLab ต่างกันอย่างไร?
Jupyter Notebook เป็นเวอร์ชันเดิม UI เรียบง่าย JupyterLab เป็นเวอร์ชันใหม่มี IDE-like features เช่น file browser, multiple tabs, extension marketplace ปัจจุบัน JupyterLab คือ default ที่แนะนำ
Jupyter บน VPS ดีกว่า Google Colab อย่างไร?
Google Colab ฟรีแต่ session หมดเองทุก 12 ชั่วโมง ไม่มี persistent storage และ GPU ไม่ stable Jupyter บน VPS รันตลอดเวลา ข้อมูลอยู่บน VPS ตลอด สามารถตั้ง RAM/CPU ตามต้องการ และ cost ถูกกว่าสำหรับใช้งานหนักสม่ำเสมอ
ทำ Auto-save และ Backup notebooks ได้อย่างไร?
Jupyter มี auto-save ทุก 2 นาทีโดย default ไฟล์ .ipynb อยู่ใน server สำหรับ backup ให้ใช้ rsync หรือ git: cd ~/notebooks && git init && git add . && git commit -m "backup" แล้ว push ไป private repository