憑證簽發與部署

憑證簽發與部署

3 min read

技術教學

Ubuntu 24 + Apache + WordPress
PEM 格式 SSL 憑證導入指南

說明如何將憑證授權機構(CA)管理主控台核發的 PEM 格式 SSL 憑證,
導入至 Ubuntu 24 + Apache + WordPress 環境的一般性教學。

Ubuntu 24
Apache
WordPress
PEM SSL

免責聲明 #

本文為技術教學,說明在 Ubuntu 24 + Apache + WordPress 環境中導入 SSL 憑證(PEM 格式)的一般性程序。

內容係依撰寫當下之資訊為準,實際操作程序與設定方式可能因個別環境、設定、憑證種類、CA 規格、中介軟體版本等因素而有所不同。

概觀 #

本文為將PEM 格式 SSL 憑證
(CA 管理主控台中以文字方塊顯示的類型)導入
Ubuntu 24 + Apache + WordPress網站的一般性教學。

不論憑證為 EV/OV/DV 何種類型,伺服器端部署的基本程序皆相同。

事前準備 #

  • 作業系統:Ubuntu 24(具備 root 或 sudo 權限)
  • 網頁伺服器:Apache(apache2)正在執行中
  • CMS:WordPress 於 Apache 上運作中
  • 憑證:伺服器憑證已由 CA 核發
  • 中介憑證:CA 提供的中介憑證(CA Bundle)已備妥
  • 重要:擁有產生 CSR 時建立的私密金鑰(Private Key)(若遺失則須重新核發)

CA 主控台顯示的憑證格式(範例畫面) #

在 CA 管理主控台中,可能會以文字方塊顯示以下區塊(PEM)。請完整複製此區塊(從開頭到結尾)並儲存至伺服器。

-----BEGIN CERTIFICATE-----
(long string of alphanumeric characters)
-----END CERTIFICATE-----

注意

  • 複製時請一併包含 BEGIN/END 那兩行。
  • 即使換行方式有些許差異設定通常仍可運作,但最安全的做法是依畫面顯示原樣貼上。
  • 請勿貼上私密金鑰(BEGIN PRIVATE KEY)。

整體導入流程 #

  1. 將憑證檔案儲存至指定位置
  2. 準備中介憑證(憑證鏈)
  3. 建立 fullchain(伺服器憑證+中介憑證)
  4. 確認私密金鑰與憑證是否相符
  5. 設定 Apache HTTPS VirtualHost(含 80 → 443 轉址)
  6. 重新載入 Apache 並準備 WordPress URL/混合內容(Mixed Content)處理
  7. 驗證運作情形(憑證鏈、轉址、憑證資訊)

步驟 1憑證檔案放置(伺服器憑證) #

以下為 Ubuntu 常見的放置範例(請依貴公司的運作方針調整)。

儲存位置(範例) #

  • 私密金鑰(既有):/etc/ssl/private/example.com.key
  • 伺服器憑證:/etc/ssl/certs/example.com.crt
  • 中介憑證:/etc/ssl/certs/intermediate-ca.pem
  • Fullchain:/etc/ssl/certs/example.com.fullchain.pem

建立目錄與權限設定 #

sudo mkdir -p /etc/ssl/private /etc/ssl/certs
sudo chmod 700 /etc/ssl/private

將 CA 主控台的憑證(PEM)儲存為檔案 #

複製 CA 主控台文字方塊中的憑證區塊,並儲存至 /etc/ssl/certs/example.com.crt。

sudo nano /etc/ssl/certs/example.com.crt
sudo chmod 644 /etc/ssl/certs/example.com.crt

本機自動化儲存範例:

sudo tee /etc/ssl/certs/example.com.crt >/dev/null <<'EOF'
-----BEGIN CERTIFICATE-----
(paste certificate content here)
-----END CERTIFICATE-----
EOF
sudo chmod 644 /etc/ssl/certs/example.com.crt

步驟 2準備中介憑證(CA Bundle/Intermediate) #

許多 CA 會提供獨立於伺服器憑證之外的中介憑證(或 CA Bundle)。若缺少此憑證,部分裝置可能會顯示「不受信任」等訊息。

儲存中介憑證(範例) #

請將自 CA 取得的中介憑證(一份或多份)以 PEM 格式儲存至以下位置:

sudo nano /etc/ssl/certs/intermediate-ca.pem
sudo chmod 644 /etc/ssl/certs/intermediate-ca.pem

若有多份中介憑證時 #

若 CA Bundle 中包含多份中介憑證,請依 CA 指定的順序(通常朝向根憑證的方向)合併為單一檔案。

步驟 3建立 Fullchain(伺服器憑證+中介憑證) #

在 Apache 中,通常會將fullchain(伺服器憑證+中介憑證)指定為憑證檔案,此為標準做法。

sudo cat /etc/ssl/certs/example.com.crt \
  /etc/ssl/certs/intermediate-ca.pem \
  | sudo tee /etc/ssl/certs/example.com.fullchain.pem >/dev/null

sudo chmod 644 /etc/ssl/certs/example.com.fullchain.pem

步驟 4確認私密金鑰與憑證是否相符(重要) #

若憑證與私密金鑰不相符,即使完成 Apache 設定,HTTPS 仍無法正常運作。請務必進行確認。

確認方法 A:RSA 金鑰情況(比對 Modulus) #

openssl x509 -noout -modulus -in /etc/ssl/certs/example.com.crt | openssl md5
openssl rsa  -noout -modulus -in /etc/ssl/private/example.com.key | openssl md5

若兩者的 MD5 值相同,即代表相符。

確認方法 B:與金鑰類型無關(比對公開金鑰的 SHA256) #

# Certificate → Public Key → SHA256
openssl x509 -in /etc/ssl/certs/example.com.crt -pubkey -noout \
  | openssl pkey -pubin -outform DER \
  | openssl sha256

# Private Key → Public Key → SHA256
openssl pkey -in /etc/ssl/private/example.com.key -pubout -outform DER \
  | openssl sha256

若兩者的 SHA256 值相同,即代表相符。

若不相符時 #

  • 很可能是以不同的私密金鑰建立了 CSR。
  • 若找不到正確的私密金鑰,則必須從 CA 端重新核發(Reissue),並以正確的金鑰重新建立 CSR。

步驟 5於 Apache 啟用 HTTPS #

啟用必要模組 #

sudo a2enmod ssl
sudo a2enmod headers
sudo a2enmod rewrite
sudo systemctl reload apache2

確認是否正在監聽 443 連接埠 #

通常 /etc/apache2/ports.conf 中會含有 Listen 443。請視需要進行確認。

步驟 6Apache VirtualHost 設定(443 + 80→443 轉址) #

此處以 example.com 為例,建立並套用網站設定檔。

建立網站設定檔(範例) #

sudo nano /etc/apache2/sites-available/example.com.conf

設定範例(以 WordPress 為前提) #

※ 請將程式碼中的 example.com 與 DocumentRoot 替換為貴公司實際環境的內容。

<VirtualHost *:80>
    ServerName example.com

    # HTTP → HTTPS permanent redirect
    RewriteEngine On
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName example.com

    DocumentRoot /var/www/example.com/public

    # For WordPress (.htaccess operation case)
    <Directory /var/www/example.com/public>
        AllowOverride All
        Require all granted
    </Directory>

    SSLEngine on

    # Specify fullchain (server certificate + intermediate)
    SSLCertificateFile      /etc/ssl/certs/example.com.fullchain.pem
    SSLCertificateKeyFile   /etc/ssl/private/example.com.key

    # Recommended: Enable as needed (prioritize operation verification first)
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

    ErrorLog ${APACHE_LOG_DIR}/example.com-ssl-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-ssl-access.log combined
</VirtualHost>
</IfModule>

啟用網站 #

sudo a2ensite example.com.conf

設定測試 → 重新載入 #

sudo apache2ctl configtest
sudo systemctl reload apache2

確認目前已啟用哪些 VirtualHost:

sudo apache2ctl -S

步驟 7將 WordPress 轉換為 HTTPS #

將網站網址統一為 HTTPS #

  • WordPress 管理主控台 → 設定 → 一般
  • 將「WordPress 網址(URL)」與「網站網址(URL)」皆設為 https://example.com

混合內容(Mixed Content)因應 #

  • 若圖片、CSS/JS 仍維持 http://,將導致鎖頭圖示消失。
  • 可透過資料庫置換、佈景主題設定、外掛等方式統一為 https://(建議操作前先備份)。

步驟 8驗證運作 #

HTTPS 連線確認(curl) #

curl -I https://example.com/
curl -I http://example.com/

預期結果:

  • https://example.com/ 回傳 200/301 或其他正常回應
  • http://example.com/ 以 301 轉址至 https

憑證鏈驗證(openssl) #

echo | openssl s_client -connect example.com:443 -servername example.com -showcerts

瀏覽器確認 #

  • 未出現憑證錯誤
  • 憑證詳細資訊顯示的核發者/到期日/SAN 等資訊與預期相符

常見疑難排解 #

明明設定正確卻顯示為「不受信任」 #

  • 可能是未提供中介憑證(CA Bundle),或未將其納入 fullchain 之中。
  • 請確認 SSLCertificateFile 是否指向 fullchain。

Apache 啟動/重新載入時發生 SSL 錯誤 #

  • 可能是私密金鑰路徑錯誤、權限不適當,或金鑰與憑證不相符。
  • 請重新執行「步驟 4」中的相符性確認。

發生轉址迴圈(Redirect Loop) #

  • 可能是 WordPress 端(siteurl/home)與 Apache 端(80 → 443)的設定互相矛盾。
  • 若使用 Cloudflare 等 CDN/反向代理,其 SSL 模式設定也可能造成影響。

回復(Rollback) #

  • 建議在導入前先備份 Apache 網站設定檔與憑證相關檔案。
  • 若發生問題,可停用相關網站並重新載入。
sudo a2dissite example.com.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

維運注意事項(更新/重新核發) #

  • 更新/重新核發的基本流程:建立新的 CSR → 於 CA 端進行重新核發/更新 → 替換 fullchain → 重新載入 Apache
  • 懷疑私密金鑰外洩時:撤銷 → 以新金鑰建立 CSR → 重新核發
  • 請勿修改憑證檔案,並正確保存已核發的資料(PEM)
Updated on 2026年6月9日

What are your feelings

  • Happy
  • Normal
  • Sad

©2020 BESTNET.LLC . All Rights Reserved.