ติดตั้ง VS Code Server บน VPS รัน IDE ผ่าน Browser
VS Code Server เป็นเวอร์ชันเบาของ Visual Studio Code ที่รัน บนเซิร์ฟเวอร์ คุณสามารถเข้าใช้ IDE ผ่านเว็บเบราว์เซอร์จากทุกอุปกรณ์ ไม่ต้องติดตั้งโปรแกรมไหนบนเครื่องของคุณ เพียงแค่มีเว็บเบราว์เซอร์และอินเทอร์เน็ตก็พอ ในบทความนี้เราจะสอนติดตั้ง code-server (OpenVSCode Server) บน VPS ตั้ง HTTPS การป้องกันด้วย password และ SSH tunnel เพื่อใช้งานอย่างปลอดภัย
ข้อกำหนดระบบและการเตรียมการ
ก่อนเริ่มติดตั้ง VS Code Server ให้แน่ใจว่า VPS ของคุณมีข้อกำหนดต่อไปนี้:
- OS: Ubuntu 20.04 LTS / 22.04 LTS หรือ Debian 11+
- CPU: 1 core ขึ้นไป (แนะนำ 2 core สำหรับใช้งานหลายตัว)
- RAM: 512 MB ขั้นต่ำ (แนะนำ 2 GB หรือมากกว่า)
- Disk: 500 MB สำหรับติดตั้ง extensions เพิ่มเติม
- Port: 8080 (ค่าเริ่มต้น) ต้องเปิดใช้ได้
เข้า VPS ผ่าน SSH และทำการอัปเดตระบบ:
sudo apt update && sudo apt upgrade -y
ติดตั้ง code-server บน VPS
วิธีติดตั้ง code-server ที่ง่ายที่สุดคือใช้ script อย่างการติดตั้ง ทำให้กระบวนการติดตั้งเป็นอัตโนมัติ:
curl -fsSL https://code-server.dev/install.sh | sh
Script นี้จะทำการ:
- ตรวจสอบเวอร์ชันของระบบปฏิบัติการ
- ติดตั้ง code-server ลงใน
/usr/local/bin/code-server - สร้าง systemd service หรือ init script
- ตั้งค่า code-server ให้รัน โดยอัตโนมัติ
หลังจากติดตั้งเสร็จ ให้สตาร์ท code-server:
systemctl start code-server@$USER
systemctl enable code-server@$USER
$USER ด้วย username ของคุณตั้งค่า config และเข้ารหัสผ่าน
ไฟล์ config ของ code-server อยู่ที่ ~/.config/code-server/config.yaml แก้ไขได้ตามต้องการ:
nano ~/.config/code-server/config.yaml
เนื้อหาไฟล์ config มักจะดูเหมือนนี้:
bind-addr: 127.0.0.1:8080
auth: password
password: your_secure_password_here
cert: false
อธิบายตัวเลือกสำคัญ:
bind-addr- ที่อยู่ IP และ port ที่ code-server รับฟัง ปกติ 127.0.0.1:8080 (localhost)auth- วิธีการรับรอง identity ตั้งpasswordสำหรับการป้องกันด้วย passwordpassword- รหัสผ่าน code-server สร้างอัตโนมัติ เปลี่ยนเป็นรหัสผ่านแข็งแกร่งcert- ใช้ HTTPS ตั้งเป็นtrueหากต้องการเข้ารหัส SSL
ดูรหัสผ่านปัจจุบัน:
cat ~/.config/code-server/config.yaml | grep password
สร้างรหัสผ่านแข็งแกร่งใหม่:
openssl rand -base64 24
เปลี่ยนค่า password ในไฟล์ config แล้วรีสตาร์ท service:
systemctl restart code-server@$USER
ตั้งค่า Reverse Proxy ด้วย Nginx
เพื่อให้เข้าถึง code-server ได้ผ่านโดเมนของคุณ และใช้ HTTPS เราจะตั้งค่า Nginx เป็น reverse proxy:
sudo nano /etc/nginx/sites-available/code-server
ใส่การตั้งค่าต่อไปนี้ (แทนที่ domain.com ด้วยโดเมนของคุณ):
server {
listen 80;
server_name code.domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name code.domain.com;
ssl_certificate /etc/letsencrypt/live/code.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/code.domain.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_request_buffering off;
}
}
เปิดใช้งาน site:
sudo ln -s /etc/nginx/sites-available/code-server /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
สร้างใบรับรอง SSL ด้วย Certbot (ต้องติดตั้ง Certbot ก่อน):
sudo apt install certbot python3-certbot-nginx -y
sudo certbot certonly --nginx -d code.domain.com
ตั้งค่า SSH Tunnel (ทางเลือก)
หากไม่ต้องการเปิดโดเมนแบบสาธารณะ คุณสามารถใช้ SSH tunnel เพื่อเข้าถึง code-server อย่างปลอดภัยจากเครื่องท้องถิ่นของคุณ:
ssh -L 8080:localhost:8080 user@your_vps_ip
หลังจากนั้น เปิดเว็บเบราว์เซอร์และเข้า:
http://localhost:8080
ใส่รหัสผ่าน code-server แล้วกด Enter จากนั้นคุณจะเข้า VS Code บน VPS ได้สำเร็จ
การจัดการ Extensions และ Settings
code-server รองรับ VS Code extensions เกือบทั้งหมด ติดตั้ง extensions ได้ผ่าน UI หรือ command line:
code-server --install-extension ms-python.python
code-server --install-extension golang.go
code-server --install-extension rust-lang.rust-analyzer
extensions ติดตั้งไว้ใน ~/.local/share/code-server/extensions/
ตัวอย่าง extensions ที่นิยม:
| Extension | ประโยชน์ |
|---|---|
| Python | ตรวจสอบไวยากรณ์ debugger และ IntelliSense สำหรับ Python |
| Go | พัฒนา Go applications |
| Prettier | Format โค้ด JavaScript, HTML, CSS อัตโนมัติ |
| GitLens | Git integration ขั้นสูง |
| Docker | จัดการ Docker containers และ images |
ตรวจสอบว่า VS Code Server ทำงานถูกต้อง
ตรวจสอบ service ทำงานอยู่หรือไม่:
systemctl status code-server@$USER
ต้องเห็น output ที่ว่า active (running)
ตรวจสอบว่า port 8080 เปิดฟังอยู่:
ss -tlnp | grep 8080
ต้องเห็น output เช่น LISTEN 127.0.0.1:8080
เทสการเชื่อมต่อ HTTP:
curl -s http://127.0.0.1:8080 | head -20
ต้องเห็น HTML markup ของ code-server UI
ตรวจสอบไฟล์ log เพื่อดูข้อผิดพลาด:
journalctl -u code-server@$USER -n 50 --no-pager
error ใน log ให้อ่านข้อความตั้งแต่บรรทัดแรกจึงจะรู้ว่ามีปัญหาอะไรแก้ปัญหาที่พบบ่อย
Port 8080 ถูกใช้งานอยู่แล้ว
ถ้า code-server ไม่สามารถรัน ลองแก้ไขไฟล์ config:
sudo nano ~/.config/code-server/config.yaml
เปลี่ยนบรรทัด bind-addr เป็น port อื่น เช่น 8081:
bind-addr: 127.0.0.1:8081
บันทึกไฟล์แล้วรีสตาร์ท:
systemctl restart code-server@$USER
เข้ารหัส HTTPS บน Nginx แล้วหน้าขึ้น 502 Bad Gateway
ปัญหาสำหรับใช้ WebSocket หรือการเชื่อมต่อแบบสดๆ ต้องเพิ่ม header ต่อไปนี้ใน Nginx config:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering off;
proxy_request_buffering off;
เทส Nginx syntax แล้วรีโหลด:
sudo nginx -t && sudo systemctl reload nginx
รหัสผ่าน VS Code Server บลลงทะเบียน ไม่สามารถเข้าได้
ตรวจดูว่า config file มี password หรือไม่:
grep password ~/.config/code-server/config.yaml
ถ้าขาด password ให้สร้างรหัสผ่านใหม่:
openssl rand -base64 24
วาง output ลงใน config file:
sed -i 's/password: .*/password: YOUR_NEW_PASSWORD/' ~/.config/code-server/config.yaml
แทน YOUR_NEW_PASSWORD ด้วยรหัสผ่านจริง แล้วรีสตาร์ท:
systemctl restart code-server@$USER
Extension ติดตั้งไม่ได้หรือ timeout
หาก extension ติดตั้งช้า ตรวจดูเน็ต connectivity:
ping marketplace.visualstudio.com
ถ้าติดตั้ง extension ผ่าน command line แล้วติดตั้งไม่ได้ ลองเพิ่ม timeout:
code-server --install-extension ms-python.python --install-extension-pack-retry-count 10
หรือติดตั้งผ่าน UI ของ code-server เลย
คำถามที่พบบ่อย
Q1: VS Code Server สามารถแชร์ใหม่กับเพื่อนได้ไหม?
ได้ แต่ต้องระมัดระวังด้านความปลอดภัย แนะนำให้ใช้ SSH key authentication แทน password ปกติ และตั้งค่า firewall ให้แคบลง ถ้าต้องการให้หลาย user ใช้ร่วมกัน อาจตั้ง code-server ใจเย็นสำหรับแต่ละ user หรือใช้โปรเจกต์ open source เช่น Theia IDE ที่มีฟีเจอร์สำหรับทีมได้ดีกว่า
Q2: VS Code Server ปลอดภัยไหม ถ้าเข้าผ่าน HTTPS?
ปลอดภัยมาก ปกติ HTTPS ใช้ TLS 1.2 ขึ้นไปเข้ารหัส การสื่อสารทั้งหมด หากตั้งรหัสผ่านแข็งแกร่งด้วย ความปลอดภัยจะดียิ่งขึ้น เพิ่มเติม อาจตั้ง firewall ให้อนุญาตเฉพาะ IP หรือใช้ VPN เพิ่มเติม
Q3: VS Code Server ต้องมี RAM เท่าไหร่ขั้นต่ำ?
ขั้นต่ำ 512 MB แต่เมื่อติดตั้ง extensions หลาย ตัว ตัวแปร extensions ตัวเองอาจต้องการ RAM เพิ่มเติม แนะนำให้มี 2 GB สำหรับการทำงานปกติ และ 4 GB หากใช้ extension ขนาดใหญ่ เช่น Python, Go, Rust analyzer เพิ่มเติม
สำหรับนักพัฒนาที่ต้องการ VPS ที่เสถียรและรวดเร็วสำหรับรัน VS Code Server asiagb.com มีบริการ VPS ในประเทศไทยพร้อม SSD ความเร็วสูง uptime 99% เหมาะสำหรับ remote development environment ที่ต้องทำงานตลอด 24 ชั่วโมง