完整懶人教學:GoDaddy 網域 + AWS EC2 + 固定 IP + HTTPS
最後更新:2026-10-13
你需要準備的東西:一張信用卡、一個 email
完成後你會有:一個用自己網域名稱、有 HTTPS 鎖頭的網站
預估時間:約 1~2 小時(含等待 DNS 生效)
每月花費:約 USD $5~15(EC2 t2.micro) + 網域年費約 USD $10~20
yourdomain.orgmysite.org).org、.com、.net 都可以。第一年通常有優惠,之後續費會比較貴,買之前看一下續費價格。ap-northeast-1)| 欄位 | 填什麼 |
|---|---|
| Name | 隨便取,例如 my-web-server |
| AMI(作業系統) | 選 Ubuntu Server 22.04 LTS(Free tier eligible) |
| Instance type | t2.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 |
16.76.146.43),記下來打開 Terminal,執行:
# 設定金鑰權限(只需要做一次)
chmod 400 ~/Downloads/my-key.pem
# SSH 連線(把 IP 換成你的 Elastic IP)
ssh -i ~/Downloads/my-key.pem ubuntu@你的Elastic-IP
方法 A:使用 Windows Terminal / PowerShell(Windows 10+)
ssh -i C:\Users\你的使用者名\Downloads\my-key.pem ubuntu@你的Elastic-IP
方法 B:使用 PuTTY
ubuntu@你的Elastic-IP.ppk 檔yes 即可。看到類似以下畫面就表示連線成功:
Welcome to Ubuntu 22.04.4 LTS
ubuntu@ip-172-31-xx-xx:~$
在 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 的預設歡迎頁面。
網站的檔案放在 /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!"
告訴 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 就沒問題。修改或新增以下兩條 A Record:
| Type | Name | Value | TTL |
|---|---|---|---|
| A | @ | 你的 Elastic IP(例如 16.76.146.43) | 600 秒 |
| A | www | 你的 Elastic IP(例如 16.76.146.43) | 600 秒 |
操作方式:
@ 的 A Record → 點編輯(鉛筆圖示),把 Value 改成你的 Elastic IP@,Value 填你的 IPwww 同理。如果 www 已經有一條 CNAME 記錄 → 先刪掉它,再新增 A Record@ 代表根網域(yourdomain.org)www 代表子網域(www.yourdomain.org)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 生效後才能做,否則會失敗。apt install certbot# 確保 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
如果兩個網域(@ 和 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! 就成功了。
--redirect 讓所有 HTTP 連線自動跳轉到 HTTPS方法 1:用瀏覽器
打開瀏覽器,依序測試:
https://yourdomain.org → 應該看到你的網頁 + 鎖頭https://www.yourdomain.org → 同上http://yourdomain.org → 應該自動跳轉到 httpshttp://www.yourdomain.org → 同上方法 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.org | 200 OK + 鎖頭 |
https://www.yourdomain.org | 200 OK + 鎖頭 |
http://yourdomain.org | 自動跳轉到 https |
http://www.yourdomain.org | 自動跳轉到 https |
# 測試自動續期(模擬,不會真的續期)
sudo certbot renew --dry-run
看到 Congratulations, all simulated renewals succeeded 就沒問題。
之後 Certbot 會自動在憑證到期前 30 天續期,你不需要做任何事。
/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/月 |
用 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 檔。
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
最常見的三個原因:
dig +short yourdomain.org 確認 IP 正確sudo systemctl start apache2# 測試設定檔語法
sudo apache2ctl configtest
# 如果報錯,看錯誤訊息指向哪個檔案,去修改它
sudo nano /etc/apache2/sites-available/yourdomain.conf
# 修完後重啟
sudo systemctl restart apache2
不會。Apache 預設開機自動啟動,Elastic IP 也不會變。重開機後網站會自動恢復。
# 即時查看錯誤紀錄
sudo tail -f /var/log/apache2/error.log
# 即時查看訪問紀錄
sudo tail -f /var/log/apache2/access.log
為每個網域建一個 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