LoginSignup
12
13

More than 5 years have passed since last update.

CentOS 7 の Apache に Let's Encrypt で SSL を設定する

Last updated at Posted at 2017-11-06

EPEL リポジトリにある certbot パッケージを利用して Let's Encrypt で SSL 証明書を取得して Apache に設定する手順。

インストール

yum -y epel-release && yum -y install certbot{,-apache}

EPEL リポジトリを追加し、certbot パッケージと Apache プラグインの python2-certbot-apache パッケージをインストールする。

certbot-apache を指定すると python2-certbot-apache として補完してくれる。

firewalld

firewall-cmd --add-service=https --permanent && firewall-cmd --reload 

https 通信で使用する 443 ポートを公開する。

SSL 証明書の取得

sed -i /etc/sysconfig/certbot \
 -e "/^PRE_HOOK/ s/\"\"/\"--pre-hook 'systemctl stop httpd'\"/" \
 -e "/^POST_HOOK/ s/\"\"/\"--post-hook 'systemctl restart httpd'\"/"

certbot 実行時に Apache を停止する設定を追加。

certbot run --apache -d www.example.com

certbot run で SSL 証明書を取得。
--apache オプションを利用するには python2-certbot-apache パッケージがインストールされていることが必要。
--apache オプションを指定すると、既存の Apache 設定ファイルから自動的に該当する箇所を検出して https 用の設定ファイル -le-ssl.conf として自動生成してくれる。

自動更新の設定

Let's Encrypt で取得できる SSL 証明書の有効期限は90日間となっており、自動更新する必要がある。
Cron での設定例が多いが、certbot パッケージでは Timer の設定ファイルが用意されており、そちらを利用するのがスマートだろう。

cp -av /{usr/lib,etc}/systemd/system/certbot-renew.timer && \
sed -i /etc/systemd/system/certbot-renew.timer \
 -e '/OnCalendar/ s/daily/weekly/' && \
systemctl daemon-reload

certbot パッケージでは OnCalendar=daily と日次指定されているが、ここでは週次に変更している。月次の方がより適切だろう。

systemctl enable certbot-renew.timer && systemctl list-timers

Timer を有効化し、設定状況を確認する。
systemctl list-unit-files --type=timer または systemctl --type=timer などでも詳細を確認できる。

コマンドまとめ

yum -y epel-release && yum -y install certbot{,-apache} && \
firewall-cmd --add-service=http{,s} --permanent && \
firewall-cmd --reload && \
systemctl stop httpd && \
certbot run --apache -d www.example.com && \
systemctl start httpd && \
sed -i /etc/sysconfig/certbot \
 -e "/^PRE_HOOK/ s/\"\"/\"--pre-hook 'systemctl stop httpd'\"/" \
 -e "/^POST_HOOK/ s/\"\"/\"--post-hook 'systemctl restart httpd'\"/" && \
cp -av /{usr/lib,etc}/systemd/system/certbot-renew.timer && \
sed -i /etc/systemd/system/certbot-renew.timer \
 -e '/OnCalendar/ s/daily/weekly/' && \
systemctl daemon-reload && \
systemctl enable certbot-renew.timer && \
systemctl list-timers

なお、VirtualHost ディレクティブで <VirtualHost *:443> のようにワイルドカードを指定していると、 /etc/httpd/conf.d/ssl.conf<VirtualHost _default_:443> の設定が優先されるのか SSLProtocolSSLCipherSuite などが上書きされない模様。(詳細未調査)

12
13
1

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
12
13