0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【RockyLinux 9.5 で WordPress 環境構築】DNS設定とCertbotによるSSL証明書の取得と設定

Last updated at Posted at 2025-02-21

RockyLinux9.5でWordPress環境構築した際のメモです。
今回はDNS設定とCertbotによるSSL証明書の取得と設定を行います。

DNS設定

新規サイトであれば、ドメインを取得後にDNSレコードにサーバーのIPアドレスを設定してください。既存サイトからの移行の場合、既存のDNSレコードのTTLを60など短く設定しておくことで切替がスムーズにいきます。

hostsファイル設定

クライアント端末のhostファイルを編集することで、DNSを設定していないサーバーにドメインでアクセスすることができます。

hostsファイルの設定を行う前に、WordPressの「設定 > 一般」 の「WordPress アドレス (URL)」と「サイトアドレス (URL)」をドメイン版のURLに変更します。設定変更時にエラーとなりますが、hostsファイル変更後に正しくアクセスできます。

MacOSなどUnix系の端末の場合は以下のファイルを編集します。

sudo vim /etc/hosts

Windowsの場合は以下のファイルを管理者権限のメモ帳で編集します。

C:\Windows\System32\drivers\etc\hosts

以下のように「IPアドレス ドメイン」を追記します。

hosts
xxx.xxx.xxx.xxx yoursite.com

指定したドメイン名で新しいサイトにアクセスできます。ドメインでアクセスしたことによりWordPressの動作に問題がある場合は、この設定をコメントアウトしてIPアドレスでアクセスしなおして原因を探ってください。

Certbotのインストール

Let's EncryptのSSL証明書を取得するため、Certbotのインストールを行います。

Snapdをパッケージをインストールする

sudo dnf install snapd
sudo dnf install epel-release snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap

Snapdを利用してcertbotをインストールする

sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --version

Certbot を使用して DNS-01 チャレンジを指定

SSL証明書を取得します。以下のコマンドを実行すると、CertbotがTXTレコードの値を指示するので、それをDNSに設定します。TXTレコードのTTLは最低値60に設定しておきます。

DNSの設定反映を待ってからEnterキーを押します。

sudo certbot certonly --manual --preferred-challenges dns -d yoursite.com

SSL証明書が作成されたか確認。

sudo ls -la /etc/letsencrypt/live/yoursite.com/fullchain.pem
sudo ls -la /etc/letsencrypt/live/yoursite.com/privkey.pem

ssl.confの設定

オリジナルをバックアップ

sudo cp /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.org

vim で ssl.conf を編集

sudo vim /etc/httpd/conf.d/ssl.conf
/etc/httpd/conf.d/ssl.conf
#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/letsencrypt/live/yoursite.com/fullchain.pem

#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/letsencrypt/live/yoursite.com/privkey.pem

設定に問題がないか確認

sudo httpd -t 

httpdの再起動

sudo systemctl restart httpd

https://でWordPressサイトにアクセスできるか確認します。
問題がなければ、これにて構築は一段落です。

WordPressのテーマ設定や一般設定などが一通り完了し公開できる状態となったら次へ。

DNSの切り替えとSSL証明書の再設定

DNSの切り替えを行います。

DNSの切り替え完了後、SSL証明書の再取得と自動更新設定を行います。
DNS-01チャレンジで取得した証明書は自動更新がエラーとなるためです。

DNSの切り替えが完了後、webrootオプションで証明書を再取得します。

sudo certbot certonly --webroot -w /var/www/html/yoursite.com -m you@yoursite.com -d yoursite.com -n --agree-tos  --force-renewal

httpdを再起動。

sudo systemctl restart httpd
sudo certbot certificates

Snap 版の Certbot は自動的に certbot renew を実行するようになっています。
以下のコマンドで Snap の自動更新が有効かどうかを確認してください。

systemctl list-timers --all | grep snap.certbot.renew

以下のコマンドで証明書を手動で更新できます。

sudo certbot renew --dry-run

もし有効になっていない場合、以下のコマンドで手動実行を設定します。

sudo snap set certbot trust-plugin-with-root=ok
sudo snap get certbot

以上、お疲れ様でした!

RockyLinux9.5でWordPress環境構築

  1. さくらのVPS設定
  2. Apacheのインストールと設定
  3. PHPのインストールと設定
  4. mariaDBのインストールと設定
  5. WordPressのインストールと設定
  6. DNS設定とCertbotによるSSL証明書の取得と設定
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?