AWS EC2 (Ubuntu 22.04) + Apache + Let's Encrypt + GoDaddy DNS
最後更新:2026-10-13
yourdomain.org)到 AWS Console → EC2 → Security Groups,確認 Inbound Rules 包含:
| Type | Protocol | Port | Source |
|---|---|---|---|
| HTTP | TCP | 80 | 0.0.0.0/0 |
| HTTPS | TCP | 443 | 0.0.0.0/0 |
或用 AWS CLI:
# 查看目前規則
aws ec2 describe-security-groups --group-ids sg-xxxxxxxx \
--query 'SecurityGroups[0].IpPermissions' --output table
# 如果沒有 443,手動加入
aws ec2 authorize-security-group-ingress \
--group-id sg-xxxxxxxx \
--protocol tcp --port 443 --cidr 0.0.0.0/0
登入 GoDaddy → My Products → 你的網域 → DNS Management
新增或修改以下 A Records,將 IP 替換為你的 Elastic IP:
| Type | Name | Value | TTL |
|---|---|---|---|
| A | @ | 你的 Elastic IP | 600 秒 |
| A | www | 你的 Elastic IP | 600 秒 |
www 已有 CNAME 記錄指向其他地方,請先刪除再建立 A Record。驗證 DNS 是否生效(可能需要幾分鐘):
dig +short yourdomain.org A
dig +short www.yourdomain.org A
# 兩個都應該回傳你的 Elastic IP
# 更新套件庫 & 安裝 Apache
sudo apt-get update
sudo apt-get install -y apache2
# 啟用 SSL 和 Rewrite 模組
sudo a2enmod ssl rewrite
# 重啟 Apache
sudo systemctl restart apache2
# 確認 Apache 正在運行
sudo systemctl status apache2
# 建立網站根目錄(依你需求調整路徑)
sudo mkdir -p /var/www/html
# 建立測試首頁
echo "<h1>Hello from yourdomain.org</h1>" | sudo tee /var/www/html/index.html
sudo nano /etc/apache2/sites-available/yourdomain.conf
貼入以下內容(把 yourdomain.org 替換成你的網域):
<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>
# 啟用新網站設定
sudo a2ensite yourdomain.conf
# 停用預設網站(可選)
sudo a2dissite 000-default.conf
# 測試設定檔語法
sudo apache2ctl configtest
# 重啟 Apache
sudo systemctl restart apache2
用瀏覽器打開 http://yourdomain.org 確認看到測試頁面。
apt install certbot,Ubuntu 22.04 上的 apt 版本與 pyOpenSSL 不相容,會報錯。請用 Snap 安裝。# 確保 snap 已安裝且更新
sudo snap install core
sudo snap refresh core
# 移除舊版 certbot(如果有的話)
sudo apt-get remove -y certbot python3-certbot-apache 2>/dev/null
# 用 snap 安裝 certbot
sudo snap install --classic certbot
# 建立 symlink 讓 certbot 可直接使用
sudo ln -sf /snap/bin/certbot /usr/bin/certbot
# 確認安裝成功
certbot --version
sudo certbot --apache \
-d yourdomain.org \
--non-interactive \
--agree-tos \
--email your-email@example.com \
--redirect
sudo certbot --apache \
-d yourdomain.org \
-d www.yourdomain.org \
--non-interactive \
--agree-tos \
--email your-email@example.com \
--redirect \
--expand
參數說明:
--apache:使用 Apache 外掛,自動修改設定檔--redirect:自動設定 HTTP → HTTPS 301 轉址--expand:擴展已存在的憑證,加入新網域--non-interactive:不需手動互動確認# 測試 HTTPS 是否回應 200
curl -I https://yourdomain.org
curl -I https://www.yourdomain.org
# 測試 HTTP 是否自動轉址到 HTTPS(應看到 301)
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 | 301 → https://yourdomain.org |
http://www.yourdomain.org | 301 → https://www.yourdomain.org |
Let's Encrypt 憑證有效期為 90 天,Certbot 會自動續期。確認排程正常:
# 檢查 certbot 的自動續期排程
sudo systemctl list-timers | grep certbot
# 手動測試續期(不會真的續期,只是模擬)
sudo certbot renew --dry-run
--dry-run 測試失敗,檢查 Apache 是否正在運行、Port 80/443 是否開放。檔案:/etc/apache2/sites-available/yourdomain.conf
<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
RewriteEngine on
RewriteCond %{SERVER_NAME} =yourdomain.org [OR]
RewriteCond %{SERVER_NAME} =www.yourdomain.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
檔案:/etc/apache2/sites-available/yourdomain-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
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
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/yourdomain.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.org/privkey.pem
</VirtualHost>
</IfModule>
X509_V_FLAG_NOTIFY_POLICY這是 Ubuntu 22.04 apt 版本的 pyOpenSSL 不相容問題。
解法:移除 apt 版本,改用 snap 安裝(見 Step 5)。
常見原因:
dig +short yourdomain.org 確認sudo systemctl start apache2sudo certbot renew
通常不需要,Certbot 會自動處理。