LoginSignup
10
7

More than 3 years have passed since last update.

Let’s EncryptでApacheをHTTPSに対応させる

Last updated at Posted at 2019-05-13

2003年〜2005年くらいに作成し、その後内容は更新していないがメンテナンスだけしている自分のホームページをLet’s EncryptでHTTPS対応したメモ。

各コンポーネントのバージョンは以下。

コンポーネント バージョン
OS CentOS 7.6
Apache 2.4.6
Certbot 0.31.0

参考リンク

Let’s Encryptはググるとびっくりするくらい先人の情報が多い。参考にさせていただきつつも、最新の情報を得るためにはなるべく公式ドキュメントをみるほうがよい。

Firewalldの設定変更

HTTPSのポートを開ける。

firewall-cmd --list-all --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload

Certbotの導入

Certbotを導入する。

# EPELのリポジトリを追加
yum install epel-release
# certbotのインストール
yum install --enablerepo=epel certbot python2-certbot-apache

証明書の取得

ApacheではVirtualHostを使い、複数のドメインかつそれぞれでサブドメインも使用している。なので以下の対応が考えられる。

  • SANs(Subject Alternative Names)で複数ドメインに対応した証明書を作る
  • ワイルドカード証明書でかつ複数ドメインに対応した証明書を作る

前者の方が簡単そうなのでとりあえず前者でやることにする。

  • 複数ドメイン対応の証明書を作る場合は-dで複数のドメインを指定する
  • 既に80番ポートをListenしているプロセスがいるので--webrootモードを使い、そのドキュメントルートを指定する
    • 各ドメインの名前解決ができる必要がある
    • 指定したディレクトリーにファイルが作られ、アクセスできるかが検証される
    • ドメイン毎にドキュメントルートが異なる場合は以下の例のように指定可能
  • あるいは--apacheモードを指定すると設定ファイルをみてくれる
  • certonlyを指定しない場合は設定ファイルを書き換えてくれる

--webrootで証明書の生成だけをする場合は以下を実行する。

certbot certonly --webroot \
  -w /home/sotoiwa/www/www.sotoiwa.com/html \
  -d sotoiwa.com \
  -d www.sotoiwa.com \
  -w /home/sotoiwa/www/blog.sotoiwa.com/blog \
  -d blog.sotoiwa.com \
  -w /home/sotoiwa/www/www.husv.net/pukiwiki \
  -d husv.net \
  -d www.husv.net \
  -d wiki.husv.net

今回は以下を実行し、--apacheモードで、certonlyを指定しないでApacheの設定ファイルも書き換えてもらう。

certbot --apache \
  -d sotoiwa.com \
  -d www.sotoiwa.com \
  -d blog.sotoiwa.com \
  -d husv.net \
  -d www.husv.net \
  -d wiki.husv.net

(初回実行のログではなく、何度か失敗したりオプションを変えて試したりしたあとのログ)

実行ログ
# certbot --apache \
>   -d sotoiwa.com \
>   -d www.sotoiwa.com \
>   -d blog.sotoiwa.com \
>   -d husv.net \
>   -d www.husv.net \
>   -d wiki.husv.net
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Cert not yet due for renewal

You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.
(ref: /etc/letsencrypt/renewal/sotoiwa.com.conf)

What would you like to do?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Attempt to reinstall this existing certificate
2: Renew & replace the cert (limit ~5 per 7 days)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Renewing an existing certificate
Resetting dropped connection: acme-v02.api.letsencrypt.org
Deploying Certificate to VirtualHost /etc/httpd/conf/httpd-le-ssl.conf
Deploying Certificate to VirtualHost /etc/httpd/conf/httpd-le-ssl.conf
Deploying Certificate to VirtualHost /etc/httpd/conf/httpd-le-ssl.conf
Deploying Certificate to VirtualHost /etc/httpd/conf/httpd-le-ssl.conf
Deploying Certificate to VirtualHost /etc/httpd/conf/httpd-le-ssl.conf
Deploying Certificate to VirtualHost /etc/httpd/conf/httpd-le-ssl.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Enhancement redirect was already set.
Enhancement redirect was already set.
Enhancement redirect was already set.
Enhancement redirect was already set.
Enhancement redirect was already set.
Enhancement redirect was already set.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Your existing certificate has been successfully renewed, and the new certificate
has been installed.

The new certificate covers the following domains: https://sotoiwa.com,
https://www.sotoiwa.com, https://blog.sotoiwa.com, https://husv.net,
https://www.husv.net, and https://wiki.husv.net

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=sotoiwa.com
https://www.ssllabs.com/ssltest/analyze.html?d=www.sotoiwa.com
https://www.ssllabs.com/ssltest/analyze.html?d=blog.sotoiwa.com
https://www.ssllabs.com/ssltest/analyze.html?d=husv.net
https://www.ssllabs.com/ssltest/analyze.html?d=www.husv.net
https://www.ssllabs.com/ssltest/analyze.html?d=wiki.husv.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/sotoiwa.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/sotoiwa.com/privkey.pem
   Your cert will expire on 2019-08-11. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. 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

#

これでいい感じにhttpd.confが設定されていてちゃんとアクセスできた。リダイレクトもされた。何を書き換えているかは上記のログを参考に実際のファイルを見ればわかる。

自動更新

更新に関する設定は設定ファイルに書き込まれている。

cat /etc/letsencrypt/renewal/sotoiwa.com.conf
# renew_before_expiry = 30 days
version = 0.31.0
archive_dir = /etc/letsencrypt/archive/sotoiwa.com
cert = /etc/letsencrypt/live/sotoiwa.com/cert.pem
privkey = /etc/letsencrypt/live/sotoiwa.com/privkey.pem
chain = /etc/letsencrypt/live/sotoiwa.com/chain.pem
fullchain = /etc/letsencrypt/live/sotoiwa.com/fullchain.pem

# Options used in the renewal process
[renewalparams]
authenticator = apache
account = hogehoge
server = https://acme-v02.api.letsencrypt.org/directory
installer = apache
#

rootcrontab -eで設定してもよいが、以下のファイルを作成して毎月1日の12時13分に自動更新させる。

/etc/cron.d/letsencrypt
13 12 1 * * root certbot renew --post-hook "systemctl restart httpd"
10
7
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
10
7