閉じる

Apache・mod_SSL・Let’s Encrypt・Certbot(Webサーバー間の通信内容の暗号化)

ここでは Let’s Encrypt の無料サーバー証明書を利用します。

[root@server1 ~]# dnf -y install epel-release ← dnf用EPELリポジトリをインストールします。


[root@server1 ~]# dnf -y install snapd ← snapdをインストールします。


[root@server1 ~]# systemctl enable snapd.socket ← snapd.socketを有効化します。


[root@server1 ~]# ln -s /var/lib/snapd/snap /snap ← /var/lib/snapd/snapから/snapへシンボリックリンクをはります。


[root@server1 ~]# snap install –classic certbot ← certbotをインストールします。


[root@server1 ~]# ln -s /snap/bin/certbot /usr/bin/certbot ← /snap/bin/certbotから/usr/bin/certbotへシンボリックリンクをはります。

[root@server1 ~]# certbot certonly --webroot -w ドキュメントルート -m メールアドレス -d Webサーバー名 --agree-tos ← サーバー証明書を取得します。

–パラメータ指定例–
ドキュメントルート: /var/www/html/
メールアドレス: 任意のメールアドレス
Webサーバー名: www.zoah.net

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for centossrv.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/www.zoah.net/fullchain.pem
Key is saved at: /etc/letsencrypt/live/www.zoah.net/privkey.pem
This certificate expires on 2024-01-04.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
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
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

[root@server1 ~]# dnf -y install mod_ssl ← mod_sslをインストールします。

[root@server1 ~]# vi /etc/httpd/conf.d/ssl.conf ← SSL設定ファイルを編集します。

#   Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate.  If
# the certificate is encrypted, then you will be prompted for a
# pass phrase.  Note that a kill -HUP will prompt again.  A new
# certificate can be generated using the genkey(1) command.
SSLCertificateFile /etc/letsencrypt/live/www.zoah.net/cert.pem ← 公開鍵を指定します。

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /etc/letsencrypt/live/www.zoah.net/privkey.pem ← 秘密鍵を指定します。

#   Server Certificate Chain:
#   Point SSLCertificateChainFile at a file containing the
#   concatenation of PEM encoded CA certificates which form the
#   certificate chain for the server certificate. Alternatively
#   the referenced file can be the same as SSLCertificateFile
#   when the CA certificates are directly appended to the server
#   certificate for convinience.
SSLCertificateChainFile /etc/letsencrypt/live/www.zoah.net/chain.pem ← 中間証明書を指定します。

[root@server1 ~]# vi /etc/httpd/conf.d/ssl.conf ← SSL設定ファイルを編集します。

#   SSL Protocol support:
# List the enable protocol levels with which clients will be able to
# connect.  Disable SSLv2 access by default:
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 ← TLS 1.2のみ有効にします。

#   Speed-optimized SSL Cipher configuration:
#   If speed is your main concern (on busy HTTPS servers e.g.),
#   you might want to force clients to specific, performance
#   optimized ciphers. In this case, prepend those ciphers
#   to the SSLCipherSuite list, and enable SSLHonorCipherOrder.
#   Caveat: by giving precedence to RC4-SHA and AES128-SHA
#   (as in the example below), most connections will no longer
#   have perfect forward secrecy - if the server's key is
#   compromised, captures of past or future traffic must be
#   considered compromised, too.
#SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5
#SSLHonorCipherOrder on
SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS ← 追加(使用する暗号化方法を指定します。)
SSLHonorCipherOrder on ← 追加(暗号化方法をサーバー側で決定します。)

Header always set Strict-Transport-Security "max-age=15768000" ← 追加(HSTS=常時HTTPSアクセスを有効化します。)
</VirtualHost>

[root@server1 ~]# vi /etc/httpd/conf.d/ssl.conf ← SSL設定ファイルを編集します。

# Use separate log files for the SSL virtual host; note that LogLevel
# is not inherited from httpd.conf.
ErrorLog logs/error_log ← ログファイル名を変更します。
CustomLog logs/access_log combined ← ログ取得ディレクティブとログファイル名を変更します。
LogLevel warn

[root@server1 ~]# systemctl reload httpd ← Apacheの設定を反映させます。

Let’s Encryptのサーバー証明書の有効期間は3ヶ月の為、自動的にサーバー証明書を更新するようにします。

[root@server1 ~]# vi /var/spool/cron/root

0 5 1 * * /usr/bin/systemctl stop postfix && /usr/bin/systemctl stop dovecot && /usr/bin/systemctl stop httpd
2 5 1 * * /usr/bin/certbot renew --force-renewal
6 5 1 * * /usr/bin/systemctl restart postfix && /usr/bin/systemctl restart dovecot && /usr/bin/systemctl restart httpd

↑ 毎月1日の午前5時に各証明書を更新し、各デーモンをリスタートします。

※--force-renewal は使用しない方が良いので、以下のようにも出来ます。

[root@server1 ~]# vi /var/spool/cron/root

2 5 1,8,15,21,28 * * /usr/bin/certbot renew --pre-hook "/usr/bin/systemctl stop httpd" --post-hook "/usr/bin/systemctl start httpd"
6 5 1,8,15,21,28 * * /usr/bin/systemctl restart postfix && /usr/bin/systemctl restart dovecot

↑ 毎月1日・8日・15日・21日・28日の午前5時に各証明書を更新し、各デーモンをリスタートします。

© 2025 Linuxで自宅サーバー(ホームサーバー)-構築・設定方法- | WordPress テーマ: CrestaProject の Annina Free