2026年5月20日
2026年5月20日
WordPressのSSLをインストールする方法(Let’s Encrypt)
はじめに
Let's EncryptはSSL証明書を無料で発行できるサービスです。CertbotというツールでApache/Nginxに自動インストールでき、90日ごとの自動更新も設定できます。有料SSL証明書と同等のHTTPS暗号化をコストゼロで実現できます。
症状・原因
- WordPressがHTTPのままでGoogleから「保護されていない通信」と表示される
- SSL証明書を設置したいが費用をかけたくない
- 既存の有料証明書の期限が切れたので無料に切り替えたい
- ホスティング会社のSSLパネルから設定できない環境
解決手順
ステップ1:Certbotをインストールする
# Ubuntu/Debian の場合
sudo apt update
sudo apt install -y certbot python3-certbot-apache
# Nginx の場合
sudo apt install -y certbot python3-certbot-nginx
# CentOS/RHEL の場合
sudo dnf install -y certbot python3-certbot-apache
# Nginx の場合
sudo dnf install -y certbot python3-certbot-nginx
# Certbot のバージョン確認
certbot --version
ステップ2:SSL証明書を取得する
# Apache の場合(自動設定)
sudo certbot --apache -d example.com -d www.example.com
# Nginx の場合(自動設定)
sudo certbot --nginx -d example.com -d www.example.com
# Webサーバーに依存しない方法(証明書のみ取得)
sudo certbot certonly --webroot \
-w /var/www/html \
-d example.com \
-d www.example.com \
--email admin@example.com \
--agree-tos \
--no-eff-email
# 証明書の保存場所を確認
sudo ls -la /etc/letsencrypt/live/example.com/
# fullchain.pem: 証明書+中間証明書
# privkey.pem: 秘密鍵
# cert.pem: 証明書のみ
# chain.pem: 中間証明書のみ
ステップ3:Apache/Nginx にSSLを設定する
# /etc/apache2/sites-available/example.com-ssl.conf
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
# TLS 1.2以降のみ許可
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:...
SSLHonorCipherOrder off
# HTTP → HTTPS リダイレクト(HTTP側VirtualHostに設定)
# <VirtualHost *:80> で以下を設定:
# Redirect permanent / https://example.com/
</VirtualHost>
# /etc/nginx/sites-available/example.com
server {
listen 443 ssl http2;
server_name example.com www.example.com;
root /var/www/html;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# 推奨TLS設定
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
# HSTS(一度有効にすると元に戻しにくいので注意)
add_header Strict-Transport-Security "max-age=31536000" always;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
ステップ4:WordPressをHTTPSに切り替える
# WordPress の URL 設定を HTTPS に変更
wp option update siteurl 'https://example.com'
wp option update home 'https://example.com'
# wp-config.php に HTTPS 強制設定を追加
wp config set FORCE_SSL_ADMIN true --raw
# Mixed Content(HTTPリソース)を一括置換
wp search-replace 'http://example.com' 'https://example.com' \
--skip-columns=guid \
--report-changed-only
# キャッシュをクリア
wp cache flush
wp rewrite flush
// wp-config.php: リバースプロキシ環境でのSSL強制
// Cloudflare やロードバランサーを使っている場合
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
&& $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}
define('FORCE_SSL_ADMIN', true);
define('WP_HOME', 'https://example.com');
define('WP_SITEURL', 'https://example.com');
ステップ5:自動更新を設定する
# Certbot の自動更新テスト
sudo certbot renew --dry-run
# crontab で自動更新(月2回実行推奨)
sudo crontab -e
# 以下を追加:
# 0 3 1,15 * * certbot renew --quiet && systemctl reload apache2
# または systemd timer を使用(Ubuntu 推奨)
sudo systemctl status certbot.timer
sudo systemctl enable certbot.timer
# 更新後のWebサーバー再起動フックを設定
# /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh
cat > /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh << 'EOF'
#!/bin/bash
systemctl reload apache2
EOF
chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh
注意事項
- Let's Encrypt の証明書は90日間有効です。自動更新の設定を忘れると証明書が期限切れになり、訪問者に警告が表示されます
- HSTS(
Strict-Transport-Security)を設定すると、有効期間中はHTTPへの切り替えができなくなります。まず短いmax-age(例: 300秒)でテストしてから長い期間に変更してください - ワイルドカード証明書(
*.example.com)はDNS認証が必要で、certonly --manualまたは対応するDNSプラグインが必要です
まとめ
Let's EncryptによるSSL設置は①apt install certbot python3-certbot-apacheでCertbotインストール、②certbot --apache -d example.comで証明書取得・Apache設定を自動化、③wp option update siteurl 'https://...'とsearch-replaceでWordPressをHTTPS化、④wp-config.phpにFORCE_SSL_ADMIN trueを追加、⑤certbot renew --dry-runで自動更新の動作確認、の順で実施します。