TopBlogWorksWeb DesignGallery
© 2021 ふうねこワーク.

WordPress nginx CentOS7 SSL対応した話

戻る
公開日:2023/3/8
WordPress nginx CentOS7 SSL対応した話

さくらVPSでWordPressを構築して運用しています。
フリーのLet’s Encryptを使用してHTTPS化したので、その時のメモを記載します。

CentOS

7.8

WordPress

5.8.1

nginx

1.18.0

Let’s Encryptで証明書を取得する

certbotを使用します。
certbotは、自動で Let’s Encrypt の証明書を作成してくれます。

certbotをインストールしようとしたら、 「パッケージ certbot は利用できません」エラー が出ました。
snapdをインストールしようとしたら、「パッケージ snapd は利用できません」エラーが出ました。
つまり・・いろいろ足りませんでした。

snapdのインストール

cerbotを入れるにはsnapが必要なのでインストールします。

その前にEpelレポジトリが無効だったので有効にします。

EPELは最も有名なリポジトリ。
インストールしたいパッケージが標準のyumに含まれていない場合やバージョンが古いときに、このリポジトリからインストールします。

有効か無効か確認

sudo yum repolist all | grep -i epel

EPELリポジトリ有効化

sudo yum-config-manager --enable epel
sudo yum repolist epel

EPELを有効

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y

snapdをインストールします。

yum install snapd

certbotのインストール

yum -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の設定


次は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 -e
0 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

WordPressでSSLを有効にする

画面キャプチャを取り忘れましたが…。ダッシュボードのサイトヘルスステータスから進むと有効にするリンクが表示され、有効にできました。

サイトにHTTPSで接続できない

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に接続できるようになりました!

参考にしたサイト様

https://qiita.com/yamada-hakase/items/fdf9c276b9cae51b3633
https://algo-ai.work/2021/04/19/post-1206/
https://worklog.be/archives/3352#i-2

戻る
画像
管理人
ふうねこ
Webエンジニア歴15年。株式会社mimu所属。
Next.jsやTailwindCSSで開発中。
読書・イラスト・英語学習も楽しんでいます。猫好きの朝活派です。


カテゴリー