●インストール
Nginxをインストールします。
ログは/var/log/nginxになります。
●Let’s EncryptによるSSLサーバー証明書の設定
参考URL:Let’s EncryptによるSSLサーバー証明書の取得、自動更新設定(2021年3月版)
既存Let’s Encryptクライアントのアンインストール
既にサーバーにLet’s Encryptクライアントcertbotをインストール済みの場合、混同しないようアンインストールします。
以下は、インストール先が /usr/local/certbot の場合です。
snapdのインストール
snapdをインストールします。
Let’s Encryptクライアントのインストール
Snapからcertbotコマンドをインストールします。
Let’s Encrypt証明書取得のためのNginxの設定
NginxのConfigで設定を追記します。
www.bigbang.mydns.bz のserverセクションに追記します。
Let’s Encrypt証明書の取得
certbotコマンドのサブコマンド certonly で、証明書を取得します。
オプションは下記のとおりです。
証明書の取得にどれくらい時間がかかるか知りたい場合は、Linuxのtimeコマンドをつけて実行するとよいでしょう。
発行された証明書ファイル群を確認します。
NginxのHTTPS設定
www.bigbang.mydns.bz のTCP/443のserverセクション内で、証明書+中間証明書、秘密鍵として、Let’s Encryptで取得した各ファイルを指定します。
また、Webブラウザの証明書情報を確認します。
Nginxの常時SSL設定
Webサイトを常時SSL化する場合は、HTTP用のTCP/80のセクションでリダイレクトの設定を行います。
Let’s Encryptの認証も、HTTPSにリダイレクトされて実施されるので、Let’s Encrypt認証用の設定を、HTTPS用のTCP/443のセクションに追記します。
以上で、Let’s Encryptを利用したSSLサーバー証明書の取得と設定は完了です。
●SSLサーバー証明書の自動更新設定
Let’s Encrypt証明書の有効期間は90日と限定されており、期限が切れる前に更新する必要があります。
※Let’s Encryptの証明書は、デフォルトでは、有効期限まで30日未満のときのみ更新可能です。有効期限まで30日以上のときは、コマンドを実行しても、証明書を更新しません。
毎回手作業で更新するのは手間がかかるので、自動更新する設定を行います。
証明書の更新、更新エラーが発生したときのアラートメール通知といった一連の処理をシェルスクリプトにまとめ、cronで定期的に実行するようにします。
証明書の更新は、certbotコマンドのサブコマンド renew を使用します。
これは、certonlyサブコマンドで証明書を取得したときに、ドメインごとのConfigファイル /etc/letsencrypt/renewal/<ドメイン>.conf に、取得実行時のオプションが保存されており、renewサブコマンドによる更新実行時は、そのConfigを参照するからです。
Configファイルの内容は以下のような感じです。
動作確認のため、–dry-runオプションをつけて、証明書の更新をテスト実行してみます。
このとき、–post-hookで指定したnginxの再起動が発生しますので注意してください。
cronの定期実行設定
証明書の更新、更新エラーが発生したときのアラートメール通知といった一連の処理を行うシェルスクリプトを設置します。
WEBSERVER_RESTART_CMD で、–post-hookに渡す、Webサーバーソフトウェアを再起動するコマンドを指定しています。
MAILTOでは、アラートメールの通知先アドレスを指定します。
また、サーバー側ではmailコマンドでインターネット上にメールが送信できることを前提としています。
シェルスクリプトを実行する前に、renewコマンドでは、証明書は有効期限まで残り30日未満にならないと更新されないため、強制的に更新するよう、–force-renewalオプションを追加します。
「Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/ssltest2021.inaba-serverdesign.jp/fullchain.pem (success)」
のように成功したメッセージが出力されればOKです。
また、Webブラウザの証明書情報を確認します。
Nginxプロセスが再起動されたことを、psコマンドで確認します。
cronの定期実行設定
証明書を毎月1回実行するよう/etc/cron.monthly/配下に保存します。
Snap版certbotの証明書自動更新機能
実は、Snap版certbotをインストールすると、証明書自動更新機能も合わせてインストールされます。
CentOS 7であれば、systemdのタイマー機能として設定されます。
systemdで登録されているタイマー機能をリストアップしてみます。
snap.certbot.renew.serviceのユニットファイルを確認します。
/usr/bin/snap run –timer=”00:00~24:00/2″ certbot.renew
が、証明書を更新するコマンドのようです。
ただし、この機能では、証明書を使用するWebサーバーソフトウェアの再起動は行われません。
このため、更新後に自動的に実行されるスクリプトを自分で設定しします。
タイマー設定によれば、毎日11時37分と19時04分に更新を試みるようです。
実際にこの機能で証明書を更新するかどうか不明です。
更新したときは、/var/log/messages に certbot.renew のログが記録されると思います。
現時点では、どのようなオプションで証明書が更新されるのかがわからず、また、強制的に更新を試す方法もわからない(あるかもしれませんが)ので、cronで更新することにします。
その場合は、systemdに登録されているSnap版cerbotの証明書自動更新のタイマーを停止しておきます。
●CGIを利用する
Nginx でCGIを利用できるように、EPELからダウンロードします。
当サイトは常時SSLのため、
●PHPスクリプトを利用する
PHP 7.4をインストールします。
●SSIを利用する
/etc/nginx/nginx.confを編集します。
当サイトは常時SSLのため、下記に追記します。
●プロキシ経由でSSIを実行する
プロキシを経由するとNginx上のSSIが動作しなくなりました。
Nginxをインストールします。
# dnf -y install nginx基本設定をします。
# vi /etc/nginx/nginx.conf # 41行目:サーバー名を自身のものに変更 server_name www.bigbang.mydns.bz; # systemctl enable --now nginxHTTPサービスを許可します。
# firewall-cmd --add-service=http --permanent success # firewall-cmd --reload successEginxのデフォルトのページが表示されればOKです。
ログは/var/log/nginxになります。
●Let’s EncryptによるSSLサーバー証明書の設定
参考URL:Let’s EncryptによるSSLサーバー証明書の取得、自動更新設定(2021年3月版)
既存Let’s Encryptクライアントのアンインストール
既にサーバーにLet’s Encryptクライアントcertbotをインストール済みの場合、混同しないようアンインストールします。
# dnf remove certbotgithubからgit cloneコマンドでインストールした場合は、インストール先ディレクトリと/opt/eff.orgディレクトリを削除します。
以下は、インストール先が /usr/local/certbot の場合です。
# rm -rf /usr/local/certbot/ /opt/eff.org/
snapdのインストール
snapdをインストールします。
# dnf --enablerepo=epel install snapdsnapdのサービス自動起動設定を行いつつ、起動します。 # systemctl enable --now snapd.socket classic snapをサポートするため、シンボリックリンクを作成します。
# ln -s /var/lib/snapd/snap /snapcoreパッケージをインストールします。
# snap install core
error: too early for operation, device not yet seeded or device model not acknowledged
※上記のようなエラーが表示された場合、暫くしてから(数分〜5分程度)再実行してください。
# snap install core
2021-05-12T15:01:57+09:00 INFO Waiting for automatic snapd restart...
Warning: /var/lib/snapd/snap/bin was not found in your $PATH. If you've not restarted your session
since you installed snapd, try doing that. Please see https://forum.snapcraft.io/t/9469
for more details.
core 16-2.49.2 from Canonical✓ installed
coreパッケージを最新バージョンにアップデートします。
# snap refresh core 2021-05-12T15:08:45+09:00 INFO Waiting for automatic snapd restart... core 16-2.50 from Canonical✓ refreshedSnapのバージョンを確認してみます。
# snap --version snap 2.49-2.el8 snapd 2.49-2.el8 series 16 centos 8 kernel 4.18.0-301.1.el8.x86_64
Let’s Encryptクライアントのインストール
Snapからcertbotコマンドをインストールします。
# snap install --classic certbot Warning: /var/lib/snapd/snap/bin was not found in your $PATH. If you've not restarted your session since you installed snapd, try doing that. Please see https://forum.snapcraft.io/t/9469 for more details. certbot 1.15.0 from Certbot Project (certbot-eff✓) installed/snap/bin にインストールされたので、使いやすいように /usr/bin/certbot としてシンボリックリンクを作成。
# ln -s /snap/bin/certbot /usr/bin/certbotcertbotコマンドでバージョンを確認します。 # certbot --version certbot 1.15.0
Let’s Encrypt証明書取得のためのNginxの設定
NginxのConfigで設定を追記します。
www.bigbang.mydns.bz のserverセクションに追記します。
# vi /etc/nginx/nginx.conf server { listen 80; server_name www.bigbang.mydns.bz; root /usr/share/nginx/html; ... # for Let's Encrypt location ^~ /.well-known/ { root /usr/share/nginx/certbot/www.bigbang.mydns.bz; }Nginx Configファイルの文法チェックを行ったうえで、NginxサービスをreloadしてNginxに反映します。
# nginx -t # systemctl reload nginx
Let’s Encrypt証明書の取得
certbotコマンドのサブコマンド certonly で、証明書を取得します。
オプションは下記のとおりです。
- –webroot: Apache/Nginxを起動したままの認証とする。
- -w: 認証時の Documentroot を指定。
- -d: ドメインを指定。
- –preferred-challenges: 認証方式を指定する。ここではHTTP認証とする。
- –agree-tos: 利用規約の同意し、確認画面を表示しない。
- -m: Let’s Encryptに登録するメールアドレス。登録したくなければ、代わりに –register-unsafely-without-email を指定すればよい。
証明書の取得にどれくらい時間がかかるか知りたい場合は、Linuxのtimeコマンドをつけて実行するとよいでしょう。
# time /usr/bin/certbot certonly \ > --webroot \ > -w /usr/share/nginx/certbot/www.bigbang.mydns.bz \ > -d www.bigbang.mydns.bz \ > --preferred-challenges http \ > --agree-tos \ > -m shiba@mail.bigbang.mydns.bz Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator webroot, Installer None - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing, once your first certificate is successfully issued, to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: y Account registered. Requesting a certificate for www.bigbang.mydns.bz Performing the following challenges: http-01 challenge for www.bigbang.mydns.bz Using the webroot path /usr/share/nginx/certbot/www.bigbang.mydns.bz for all unmatched domains. Waiting for verification... Cleaning up challenges Subscribe to the EFF mailing list (email: shiba@mail.bigbang.mydns.bz). IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/www.bigbang.mydns.bz/privkey.pem Your certificate will expire on 2021-08-13. 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 real 0m30.641s user 0m0.817s sys 0m0.086s下記のように表示されていれば証明書の取得は成功です。
- Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem Your key file has been saved at:〜失敗した場合は、エラーメッセージや、デバッグログ /var/log/letsencrypt/letsencrypt.log を参照して、不具合箇所を調査、解消します。
発行された証明書ファイル群を確認します。
# ls -l /etc/letsencrypt/live/www.bigbang.mydns.bz/ 合計 4 -rw-r--r--. 1 root root 692 5月 15 23:55 README lrwxrwxrwx. 1 root root 44 5月 15 23:55 cert.pem -> ../../archive/www.bigbang.mydns.bz/cert1.pem lrwxrwxrwx. 1 root root 45 5月 15 23:55 chain.pem -> ../../archive/www.bigbang.mydns.bz/chain1.pem lrwxrwxrwx. 1 root root 49 5月 15 23:55 fullchain.pem -> ../../archive/www.bigbang.mydns.bz/fullchain1.pem lrwxrwxrwx. 1 root root 47 5月 15 23:55 privkey.pem -> ../../archive/www.bigbang.mydns.bz/privkey1.pem各ファイルの意味は下記のとおりです。
- 証明書:/etc/letsencrypt/live/www.bigbang.mydns.bz/cert.pem
- 中間証明書:/etc/letsencrypt/live/www.bigbang.mydns.bz/chain.pem
- 秘密鍵:/etc/letsencrypt/live/www.bigbang.mydns.bz/privkey.pem
- 証明書+中間証明書:/etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem
# openssl x509 -text -noout -in /etc/letsencrypt/live/www.bigbang.mydns.bz/cert.pem Certificate: Data: Version: 3 (0x2) Serial Number: 04:85:77:93:0d:51:98:0f:11:34:1b:41:1a:5f:75:d8:a4:ba Signature Algorithm: sha256WithRSAEncryption Issuer: C = US, O = Let's Encrypt, CN = R3 Validity Not Before: May 15 13:55:40 2021 GMT Not After : Aug 13 13:55:40 2021 GMT Subject: CN = www.bigbang.mydns.bz Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) ・・・ X509v3 Subject Alternative Name: DNS:www.bigbang.mydns.bz ・・・Not Before(発行日)、Not After(有効期限)、CN(Common Name)、DNS(Alternative Name)などを確認します。
NginxのHTTPS設定
www.bigbang.mydns.bz のTCP/443のserverセクション内で、証明書+中間証明書、秘密鍵として、Let’s Encryptで取得した各ファイルを指定します。
# vi /etc/nginx/nginx.conf -- server { listen 443 ssl http2; server_name www.bigbang.mydns.bz; root /usr/share/nginx/html; ssl_certificate /etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.bigbang.mydns.bz/privkey.pem; ssl_session_cache shared:SSL:1m; ssl_session_timeout 10m; ssl_ciphers PROFILE=SYSTEM; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } --Nginx Configファイルの文法チェックを行ったうえで、Nginxサービスを再起動してNginxに反映します。
# nginx -t # systemctl restart nginxNginxの設定を反映したのち、WebブラウザでWebサイト(https://www.bigbang.mydns.bz/)にHTTPSでアクセスして、正しくページが表示されることを確認します。
また、Webブラウザの証明書情報を確認します。
Nginxの常時SSL設定
Webサイトを常時SSL化する場合は、HTTP用のTCP/80のセクションでリダイレクトの設定を行います。
Let’s Encryptの認証も、HTTPSにリダイレクトされて実施されるので、Let’s Encrypt認証用の設定を、HTTPS用のTCP/443のセクションに追記します。
# vi /etc/nginx/nginx.conf -- server { listen 80; ... # Redirect to HTTPS return 301 https://$host$request_uri; ... server { listen 443 ssl http2; ... # for Let's Encrypt location ^~ /.well-known/ { root /usr/share/nginx/certbot/www.bigbang.mydns.bz; } ... } --Nginx Configファイルの文法チェックを行ったうえで、NginxサービスをreloadしてNginxに反映します。
# nginx -t # systemctl reload nginx/Nginxの設定を反映したのち、WebブラウザでWebサイト(http://www.bigbang.mydns.bz/)にHTTPでアクセスして、正しくHTTPSページにリダイレクトされることを確認します。
以上で、Let’s Encryptを利用したSSLサーバー証明書の取得と設定は完了です。
●SSLサーバー証明書の自動更新設定
Let’s Encrypt証明書の有効期間は90日と限定されており、期限が切れる前に更新する必要があります。
※Let’s Encryptの証明書は、デフォルトでは、有効期限まで30日未満のときのみ更新可能です。有効期限まで30日以上のときは、コマンドを実行しても、証明書を更新しません。
毎回手作業で更新するのは手間がかかるので、自動更新する設定を行います。
証明書の更新、更新エラーが発生したときのアラートメール通知といった一連の処理をシェルスクリプトにまとめ、cronで定期的に実行するようにします。
証明書の更新は、certbotコマンドのサブコマンド renew を使用します。
# /usr/bin/certbot renew --post-hook "systemctl restart httpd"オプションは下記のとおりです。
これは、certonlyサブコマンドで証明書を取得したときに、ドメインごとのConfigファイル /etc/letsencrypt/renewal/<ドメイン>.conf に、取得実行時のオプションが保存されており、renewサブコマンドによる更新実行時は、そのConfigを参照するからです。
Configファイルの内容は以下のような感じです。
# cat /etc/letsencrypt/renewal/www.bigbang.mydns.bz.conf # renew_before_expiry = 30 days version = 1.15.0 archive_dir = /etc/letsencrypt/archive/www.bigbang.mydns.bz cert = /etc/letsencrypt/live/www.bigbang.mydns.bz/cert.pem privkey = /etc/letsencrypt/live/www.bigbang.mydns.bz/privkey.pem chain = /etc/letsencrypt/live/www.bigbang.mydns.bz/chain.pem fullchain = /etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem # Options used in the renewal process [renewalparams] account = ******************************** pref_challs = http-01, authenticator = webroot webroot_path = /usr/share/nginx/certbot/www.bigbang.mydns.bz, server = https://acme-v02.api.letsencrypt.org/directory [[webroot_map]] www.bigbang.mydns.bz = /usr/share/nginx/certbot/www.bigbang.mydns.bz認証方式や認証時の ocument Rootなどを変更したい場合は、このConfigファイルを修正してください。
動作確認のため、–dry-runオプションをつけて、証明書の更新をテスト実行してみます。
このとき、–post-hookで指定したnginxの再起動が発生しますので注意してください。
# /usr/bin/certbot renew --post-hook "systemctl restart nginx" --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.bigbang.mydns.bz.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Account registered.
Simulating renewal of an existing certificate for www.bigbang.mydns.bz
Performing the following challenges:
http-01 challenge for www.bigbang.mydns.bz
Using the webroot path /usr/share/nginx/certbot/www.bigbang.mydns.bz for all unmatched domains.
Waiting for verification...
Cleaning up challenges
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: systemctl restart nginx
cronの定期実行設定
証明書の更新、更新エラーが発生したときのアラートメール通知といった一連の処理を行うシェルスクリプトを設置します。
WEBSERVER_RESTART_CMD で、–post-hookに渡す、Webサーバーソフトウェアを再起動するコマンドを指定しています。
MAILTOでは、アラートメールの通知先アドレスを指定します。
また、サーバー側ではmailコマンドでインターネット上にメールが送信できることを前提としています。
# vi /root/cron/update_sslcert.sh #!/bin/bash # CERTBOT_CMD=/usr/bin/certbot WEBSERVER_RESTART_CMD="systemctl restart nginx" MAILTO=<メール通知先アドレス> echo "===== Update SSL Certfile =====" echo "`date` Update SSL Certfile start" # 証明書の更新 ${CERTBOT_CMD} renew --post-hook "${WEBSERVER_RESTART_CMD}" LE_STATUS=$? # 証明書の取得に失敗したときはメールで通知 if [ "$LE_STATUS" != 0 ]; then echo "Update SSL Certfile failed" |mail -s "Update SSL Certfile in `hostname`" ${MAILTO} fi echo "`date` Update SSL Certfile end" # EOF実行権限を付与します。
# chmod +x /root/bin/update_sslcert.sh続いて、このシェルスクリプトの動作確認を行います。
シェルスクリプトを実行する前に、renewコマンドでは、証明書は有効期限まで残り30日未満にならないと更新されないため、強制的に更新するよう、–force-renewalオプションを追加します。
# vi /root/bin/update_sslcert.sh ※変更前 ${CERTBOT_CMD} renew --post-hook "${WEBSERVER_RESTART_CMD}" ※変更後 ${CERTBOT_CMD} renew --post-hook "${WEBSERVER_RESTART_CMD}" --force-renewalシェルスクリプトを実行します。
「Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/ssltest2021.inaba-serverdesign.jp/fullchain.pem (success)」
のように成功したメッセージが出力されればOKです。
# /root/cron/update_sslcert.sh
===== Update SSL Certfile =====
2021年 5月 17日 月曜日 14:31:08 JST Update SSL Certfile start
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.bigbang.mydns.bz.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate for www.bigbang.mydns.bz
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all renewals succeeded:
/etc/letsencrypt/live/www.bigbang.mydns.bz/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: systemctl restart nginx
2021年 5月 17日 月曜日 14:31:12 JST Update SSL Certfile end
WebブラウザでWebサイト(https://www.bigbang.mydns.bz/)にアクセスして、正しくページが表示されることを確認します。
また、Webブラウザの証明書情報を確認します。
Nginxプロセスが再起動されたことを、psコマンドで確認します。
# ps aux|grep nginx root 38302 0.0 0.0 120308 2272 ? Ss 14:31 0:00 nginx: master process /usr/sbin/nginx nginx 38303 0.0 0.0 153376 11740 ? S 14:31 0:00 nginx: worker process nginx 38304 0.0 0.0 152916 8052 ? S 14:31 0:00 nginx: worker process nginx 38305 0.0 0.0 152916 8076 ? S 14:31 0:00 nginx: worker process nginx 38306 0.0 0.0 152916 8100 ? S 14:31 0:00 nginx: worker process root 38436 0.0 0.0 5516 932 pts/1 S+ 14:33 0:00 tail -f /var/log/nginx/access.log root 39097 0.0 0.0 10308 1072 pts/0 S+ 14:47 0:00 grep --color=auto nginx証明書が更新されたことを確認します。
# ls -l /etc/letsencrypt/live/www.bigbang.mydns.bz/ -rw-r--r--. 1 root root 692 5月 15 23:55 README lrwxrwxrwx. 1 root root 44 5月 17 14:31 cert.pem -> ../../archive/www.bigbang.mydns.bz/cert2.pem lrwxrwxrwx. 1 root root 45 5月 17 14:31 chain.pem -> ../../archive/www.bigbang.mydns.bz/chain2.pem lrwxrwxrwx. 1 root root 49 5月 17 14:31 fullchain.pem -> ../../archive/www.bigbang.mydns.bz/fullchain2.pem lrwxrwxrwx. 1 root root 47 5月 17 14:31 privkey.pem -> ../../archive/www.bigbang.mydns.bz/privkey2.pem # ls -l /etc/letsencrypt/archive/www.bigbang.mydns.bz/
cronの定期実行設定
証明書を毎月1回実行するよう/etc/cron.monthly/配下に保存します。
# cp -p /root/cron/update_sslcert.sh /etc/cron.monthly/ # systemctl reload crond
Snap版certbotの証明書自動更新機能
実は、Snap版certbotをインストールすると、証明書自動更新機能も合わせてインストールされます。
CentOS 7であれば、systemdのタイマー機能として設定されます。
systemdで登録されているタイマー機能をリストアップしてみます。
# systemctl list-timers NEXT LEFT LAST PASSED UNIT ACTIVATES Mon 2021-05-17 19:04:00 JST 3h 32min left Mon 2021-05-17 11:37:18 JST 3h 53min ago snap.certbot.renew.timer snap.certbot.renew.service Tue 2021-05-18 07:37:18 JST 16h left Mon 2021-05-17 07:37:18 JST 7h ago systemd-tmpfiles-clean.timer ssystemd-tmpfiles-clean.serviceタイマーで、snap.certbot.renew.serviceを起動することがわかります。
snap.certbot.renew.serviceのユニットファイルを確認します。
# cat /etc/systemd/system/snap.certbot.renew.service # Auto-generated, DO NOT EDIT Description=Timer renew for snap application certbot.renew Requires=var-lib-snapd-snap-certbot-1150.mount After=var-lib-snapd-snap-certbot-1150.mount X-Snappy=yes [Timer] Unit=snap.certbot.renew.service OnCalendar=*-*-* 11:37 OnCalendar=*-*-* 19:04 [Install] WantedBy=timers.target [root@nezumi ~]# cat /etc/systemd/system/snap.certbot.renew.service [Unit] # Auto-generated, DO NOT EDIT Description=Service for snap application certbot.renew Requires=var-lib-snapd-snap-certbot-1150.mount Wants=network.target After=var-lib-snapd-snap-certbot-1150.mount network.target snapd.apparmor.service X-Snappy=yes [Service] EnvironmentFile=-/etc/environment ExecStart=/usr/bin/snap run --timer="00:00~24:00/2" certbot.renew SyslogIdentifier=certbot.renew Restart=no WorkingDirectory=/var/snap/certbot/1150 TimeoutStopSec=30 Type=oneshot [Install] WantedBy=multi-user.targetExecStart行の
/usr/bin/snap run –timer=”00:00~24:00/2″ certbot.renew
が、証明書を更新するコマンドのようです。
ただし、この機能では、証明書を使用するWebサーバーソフトウェアの再起動は行われません。
このため、更新後に自動的に実行されるスクリプトを自分で設定しします。
# vi /etc/letsencrypt/renewal-hooks/post/web_restart.sh #!/bin/bash systemctl restart nginx # chmod +x /etc/letsencrypt/renewal-hooks/post/web_restart.sh上記のSnap版certbotの証明書自動更新機能を使用する場合は、cronを使用しての設定手順は不要となります。
タイマー設定によれば、毎日11時37分と19時04分に更新を試みるようです。
実際にこの機能で証明書を更新するかどうか不明です。
更新したときは、/var/log/messages に certbot.renew のログが記録されると思います。
現時点では、どのようなオプションで証明書が更新されるのかがわからず、また、強制的に更新を試す方法もわからない(あるかもしれませんが)ので、cronで更新することにします。
その場合は、systemdに登録されているSnap版cerbotの証明書自動更新のタイマーを停止しておきます。
# systemctl stop snap.certbot.renew.timer # systemctl disable snap.certbot.renew.timer
●CGIを利用する
Nginx でCGIを利用できるように、EPELからダウンロードします。
# dnf --enablerepo=epel -y install fcgiwrap # vi /etc/nginx/fcgiwrap.conf location /cgi-bin/ { gzip off; root /usr/share/nginx; fastcgi_pass unix:/var/run/fcgiwrap.socket; include /etc/nginx/fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } # mkdir /usr/share/nginx/cgi-bin # chmod 755 /usr/share/nginx/cgi-bin設定をしたいサイト定義の [server] セクション内に追記します。
当サイトは常時SSLのため、
# vi /etc/nginx/nginx.conf server { listen 443 ssl http2; ... # CGIを有効にするための設定 include fcgiwrap.conf; ... } # systemctl restart nginxFastCGIWrapサービス用のSystemdファイルを新規作成して起動します。
# vi /usr/lib/systemd/system/fcgiwrap.service [Unit] Description=Simple CGI Server After=nss-user-lookup.target Requires=fcgiwrap.socket [Service] EnvironmentFile=/etc/sysconfig/fcgiwrap ExecStart=/usr/sbin/fcgiwrap ${DAEMON_OPTS} -c ${DAEMON_PROCS} User=nginx Group=nginx [Install] Also=fcgiwrap.socket # vi /usr/lib/systemd/system/fcgiwrap.socket [Unit] Description=fcgiwrap Socket [Socket] ListenStream=/run/fcgiwrap.socket [Install] WantedBy=sockets.target # systemctl enable --now fcgiwrapCGIテストスクリプトを作成します。
# vi /usr/share/nginx/cgi-bin/test.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html><head><title>CGI-Perl TEST</title>\n"; print "<meta http-equiv='content-type' content='text/html; charset=utf-8'>\n"; print "</head><body><h1>CGI-Perlテスト</h1>\n"; print "</body></html>\n"; exit; # chmod +x /usr/share/nginx/cgi-bin/test.cgi ページ表示確認後、削除 # rm -f /usr/share/nginx/cgi-bin/test.cgi
●PHPスクリプトを利用する
PHP 7.4をインストールします。
# dnf module list php CentOS Stream 8 - AppStream Name Stream Profiles Summary php 7.2 [d] common [d], devel, minimal PHP scripting language php 7.3 common [d], devel, minimal PHP scripting language php 7.4 [e] common [d] [i], devel, minimal PHP scripting language Remi's Modular repository for Enterprise Linux 8 - x86_64 Name Stream Profiles Summary php remi-7.2 common [d], devel, minimal PHP scripting language php remi-7.3 common [d], devel, minimal PHP scripting language php remi-7.4 common [d], devel, minimal PHP scripting language php remi-8.0 common [d], devel, minimal PHP scripting language ヒント: [d]efault, [e]nabled, [x]disabled, [i]nstalled # dnf module install php:7.4 -yコマンドでバージョンを確認します。
# php -v PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend TechnologiesPHPスクリプトを作成します。
# vi /usr/share/nginx/html/test.php <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>PHP TEST</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <h2>PHPテスト</h2> <?php print Date("y/m/d H:i:s"); ?> <hr> <?php phpinfo(); ?> </body> </html> ページ表示確認後、削除 # rm -f /usr/share/nginx/cgi-bin/test.cgi
●SSIを利用する
/etc/nginx/nginx.confを編集します。
当サイトは常時SSLのため、下記に追記します。
http { server { listen 443 ssl http2 default_server; server_name www.bigbang.mydns.bz; root /usr/share/nginx/html; ... location / { ssi on; ssi_last_modified on; } ... }
●プロキシ経由でSSIを実行する
プロキシを経由するとNginx上のSSIが動作しなくなりました。
2021/05/24 07:32:20 [error] 149107#0: *462 invalid SSI command: "exec" while sending response to client, \client: 192.168.0.1, server: www.bigbang.mydns.bz, request: "GET / HTTP/2.0", host: "www.bigbang.mydns.bz" 2021/05/24 07:32:20 [error] 149107#0: *462 invalid SSI command: "exec" while sending response to client, \ client: 192.168.0.1, server: www.bigbang.mydns.bz, request: "GET / HTTP/2.0", host: "www.bigbang.mydns.bz"下記のように書き換えます。
<!--#exec cgi="cgi-bin/uptime.cgi"--> ↓↓↓ <!--#include virtual="cgi-bin/uptime.cgi"-->これにより解決しました。