本文說明在 Linux VPS 或獨立主機上,如何產生 CSR、安裝購買的 SSL 憑證到 Nginx 或 Apache,以及如何用 Certbot 申請免費憑證並自動續期。指令以 Rocky Linux/AlmaLinux 與 Ubuntu/Debian 為例,需要 root 權限。
- 用 openssl 在主機上產生 CSR 與私鑰,私鑰權限設為 600。
- 把網站憑證與中繼憑證合併成 fullchain.crt,再設定到 Nginx 或 Apache。
- 用 openssl s_client 檢查,看到 Verify return code: 0 (ok) 代表憑證鏈完整。
- 不需要付費憑證時,用 Certbot 申請 Let's Encrypt 免費憑證並自動續期。
最後更新:2026 年 11 月 29 日
產生 CSR 與私鑰
在要安裝憑證的主機上執行(把 yourdomain.com 換成您的網域):
mkdir -p /etc/ssl/yourdomain && cd /etc/ssl/yourdomain openssl req -new -newkey rsa:2048 -nodes \ -keyout yourdomain.key -out yourdomain.csr \ -subj "/C=TW/L=Taipei/O=Your Company/CN=www.yourdomain.com" chmod 600 yourdomain.key
CN填要保護的網域;萬用字元憑證填*.yourdomain.com。O填公司英文名稱(OV/EV 憑證需和公司登記一致)。- 執行
cat yourdomain.csr,把顯示的全部內容貼到工單中提供給我們。 yourdomain.key是私鑰,請留在主機上,不要寄給任何人。
上傳憑證並合併中繼憑證
收到憑證後,把網站憑證(yourdomain_com.crt)與中繼憑證(yourdomain_com.ca-bundle)上傳到 /etc/ssl/yourdomain/,再合併成一個完整憑證鏈檔,網站憑證要在前面:
cd /etc/ssl/yourdomain cat yourdomain_com.crt yourdomain_com.ca-bundle > fullchain.crt
fullchain.crt。Nginx 設定
在網站的 server 區塊加入(設定檔通常在 /etc/nginx/conf.d/ 或 /etc/nginx/sites-available/):
server {
listen 443 ssl;
http2 on;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/ssl/yourdomain/fullchain.crt;
ssl_certificate_key /etc/ssl/yourdomain/yourdomain.key;
ssl_protocols TLSv1.2 TLSv1.3;
# ...原本的網站設定...
}
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
Nginx 版本低於 1.25 時,把 http2 on; 刪除,改寫成 listen 443 ssl http2;。檢查設定並重新載入:
nginx -t && systemctl reload nginx
Apache 設定
先確認已安裝 SSL 模組:Rocky/AlmaLinux 執行 dnf install mod_ssl;Ubuntu/Debian 執行 a2enmod ssl。然後在網站設定加入:
<VirtualHost *:443>
ServerName www.yourdomain.com
ServerAlias yourdomain.com
DocumentRoot /var/www/yourdomain
SSLEngine on
SSLCertificateFile /etc/ssl/yourdomain/fullchain.crt
SSLCertificateKeyFile /etc/ssl/yourdomain/yourdomain.key
SSLProtocol -all +TLSv1.2 +TLSv1.3
</VirtualHost>
<VirtualHost *:80>
ServerName www.yourdomain.com
ServerAlias yourdomain.com
Redirect permanent / https://www.yourdomain.com/
</VirtualHost>
檢查設定並重新載入:Rocky/AlmaLinux 執行 apachectl configtest && systemctl reload httpd;Ubuntu/Debian 執行 apachectl configtest && systemctl reload apache2。
確認安裝結果
openssl s_client -connect www.yourdomain.com:443 -servername www.yourdomain.com < /dev/null 2>/dev/null | grep -E "subject=|issuer=|Verify return code"
看到 Verify return code: 0 (ok) 代表憑證鏈完整。也要記得開放防火牆的 443 連接埠(例如 firewall-cmd --permanent --add-service=https && firewall-cmd --reload)。
使用 Certbot 申請免費憑證
Certbot 會自動向 Let's Encrypt 申請憑證、修改 Nginx/Apache 設定,並定期自動續期。網域的 A 紀錄必須已經指向這台主機,且 80 連接埠可以從外部連線。
安裝 Certbot
# Rocky Linux / AlmaLinux dnf install epel-release dnf install certbot python3-certbot-nginx # Apache 改裝 python3-certbot-apache # Ubuntu / Debian apt install certbot python3-certbot-nginx # Apache 改裝 python3-certbot-apache
申請並安裝
certbot --nginx -d yourdomain.com -d www.yourdomain.com # Apache 把 --nginx 改成 --apache
依畫面輸入聯絡信箱並同意條款,Certbot 會自動完成設定。
確認自動續期
安裝後系統會建立排程自動續期。用以下指令模擬一次續期,沒有錯誤就代表設定正確:
certbot renew --dry-run
SSL 憑證系列文章
- SSL 憑證入門:種類與怎麼選
- 在 Plesk 開啟免費 SSL 憑證並自動續期
- 購買 SSL 憑證後的申請流程(CSR 與網域驗證)
- 在 Windows IIS 安裝 SSL 憑證
- 在 Linux Nginx/Apache 安裝 SSL 憑證(本篇)
- 在 cPanel 安裝 SSL 憑證
- SSL 憑證有效期縮短與自動續期
- SSL 憑證常見問題與疑難排解
如依上述步驟仍無法安裝,請 提交工單,並附上您的網域名稱、作業系統版本與錯誤訊息,我們將盡快協助您。