LoginSignup
43
56

More than 5 years have passed since last update.

CentOS7でLet's Encryptを使う

Last updated at Posted at 2016-08-18

初めに

CentOS7上で、独自ドメインのSSL/TLS 証明書を無料で取得できて、しかも商用利用もできる、Let's Encrypt の設定をしてみました。Let's Encrypt 自体については https://letsencrypt.jp/ を参照ください。

インストール

CentOS7 では、certbot をyum でインストールできます。Web Server は apache なので、合わせて、certbot-apache もインストール。

yum install certbot certbot-apache

設定

今まで開けていなかった、https ポートへのアクセス許可

sudo firewall-cmd --add-port=443/tcp --zone=public
sudo firewall-cmd --add-port=443/tcp --zone=public --permanent

--permanentがないと一時的な設定でしかないのに注意

SSL証明書の自動設定

 バーチャルホストで運用しているそれぞれのホスト名 host1.sample.com と host2.sample.com の2つの証明書を設定します。
もちろん、sample.comは自分のドメインに置き変えないと動作しません。

sudo certbot run --apache -d host1.sample.com -d host2.sample.com

もし不足パッケージがあれば yum でインストールするかを聞いてくるので、y を入力するとインストールしてくれます。

2つのホストの証明書が1コマンドで同時に取得できます。Apache設定ファイルとして、ssl.conf を指定すると、ここに全部のホスト分の SSL証明書へのパス等が自動で設定されます。なお、SSL証明書は /etc/letsencrypt/archibe/host1.sample.com/ のようなホスト
名のディレクトリに保存されます。

certbotを実行すると以下のように、httpアクセスをhttpsにリダイレクトするかを聞いてきますのでお好みのものを選びます。

Please choose whether HTTPS access is required or optional.
----------------------------------------------------------------
1: Easy - Allow both HTTP and HTTPS access to these sites
2: Secure - Make all requests redirect to secure HTTPS access
----------------------------------------------------------------

http をすべて https にリダイレクトするセキュア設定は、CentOS上では動作しなかったので、必要ならば手動で設定します。
Secureを選ぶと、apacheのVirturalHOSTの設定部分に以下ようなの2行が追加されました。

RewriteCond %{SERVER_NAME} =host.exsample.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

SSL証明書の更新

証明書の有効期限が3か月なので、更新が必要です。更新も certbot でします。

certbot renew

このコマンドは、証明書の期限切れ近くにならないと更新されないので、cronで、1週間に1度くらい実行しておくと安心と思います。

なお、renew でドメイン名は指定できないようです。下のメッセージが出ました。

Currently, the renew verb is only capable of renewing all installed 
certificates that are due to be renewed; individual domains cannot be specified 
with this action. If you would like to renew specific certificates, use the 
certonly command. The renew verb may provide other options for selecting 
certificates to renew in the future.

参考

こちらのページを参考にしました。

43
56
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
43
56