
さくらVPSでWordPressを構築して運用しています。
フリーのLet’s Encryptを使用してHTTPS化したので、その時のメモを記載します。
CentOS | 7.8 |
WordPress | 5.8.1 |
nginx | 1.18.0 |
certbotを使用します。
certbotは、自動で Let’s Encrypt の証明書を作成してくれます。
certbotをインストールしようとしたら、 「パッケージ certbot は利用できません」エラー が出ました。
snapdをインストールしようとしたら、「パッケージ snapd は利用できません」エラーが出ました。
つまり・・いろいろ足りませんでした。
cerbotを入れるにはsnapが必要なのでインストールします。
その前にEpelレポジトリが無効だったので有効にします。
EPELは最も有名なリポジトリ。
インストールしたいパッケージが標準のyumに含まれていない場合やバージョンが古いときに、このリポジトリからインストールします。
有効か無効か確認
sudo yum repolist all | grep -i epelEPELリポジトリ有効化
sudo yum-config-manager --enable epel
sudo yum repolist epelEPELを有効
sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -ysnapdをインストールします。
yum install snapdyum -y install certbot証明書を発行します。発行する前にnginxは止めておいてください。
systemctl stop nginx
certbot certonly --standalone -d xxx.xxx(ドメイン) -m メールアドレス --agree-tos -nサブドメインを後から追加したので。
–expandが追加。
certbot certonly --standalone --agree-tos --expand -d xxx.xxx(ドメイン) -d xx.xxx.xxx(サブドメイン) -n
systemctl start nginxこれで証明書が発行されました!作成完了すると以下のようなメッセージが表示されます。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Account registered.
Requesting a certificate for fuuu.work
Performing the following challenges:
http-01 challenge for fuuu.work
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/***.***/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/***.***/privkey.pem
Your certificate will expire on 2022-01-20. To obtain a new or
tweaked version of this certificate in the future, simply run
certbot again. To non-interactively renew *all* of your
certificates, run "certbot renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
次はnginxの設定を行います。
view /etc/nginx/conf.d/**.conf(**は任意の名前) server {
listen 80;
server_name ***.***;
return 301 https://$host$request_uri;
}
server{
listen 443 ssl;
server_name ***.***;
ssl_certificate /etc/letsencrypt/live/***.***/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/***.***/privkey.pem;
赤文字は証明書を発行したときに表示されたメッセージの中に記載あるので、それをコピペします。
httpで接続されたときはhttpsにリダイレクトするように設定しています。
証明書の有効期限が切れたら、証明書の更新が必要になってきます。
これをcronで設定して、自動更新できるようにします。もし、有効期限が来てなかったら何もしません。–post-hookは証明書の更新をした場合に後述のコマンドを実行するというオプションです。なので、証明書の更新がなければ、nginxの再起動も行いません。
ここでは1日と20日の10時に証明書の更新をするよう設定しています。
crontab -e0 10 1 * * certbot renew --post-hook "systemctl reload nginx" > /dev/null 2>&1
0 10 20 * * certbot renew --post-hook "systemctl reload nginx" > /dev/null 2>&1画面キャプチャを取り忘れましたが…。ダッシュボードのサイトヘルスステータスから進むと有効にするリンクが表示され、有効にできました。
httpsのURLで接続すると、応答なしになってしまいました。
結果、ファイアウォールでした。80番ポートは許可していましたが、443番ポートを許可していなかったためでした。
以下のコマンドでhttpsが許可されているか確認
firewall-cmd --list-all赤文字のhttpsが入ってなかったら追加する
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ftp http https ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:httpsを追加する
firewall-cmd --add-service=https --zone=public --permanentファイアウォールを再起動
systemctl restart firewalldこれでhttpsに接続できるようになりました!