お名前.comのVPS+CentOS7+nginxで、サイトのSSL化(Let's encrypt)& ついでにHTTP2も1
(2018/10/14改定)Let's EncryptのSSL証明書の発行や更新するためのcertbotをインストールするやり方に改定しました。
ウェブサイトでhttpsを使用する必要が出てきたので、無料でSSL/TLSサーバー証明書を利用できるLet's Encryptを、お名前.comのVPS+CentOS7+nginxのサーバーで試してみた。
事前準備
certbotをインストールし、Nginxの設定を行います。
httpsを使えるようにする準備
firewallの設定で、httpsのサービスを追加しておく。
# firewall-cmd --zone=public --add-service=https --permanent
# firewall-cmd --reload
certbotのインストール
Let’s Encrypt の SSL 証明書を発行や更新するための certbot をインストールします。
certbotはEPELに準備されていますが、epelレポジトリをインストールしなくても、yumコマンドの依存関係でremiレポジトリをインストールする際に、同時にepelレポジトリもインストールされるようになりました。
# yum install -y certbot
これだけです。
Nginxの設定
要は、Let's Encryptによる承認の際に、特定のuriにアクセスが来たら、事前に準備したディレクトリに通すようにしておく。
# Let's Encryptからアクセスが来た時に参照させるディレクトリの作成
# mkdir -p /usr/share/letsencrypt/test.com
# cd /etc/nginx/conf.d
# vim test-com.conf
++++++++test-com.confの中身。以下、各人の設定に合わせて変更++++++++
server {
listen 80;
server_name test.com;
root /usr/share/nginx/test.com;
index index.php index.html index.htm
# for Let's Encrypt
location ^~ /.well-known/ {
root /usr/share/nginx/letsencrypt/test.com;
}
}
++++++++以上test-com.confの中身を保存する++++++++
nginxの設定ファイルを変更したので、nginxを再起動
# systemctl restart nginx
Let's Encrypt のSSL/TLS証明書の取得
SSL 証明書の取得コマンドを使用します
# certbot certonly --webroot -w /usr/share/nginx/letsencrypt/test.com -d test.com
この時--webroot -wで利用するディレクトリは事前に準備した専用のディレクトリであって、決してtest.com用の公開ディレクトリではないことに注意。
対話形式で設定が進むので必要に応じて回答していく。内容は簡単なので読めばわかります
++++++++オプション説明++++++++
certonly:ウェブサーバーの設定は自分で行うので、今回はcertonlyにする
--webroot:プラグイン指定。ウェブサーバーをインストールしていない場合には、--standaloneで、standaloneプラグインを指定
-w:利用するドメインのルートディレクトリを指定
-m:何かあった時に連絡が取れるメールアドレスを指定
--agree-tos:Let's Encryptの利用規約に同意したことを指定
この結果、Congratulationの表示が出てきたら、正しく設定されたことを意味します。
また、こコマンドで取得した証明書や秘密鍵は「/etc/letsencrypt/live/xxx.com/」以下に保存されるので、nginxのconfファイルの修正や、今後postfixなどを使用するときには、証明書・秘密鍵の指定にこのディレクトリを使用します。
certbot-renew.timerでLet's Encrypt更新設定
certbotは、SSL証明書自動更新時のフックポイントを用意しています。
SSL証明書の自動更新後に、nginxを再起動するための設定を行います。
# sed -i -e "s/^POST_HOOK=\"\"/POST_HOOK=\"--post-hook 'systemctl restart nginx'\"/" /etc/sysconfig/certbot
Let's Encryptによる証明書は3か月更新なので、cronで更新設定をしておりましたが、CentOS7でcertbotパッケージを使用しているので、certbot-renew.timerを利用できます。
# systemctl enable --now certbot-renew.timer
メッセージに、
Created symlink from /etc/systemd/system/timers.target.wants/certbot-renew.timer to /usr/lib/systemd/system/certbot-renew.timer.
と出ていたら完了です。
nginxの設定
最後にhttp://www.xxx.com, http://xxx.comにアクセスが来たら、https://xxx.comに301リダイレクトするように、nginxのconfファイルを設定します。
また、HTTP2化には、「listen 443 ssl http2;」とするだけでOK。
# vim /etc/nginx/conf.d/test.com.conf
++++++++xxx.com.confの内容++++++++
server {
listen 80;
server_name www.test.com test.com;
return 301 https://test.com$request_uri;
}
server {
listen 443 ssl http2; #←http2を付け加えるだけ
server_name test.com;
access_log /var/log/nginx/test.co/maccess.log main;
error_log /var/log/nginx/test.com/error.log warn;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem;
ssl_trusted_certificate /etc/letsencrypt/live/test.com/chain.pem;
ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem;
location ^~ /.well-known/acme-challenge/ {
root /var/www/letsencrypt;
}
location / {
...
}
}
++++++++xxx.com.confの内容終わり++++++++
# systemctl restart nginx