Ubuntu 18.04 と Apache2.4 で Let’s Encrypt を利用して SSL 化する
以前は Let’s Encrypt から提供された shell script が利用されていたようだが、最近は Certbot を使うらしい。
Certbot の導入
以下コマンドで Certbot をインストール
$ sudo apt install certbot
Apache を停止する
Let’s Encrypt が port80 を使用するので Apache を一時的に停止する
$ sudo systemctl stop apache2
SSL/TLS サーバ証明書を取得する
以下コマンドで証明書を取得する。
-d オプションには自分のドメイン(例ではhoge.example.com)を指定する
$ sudo certbot certonly --standalone -d hoge.example.com
途中でメールアドレスを聞かれるので
自分のメールアドレス(例ではhoge@example.com)を入力して Enter
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): hoge@example.com
進行中に再度確認をされるので
都度それぞれ、(A)gree の「a」キー、(Y)esの 「y」キー を入力
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: a
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing 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
正常にインストールされれば、SSLの発行が終わり
以下のように「IMPORTANT NOTES: Congratulations! …」に続いて
証明書の保存先が表示、証明書の導入が完了。
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for hoge.example.com
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/hoge.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/hoge.example.com/privkey.pem
Your cert will expire on 2020-02-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"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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
上記の例の場合
/etc/letsencrypt/live/hoge.example.com/fullchain.pem
/etc/letsencrypt/live/hoge.example.com/privkey.pem
の部分
SSL証明書の確認
以下のコマンドで、上記の証明書保存先を念のため確認
$ sudo ls /etc/letsencrypt/live/hoge.example.com/
次の結果が返ってきた。たしかにSSL証明書ファイルが生成されている。
README cert.pem chain.pem fullchain.pem privkey.pem
Apache の設定で SSL (https) を有効化する
次のコマンドで、念のためディフォルトの設定ファイル
/etc/apache2/sites-available/default-ssl.conf
をバックアップ
$ cd /etc/apache2/sites-available
$ sudo cp default-ssl.conf default-ssl.conf.default
/etc/apache2/sites-available/default-ssl.conf
の内容を以下の通りに修正
# 3行目付近の以下設定を、管理者アドレス(証明書取得時に入力したメールアドレス)に変更
ServerAdmin hoge@example.com
# 32、33行目付近の以下設定を、今回取得した証明書に変更
SSLCertificateFile /etc/letsencrypt/live/hoge.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hoge.example.com/privkey.pem
# 42行目付近の以下設定を、コメント解除して今回取得したチェインファイルに変更
SSLCertificateChainFile /etc/letsencrypt/live/hoge.example.com/chain.pem
次のコマンドで、上記設定ファイルの更新内容を Apache に反映
$ sudo a2ensite default-ssl.conf
Enabling site default-ssl.
To activate the new configuration, you need to run:
systemctl reload apache2
$ sudo 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:
Enabling module socache_shmcb.
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
$ sudo systemctl restart apache2
証明書を自動更新させる
Let’s Encrypt 証明書の有効期限は90日間(約3ヶ月)なので、定期的に更新する必要がある。
毎回手動で更新するのも面倒なので cerbot を cron で動かして自動的に更新する。
手動での証明書更新テスト
$ sudo systemctl stop apache2
$ sudo letsencrypt renew --dry-run --agree-tos
$ sudo systemctl start apache2
手動での証明書の更新
$ sudo systemctl stop apache2
$ sudo letsencrypt renew
$ sudo systemctl start apache2
バーチャルホストの設定
バーチャルホストの設定用に、適当な名前で設定ファイルを作成。
(今回は↓としておいた)
/etc/apache2/sites-available/virtual-host.conf
設定内容は、以下を実際に利用する自分の環境に合わせて
hoge.example.com、hoge@example.com の部分を修正して設定
ServerName hoge.example.com
ServerAdmin hoge@example.com
DocumentRoot /var/www/hoge.example.com
ErrorLog ${APACHE_LOG_DIR}/hoge.example.com.error.log
CustomLog ${APACHE_LOG_DIR}/hoge.example.com.access.log combined
LogLevel warn
次のコマンドで、上記設定ファイルの更新内容を Apache に反映
$ sudo a2ensite virtual-host.conf
Enabling site virtual-host.
To activate the new configuration, you need to run:
systemctl reload apache2
$ sudo systemctl restart apache2
バーチャルホスト + SSL の設定
バーチャルホストのSSL設定用に、適当な名前で設定ファイルを作成。
(今回は↓としておいた)
/etc/apache2/sites-available/virtual-ssl.conf
設定内容は、以下を実際に利用する自分の環境に合わせて
hoge.example.com、hoge@example.com の部分を修正して設定
ServerAdmin hoge@example.com
DocumentRoot /var/www/hoge.example.com
ErrorLog ${APACHE_LOG_DIR}/hoge.example.com.error.log
CustomLog ${APACHE_LOG_DIR}/hoge.example.com.access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/hoge.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hoge.example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/hoge.example.com/chain.pem
SSLOptions +StdEnvVars
SSLOptions +StdEnvVars
次のコマンドで、上記設定ファイルの更新内容を Apache に反映
$ sudo a2ensite virtual-ssl.conf
Enabling site virtual-ssl.
To activate the new configuration, you need to run:
systemctl reload apache2
$ sudo 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
Module ssl already enabled
$ sudo systemctl restart apache2
http を https にリダイレクト
httpでのリクエストも全てhttpsにリダイレクトするには
/etc/apache2/sites-available/000-default.conf
に以下の設定を加えて
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
次のコマンドで、上記設定ファイルの更新内容を Apache に反映
$ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
$ sudo systemctl restart apache2
ディスカッション
コメント一覧
まだ、コメントがありません