從零架設 HTTPS 網站

完整懶人教學:GoDaddy 網域 + AWS EC2 + 固定 IP + HTTPS
最後更新:2026-10-13

你需要準備的東西:一張信用卡、一個 email

完成後你會有:一個用自己網域名稱、有 HTTPS 鎖頭的網站

預估時間:約 1~2 小時(含等待 DNS 生效)

每月花費:約 USD $5~15(EC2 t2.micro) + 網域年費約 USD $10~20

目錄
  1. Phase 1 — 買網域(GoDaddy)
  2. Phase 2 — 建立雲端主機(AWS EC2)
  3. Phase 3 — 綁定固定 IP(Elastic IP)
  4. Phase 4 — 連線到主機(SSH)
  5. Phase 5 — 安裝 Web Server(Apache)
  6. Phase 6 — 設定 DNS 指向主機
  7. Phase 7 — 啟用 HTTPS(Let's Encrypt)
  8. Phase 8 — 驗證 & 完成
  9. 常見問題

Phase 1 — 買網域(GoDaddy)

在 GoDaddy 買一個屬於你的網域名稱,例如 yourdomain.org
1 註冊 GoDaddy 帳號 & 買網域
  1. 到 godaddy.com
  2. 在搜尋框輸入你想要的網域名稱(例如 mysite.org)
  3. 選一個可用的網域,點 Add to Cart
  4. 結帳時它會推銷一堆加值服務(隱私保護、Email 等),全部都可以跳過
  5. 用信用卡付款完成購買
選 .org、.com、.net 都可以。第一年通常有優惠,之後續費會比較貴,買之前看一下續費價格。
✓ 完成後你擁有了一個網域名稱

Phase 2 — 建立雲端主機(AWS EC2)

在 AWS 上開一台虛擬主機(可以想像成你在雲端租了一台電腦)
2 註冊 AWS 帳號
  1. 到 aws.amazon.com,點 Create an AWS Account
  2. 填入 email、密碼、帳號名稱
  3. 選擇 Personal(個人用途)
  4. 輸入信用卡資訊(會扣 $1 美元驗證,之後退回)
  5. 手機簡訊驗證
  6. 選擇 Basic Support(Free) 方案
新帳號有 12 個月免費額度,t2.micro(最小機型)每月 750 小時免費。
3 啟動一台 EC2 主機
  1. 登入 AWS Console → 搜尋並進入 EC2
  2. 確認右上角的 Region(地區)選擇離你最近的(例如 Tokyo ap-northeast-1)
  3. 點 Launch Instance(啟動執行個體)
  4. 設定如下:
欄位填什麼
Name隨便取,例如 my-web-server
AMI(作業系統)選 Ubuntu Server 22.04 LTS(Free tier eligible)
Instance typet2.micro(Free tier eligible)
Key pair點 Create new key pair→ 取名 my-key → 類型選 RSA → 格式選 .pem(Mac/Linux)或 .ppk(Windows 用 PuTTY)→ 下載並妥善保存
Network settings勾選 Allow SSH traffic、Allow HTTP traffic、Allow HTTPS traffic
Storage預設 8 GB 即可,也可改 20 GB
  1. 點 Launch Instance
  2. 等幾秒鐘,Instance State 變成 Running 就成功了
Key pair 檔案(.pem)非常重要!這是你連線到主機的唯一鑰匙,弄丟了就無法 SSH 進去。請存到安全的地方,不要放在桌面或公開資料夾。
✓ 完成後你有一台在雲端運行的 Ubuntu 主機

Phase 3 — 綁定固定 IP(Elastic IP)

EC2 預設的 IP 每次重開機都會變。綁一個固定 IP,這樣網域才能永久指向它。
4 申請 & 綁定 Elastic IP
  1. 在 AWS Console → EC2 → 左側選單找到 Elastic IPs
  2. 點 Allocate Elastic IP address → 點 Allocate
  3. 你會得到一個固定 IP(例如 16.76.146.43),記下來
  4. 選取這個 IP → 點 Actions → Associate Elastic IP address
  5. 在 Instance 欄位選擇你剛建立的 EC2 instance → 點 Associate
Elastic IP 綁定到運行中的 EC2 是免費的。但如果你有 Elastic IP 卻沒綁到任何 instance(或 instance 停了),AWS 會收費。不用的話記得釋放掉。
✓ 完成後你的主機有了一個不會變的固定 IP

Phase 4 — 連線到主機(SSH)

用 SSH 遠端連線到你的 EC2 主機,就像遠端遙控那台電腦。
5 SSH 連線

Mac / Linux

打開 Terminal,執行:

# 設定金鑰權限(只需要做一次)
chmod 400 ~/Downloads/my-key.pem

# SSH 連線(把 IP 換成你的 Elastic IP)
ssh -i ~/Downloads/my-key.pem ubuntu@你的Elastic-IP

Windows

方法 A:使用 Windows Terminal / PowerShell(Windows 10+)

ssh -i C:\Users\你的使用者名\Downloads\my-key.pem ubuntu@你的Elastic-IP

方法 B:使用 PuTTY

  1. 下載安裝 PuTTY
  2. 打開 PuTTY → Host Name 輸入 ubuntu@你的Elastic-IP
  3. 左邊選 Connection → SSH → Auth → Browse 選你的 .ppk 檔
  4. 點 Open
第一次連線會問你要不要信任這台主機,輸入 yes 即可。

看到類似以下畫面就表示連線成功:

Welcome to Ubuntu 22.04.4 LTS
ubuntu@ip-172-31-xx-xx:~$
✓ 完成後你已經在遠端主機上了,可以開始輸入指令

Phase 5 — 安裝 Web Server(Apache)

在主機上安裝 Apache(網頁伺服器),讓別人可以透過瀏覽器看到你的網頁。
6 安裝 Apache

在 SSH 連線中依序執行:

# 更新系統套件清單
sudo apt-get update

# 安裝 Apache
sudo apt-get install -y apache2

# 啟用 SSL 和 Rewrite 模組(後面 HTTPS 會用到)
sudo a2enmod ssl rewrite

# 重啟 Apache
sudo systemctl restart apache2

現在打開瀏覽器,輸入 http://你的Elastic-IP,應該會看到 Apache 的預設歡迎頁面。

✓ 完成後用 IP 就能看到網頁了
7 建立你的網站內容

網站的檔案放在 /var/www/html/ 目錄下:

# 建立一個簡單的首頁(可以之後再改)
sudo bash -c 'cat > /var/www/html/index.html << "EOF"
<!DOCTYPE html>
<html>
<head><title>My Website</title></head>
<body>
    <h1>Hello World!</h1>
    <p>我的網站上線了!</p>
</body>
</html>
EOF'

重新整理瀏覽器,應該會看到 "Hello World!"

8 設定 Apache VirtualHost

告訴 Apache「當有人用我的網域來訪問時,要顯示哪個目錄的內容」:

# 建立設定檔(把 yourdomain.org 換成你的網域)
sudo bash -c 'cat > /etc/apache2/sites-available/yourdomain.conf << "EOF"
<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.org
    ServerName yourdomain.org
    ServerAlias www.yourdomain.org
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
    CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
</VirtualHost>
EOF'
# 啟用這個網站設定
sudo a2ensite yourdomain.conf

# 測試設定檔有沒有語法錯誤
sudo apache2ctl configtest

# 重啟 Apache 讓設定生效
sudo systemctl restart apache2
apache2ctl configtest 顯示 Syntax OK 就沒問題。

Phase 6 — 設定 DNS 指向主機

把你在 GoDaddy 買的網域指向你的 EC2 主機 IP。這樣別人輸入你的網域就會連到你的主機。
9 在 GoDaddy 設定 DNS A Record
  1. 登入 godaddy.com → 右上角帳戶 → My Products
  2. 在你的網域旁邊點 DNS(或 Manage → DNS)
  3. 你會看到 DNS Records 列表

修改或新增以下兩條 A Record:

TypeNameValueTTL
A@你的 Elastic IP(例如 16.76.146.43)600 秒
Awww你的 Elastic IP(例如 16.76.146.43)600 秒

操作方式:

什麼是 @ 和 www?
@ 代表根網域(yourdomain.org)
www 代表子網域(www.yourdomain.org)
兩個都設,這樣不管有沒有打 www 都能連到。
10 等待 DNS 生效 & 驗證

DNS 設定後需要等待一段時間才會全球生效(通常 5~30 分鐘,最慢可能幾小時)。

在你的 SSH 連線中執行以下指令確認:

# 檢查根網域
dig +short yourdomain.org A

# 檢查 www 子網域
dig +short www.yourdomain.org A

兩個都回傳你的 Elastic IP 就代表 DNS 生效了。

也可以直接在瀏覽器輸入 http://yourdomain.org 看看是否出現你的網頁。

如果 dig 還是顯示舊的 IP,不要急,等幾分鐘再試。下一步(取得 SSL 憑證)一定要等 DNS 生效後才能做,否則會失敗。
✓ 完成後瀏覽器輸入你的網域可以看到你的網頁(但還沒有 HTTPS 鎖頭)

Phase 7 — 啟用 HTTPS(Let's Encrypt 免費 SSL 憑證)

讓你的網站從 http:// 升級成 https://(瀏覽器會顯示鎖頭),而且完全免費。
11 安裝 Certbot
重要:不要用 apt install certbot
Ubuntu 22.04 的 apt 版本有 bug(pyOpenSSL 不相容),執行會報錯。請用下面的 snap 方式安裝。
# 確保 snap 已安裝並更新
sudo snap install core
sudo snap refresh core

# 安裝 certbot
sudo snap install --classic certbot

# 建立捷徑讓 certbot 指令可直接使用
sudo ln -sf /snap/bin/certbot /usr/bin/certbot

# 確認安裝成功(應該顯示版本號)
certbot --version
12 取得 SSL 憑證(一鍵搞定)
執行前請確認 Step 10 的 DNS 已經生效!

如果兩個網域(@ 和 www)的 DNS 都已生效:

sudo certbot --apache \
  -d yourdomain.org \
  -d www.yourdomain.org \
  --non-interactive \
  --agree-tos \
  --email your-email@example.com \
  --redirect

如果只有根網域生效,www 還沒好:

# 先只申請根網域
sudo certbot --apache \
  -d yourdomain.org \
  --non-interactive \
  --agree-tos \
  --email your-email@example.com \
  --redirect

# 等 www 的 DNS 也生效後,再擴展憑證
sudo certbot --apache \
  -d yourdomain.org \
  -d www.yourdomain.org \
  --non-interactive \
  --agree-tos \
  --email your-email@example.com \
  --redirect \
  --expand

看到 Congratulations! 就成功了。

Certbot 幫你做了什麼?

Phase 8 — 驗證 & 完成

13 測試你的網站

方法 1:用瀏覽器

打開瀏覽器,依序測試:

方法 2:用指令(在 SSH 中)

# HTTPS 應該回 200 OK
curl -I https://yourdomain.org
curl -I https://www.yourdomain.org

# HTTP 應該回 301 Moved Permanently(跳轉到 HTTPS)
curl -I http://yourdomain.org
curl -I http://www.yourdomain.org
URL預期結果
https://yourdomain.org200 OK + 鎖頭
https://www.yourdomain.org200 OK + 鎖頭
http://yourdomain.org自動跳轉到 https
http://www.yourdomain.org自動跳轉到 https
14 確認 SSL 憑證自動續期
# 測試自動續期(模擬,不會真的續期)
sudo certbot renew --dry-run

看到 Congratulations, all simulated renewals succeeded 就沒問題。

之後 Certbot 會自動在憑證到期前 30 天續期,你不需要做任何事。

恭喜!你的 HTTPS 網站已經上線了!
現在你可以把網頁檔案放到 /var/www/html/ 目錄下,全世界都能透過你的網域安全地訪問它。

你剛架設的東西(架構圖)


使用者瀏覽器
    │
    │  輸入 https://yourdomain.org
    ▼
┌──────────────┐
│   GoDaddy    │  DNS:yourdomain.org → 16.76.146.43
│  (DNS 管理) │
└──────┬───────┘
       │
       ▼
┌──────────────────────────────────────┐
│          AWS EC2 主機                 │
│  Elastic IP: 16.76.146.43            │
│                                      │
│  ┌────────────────────────────────┐  │
│  │  Apache Web Server             │  │
│  │  Port 80  → 301 轉到 HTTPS     │  │
│  │  Port 443 → 提供 HTTPS 服務    │  │
│  └────────────────────────────────┘  │
│                                      │
│  ┌────────────────────────────────┐  │
│  │  Let's Encrypt SSL 憑證        │  │
│  │  Certbot 自動續期              │  │
│  └────────────────────────────────┘  │
│                                      │
│  ┌────────────────────────────────┐  │
│  │  /var/www/html/                │  │
│  │  └── index.html(你的網頁)     │  │
│  └────────────────────────────────┘  │
└──────────────────────────────────────┘

每月花費估算

項目費用
GoDaddy 網域(年繳)~$10~20/年
EC2 t2.micro(新帳號第一年免費)$0 ~ $8.5/月
Elastic IP(綁定到運行中 EC2 免費)$0
Let's Encrypt SSL 憑證$0(免費)
資料傳輸(小流量網站)~$0~1/月
總計(免費額度期間)~$1/月
總計(免費額度到期後)~$10~15/月

常見問題

Q: 我要怎麼上傳網頁檔案到主機?

用 SCP 指令或 FileZilla(圖形化工具):

# SCP 上傳單一檔案
scp -i ~/Downloads/my-key.pem ./index.html ubuntu@你的IP:/var/www/html/

# SCP 上傳整個資料夾
scp -i ~/Downloads/my-key.pem -r ./my-site/ ubuntu@你的IP:/var/www/html/

用 FileZilla:Protocol 選 SFTP,Host 填你的 IP,Username 填 ubuntu,Key file 選你的 .pem 檔。

Q: Certbot 報錯 X509_V_FLAG_NOTIFY_POLICY

這是用 apt install certbot 造成的版本不相容問題。

# 移除舊版
sudo apt-get remove -y certbot python3-certbot-apache

# 改用 snap 安裝(見 Step 11)
sudo snap install --classic certbot

Q: Let's Encrypt 驗證失敗

最常見的三個原因:

  1. DNS 還沒生效 → 用 dig +short yourdomain.org 確認 IP 正確
  2. Port 80 沒開 → 去 AWS Security Group 檢查 Inbound Rules
  3. Apache 沒在跑 → 執行 sudo systemctl start apache2

Q: 我搞砸了 Apache 設定,怎麼辦?

# 測試設定檔語法
sudo apache2ctl configtest

# 如果報錯,看錯誤訊息指向哪個檔案,去修改它
sudo nano /etc/apache2/sites-available/yourdomain.conf

# 修完後重啟
sudo systemctl restart apache2

Q: 主機重開機後網站會不會掛?

不會。Apache 預設開機自動啟動,Elastic IP 也不會變。重開機後網站會自動恢復。

Q: 怎麼看 Apache 錯誤紀錄?

# 即時查看錯誤紀錄
sudo tail -f /var/log/apache2/error.log

# 即時查看訪問紀錄
sudo tail -f /var/log/apache2/access.log

Q: 我想要在同一台主機放多個網站怎麼辦?

為每個網域建一個 VirtualHost 設定檔和對應的目錄,再分別跑一次 Certbot 就可以了。

全部指令總整理(懶人複製貼上版)

把以下指令中所有 yourdomain.org 替換成你的網域,your-email@example.com 替換成你的 email,然後從頭到尾貼到 SSH 裡執行。
# ============================
# Step 1: 安裝 Apache
# ============================
sudo apt-get update
sudo apt-get install -y apache2
sudo a2enmod ssl rewrite
sudo systemctl restart apache2

# ============================
# Step 2: 建立網站首頁
# ============================
echo "<h1>Hello World!</h1>" | sudo tee /var/www/html/index.html

# ============================
# Step 3: 設定 VirtualHost
# ============================
sudo bash -c 'cat > /etc/apache2/sites-available/yourdomain.conf << "EOF"
<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.org
    ServerName yourdomain.org
    ServerAlias www.yourdomain.org
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
    CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
</VirtualHost>
EOF'

sudo a2ensite yourdomain.conf
sudo apache2ctl configtest
sudo systemctl restart apache2

# ============================
# Step 4: 安裝 Certbot (snap)
# ============================
sudo snap install core && sudo snap refresh core
sudo snap install --classic certbot
sudo ln -sf /snap/bin/certbot /usr/bin/certbot

# ============================
# Step 5: 取得 SSL 憑證
# (確認 DNS 已指向此 IP 後才執行)
# ============================
sudo certbot --apache \
  -d yourdomain.org \
  -d www.yourdomain.org \
  --non-interactive \
  --agree-tos \
  --email your-email@example.com \
  --redirect

# ============================
# Step 6: 驗證
# ============================
curl -I https://yourdomain.org
curl -I http://yourdomain.org
sudo certbot renew --dry-run