Let's Encryptについての情報は他のサイト等沢山あるのですが、バーチャルホストを使わずに単独でapache2のサイトを立ち上げるのに少し手間取ったので備忘録として挙げてみることにしました。
2024/12/06追記:apache2のインストールの前に、dnsで対象サーバが正引き出来るようにしておいてください。
続いて最小限のconfを/etc/apache2/sites-available/exmaple.com.conf に記述します。
続いて証明書を発行するツールをインストールします。
2024/12/06追記:apache2のインストールの前に、dnsで対象サーバが正引き出来るようにしておいてください。
続いて最小限のconfを/etc/apache2/sites-available/exmaple.com.conf に記述します。
<VirtualHost *:443> ServerAdmin admin@example.com ServerName vm101.example.com DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # SSLEngine on <FilesMatch "\.(?:cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> </VirtualHost> <VirtualHost *:80> ServerAdmin admin@example.com DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>ここで、Debian 12 の場合は、コンフィグをenableさせます。
a2ensite Your choices are: 000-default default-ssl vm101.example.com Which site(s) do you want to enable (wildcards ok)? vm101.example.com Enabling site vm101.exmaple.com. To activate the new configuration, you need to run: systemctl reload apache2次にsslモジュールを有効にします。
a2enmod ssl Considering dependency setenvif for ssl: Module setenvif already enabled Considering dependency mime for ssl: Module mime already enabled Considering dependency socache_shmcb for ssl: Module socache_shmcb already enabled Enabling module ssl. See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates. To activate the new configuration, you need to run: systemctl restart apache2ここで一旦、 systemctl restart apach2 として、apache2を再起動させておきます。
続いて証明書を発行するツールをインストールします。
apt-get install certbot python3-certbot-apache次に証明書を発行します。
certbot --apache後はプロンプトに従って入力していきます。
# certbot --apache Saving debug log to /var/log/letsencrypt/letsencrypt.log Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): admin@example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.4-April-3-2024.pdf. You must agree in order to register with the ACME server. Do you agree? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Yes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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: No Account registered. Which names would you like to activate HTTPS for? We recommend selecting either all domains, or all domains in a VirtualHost/server block. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: vm101.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): 1 Requesting a certificate for vm101.example.com Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/vm101.example.com/fullchain.pem Key is saved at: /etc/letsencrypt/live/vm101.example.com/privkey.pem This certificate expires on 2024-12-18. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. Deploying certificate Successfully deployed certificate for vm101.example.com to /etc/apache2/sites-enabled/vm101.example.com.conf Congratulations! You have successfully enabled HTTPS on https://vm101.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -証明書の発行が済むとexample.com.confは以下の様に変更されます。(整形済。ただし、SSLEngine on のところは手動で非コメント化必要)
<VirtualHost *:443> ServerAdmin admin@example.com ServerName vm101.example.com DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on <FilesMatch "\.(?:cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem Include /etc/letsencrypt/options-ssl-apache.conf </VirtualHost> <VirtualHost *:80> ServerAdmin admin@example.com DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined RewriteEngine on RewriteCond %{SERVER_NAME} =example.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>証明書を読み込むようにしたので、apache2をreloadさせます。
systemctl reload apache2なお今回インストールしたcertbot 2.1.0-4では証明書は自動的に更新するようです。
$ systemctl status certbot.timer ● certbot.timer - Run certbot twice daily Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; preset: enabled) Active: active (waiting) since Thu 2024-07-25 12:27:02 JST; 1h 46min ago Trigger: Fri 2024-07-26 03:09:26 JST; 12h left Triggers: ● certbot.service 7月 25 12:27:02 i45 systemd[1]: Started certbot.timer - Run certbot twice daily.しかし、apache2 のリロードまでは面倒を見てくれないようなので、以下の様に--deploy-hookを追加します。
#/lib/systemd/system/certbot.service [Unit] Description=Certbot Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html Documentation=https://certbot.eff.org/docs [Service] Type=oneshot ExecStart=/usr/bin/certbot -q renew --no-random-sleep-on-renew --deploy-hook "systemctl reload apache2" PrivateTmp=true設定が済んだら、サービスをリロードさせておきます。
systemctl daemon-reload今回は以上です。それでは。
コメント
コメントを投稿