Module 9 - 基礎網路與部署

VPS 與雲端伺服器入門

作者: Lynn 最後更新: 2026-09-23

VPS 與雲端伺服器入門

Reverse Proxy 架構圖 (Nginx)

VPS(Virtual Private Server)是一台虛擬的獨立伺服器。你可以在上面安裝任何軟體、架設網站、跑 API、部署 AI 應用。

三種伺服器方案

實體機 (Bare Metal) VPS 雲端 (AWS/GCP/Azure)
你擁有什麼 整台機器 一台虛擬機 按需的虛擬機
獨立性 完全獨立 獨立 OS、IP、root 獨立但共享底層
月費 $100+ USD $3-50 USD 按用量計費
擴展 買新機器 換方案 按鈕擴展
適合 大型企業 個人/小型服務 需要彈性的產品
概念圖:

一台實體伺服器
┌─────────────────────────────────────────┐
│  ┌─────────┐ ┌─────────┐ ┌─────────┐   │
│  │  VPS A  │ │  VPS B  │ │  VPS C  │   │
│  │  你的   │ │  別人的 │ │  別人的 │   │
│  │  Ubuntu │ │  CentOS │ │  Debian │   │
│  └─────────┘ └─────────┘ └─────────┘   │
│          虛擬化層 (Hypervisor)           │
│              實體硬體                    │
└─────────────────────────────────────────┘
每台 VPS 有獨立的 OS、IP、root 權限
互相看不到對方

常見 VPS 供應商

供應商 最低月費 特色
Vultr ~$2.50 全球節點多,台灣附近有東京
DigitalOcean ~$4 文件好,適合新手
Linode (Akamai) ~$5 穩定老牌
Hetzner ~$3.29 歐洲便宜大碗
AWS Lightsail ~$3.50 AWS 入門款

連線到 VPS

SSH 基礎

SSH(Secure Shell)是連接遠端伺服器的標準方式。

# 基本連線(密碼登入)
ssh root@123.45.67.89

# 使用 SSH Key(推薦)
ssh -i ~/.ssh/my_key root@123.45.67.89

# 指定 Port
ssh -p 2222 root@123.45.67.89

產生 SSH Key

# 產生 Ed25519 Key(推薦)
ssh-keygen -t ed25519 -C "your@email.com"

# 檔案產生在:
# ~/.ssh/id_ed25519     (私鑰,絕對不能外流)
# ~/.ssh/id_ed25519.pub (公鑰,放到伺服器上)

# 把公鑰複製到 VPS
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@123.45.67.89

VPS 初始安全設定

拿到新 VPS 後,第一件事是基本安全設定:

1. 更新系統

apt update && apt upgrade -y    # Debian/Ubuntu
yum update -y                    # CentOS/RHEL

2. 建立非 root 使用者

adduser deploy
usermod -aG sudo deploy

# 把 SSH Key 複製給新使用者
mkdir -p /home/deploy/.ssh
cp ~/.ssh/authorized_keys /home/deploy/.ssh/
chown -R deploy:deploy /home/deploy/.ssh

3. 關閉密碼登入 + root SSH

# 編輯 /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no

# 重啟 SSH
systemctl restart sshd

4. 設定防火牆

# UFW (Ubuntu)
ufw allow 22/tcp     # SSH
ufw allow 80/tcp     # HTTP
ufw allow 443/tcp    # HTTPS
ufw enable

# 確認狀態
ufw status

部署一個簡單服務

完整部署流程:

┌──────────────┐     ┌──────────────┐     ┌──────────────┐
│ 1. 安全設定    │ ──→ │ 2. 裝環境     │ ──→ │ 3. 部署應用   │
│ SSH, 防火牆   │     │ Node.js, etc │     │ git clone    │
└──────────────┘     └──────────────┘     └──────────────┘
                                                  ↓
┌──────────────┐     ┌──────────────┐     ┌──────────────┐
│ 6. 完成!     │ ←── │ 5. HTTPS     │ ←── │ 4. Nginx     │
│ 可以上線了    │     │ Certbot 憑證  │     │ Reverse Proxy│
└──────────────┘     └──────────────┘     └──────────────┘

以 Node.js 應用為例:

# 安裝 Node.js
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs

# 上傳你的專案(用 scp 或 git clone)
git clone https://github.com/you/app.git /opt/app
cd /opt/app && npm install

# 用 PM2 管理程序(自動重啟)
npm install -g pm2
pm2 start app.js
pm2 save
pm2 startup

Reverse Proxy (Nginx)

Reverse Proxy 架構:

使用者 (HTTPS:443)
       ↓
┌──────────────────┐
│   Nginx          │  處理 TLS/HTTPS、靜態檔案、負載均衡
│   Port 80/443    │
└────────┬─────────┘
         ↓ HTTP (內部)
┌──────────────────┐
│  你的 Node.js    │  只處理業務邏輯
│  Port 3000       │  不需要處理 HTTPS
└──────────────────┘

讓 Nginx 處理 HTTPS,轉發到你的 Node.js 應用:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
# 安裝 Nginx
apt install -y nginx

# 用 Certbot 免費取得 HTTPS 憑證
apt install -y certbot python3-certbot-nginx
certbot --nginx -d example.com

Serverless vs VPS

不是所有場景都需要 VPS。了解替代方案:

VPS Serverless (Cloudflare Workers, Vercel)
管理 你管 OS、更新、安全 供應商管基礎設施
費用 固定月費 按請求數計費
擴展 手動擴展或加機器 自動擴展
適合 長時間運行、自訂環境 API、靜態網站、短任務
冷啟動 可能有

作業

作業 1:SSH Key 實作

在你的電腦上實際操作:

  1. 產生一組 Ed25519 SSH Key
  2. 找到公鑰和私鑰的檔案位置
  3. 查看公鑰內容(cat ~/.ssh/id_ed25519.pub
  4. 解釋:為什麼私鑰不能給別人?

作業 2:VPS 選擇

假設你要部署一個小型 API 服務(預期每天 1000 次請求):

  1. 比較 Vultr、DigitalOcean、Hetzner 的最低方案
  2. 選一個你會用的供應商,說明理由
  3. 這個服務適合用 VPS 還是 Serverless?為什麼?

知識檢測

1 題 / 共 3

拿到新 VPS 後,以下哪個步驟最重要且應該最先執行?