KDE BLOG

Webデザインやコーディングについて書いています

さくらのVPS に Let's encrypt を導入してお名前.comで取得したドメインをサブドメイン含めてSSL対応する

やりたいこと

個人開発で上記を達成するためにいろいろ試行錯誤したので備忘録です。
サーバー周りの設定は詳しくないので、設定が誤っている可能性があります。

環境

  • OS: CentOS7
  • Webサーバー:Apache v2.4.6

手順

ApacheSSLモジュール mod_ssl をインストールする

# yum install mod_ssl

変更を有効にするため、httpdを再起動する

# systemctl restart httpd

導入できているか確認(ssl_module が一覧に出てくればOK)

# httpd -M

...(略)
ssl_module (shared)

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

# firewall-cmd --add-service=https --zone=public --permanent && firewall-cmd --reload 

https 通信がファイアウォールで許可されているか確認(services の項に https があればOK)

# firewall-cmd --list-all

...(略)
services: dhcpv6-client http https ssh

certbot パッケージと Apache プラグインpython2-certbot-apache パッケージをインストールします。
certbot パッケージは EPEL リポジトリにあります。さくらのVPS ではデフォルトで EPEL リポジトリが追加されているので上記コマンドだけでいけます。

# yum -y install certbot python2-certbot-apache

SSL証明書を取得する

※2018年1月中旬頃から取得方法が変更となった

# certbot --authenticator standalone --installer apache -d example.com -d test.example.com --pre-hook "apachectl stop" --post-hook "apachectl start"

いろいろ聞かれるので下記を参考に答える
Let's Encryptの証明書の取得・設定 - Qiita

https://example.com にアクセスして、SSL対応できているか確認する

自動更新の設定

cp -av /{usr/lib,etc}/systemd/system/certbot-renew.timer

vi /etc/systemd/system/certbot-renew.timer

# ここで onCalendar=monthly に変更する

systemctl daemon-reload

# Timer を有効化
systemctl enable certbot-renew.timer

# 設定状況の確認
systemctl list-timers

# 詳細確認
systemctl list-unit-files --type=timer

※ 実際に正しく更新されるかはまだ未確認です。
もし間違っていたらそのときは訂正したいと思います。

証明書の確認

デフォルトで /etc/letsencrypt/live/ 配下に証明書が保存される

# ll /etc/letsencrypt/live/

-rw-r--r-- 1 root root  740 Feb 22 22:53 README
drwxr-xr-x 2 root root 4096 Feb 22 22:53 example.com
drwxr-xr-x 2 root root 4096 Feb 24 01:41 test.example.com

さくらのVPSサブドメイン設定

下記を参考に設定

さくらVPSでサブドメイン設定はとても簡単! | noblog

バーチャルホストの設定

httpd.conf を編集

# vi /etc/httpd/conf/httpd.conf

ファイル末尾に下記を追加
IPアドレスは仮に 192.168.0.1 としておきます

NameVirtualHost 192.168.0.1:80
<VirtualHost 192.168.0.1:80>
    DocumentRoot /var/www/html
    ServerName example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost 192.168.0.1:80>
    DocumentRoot /var/www/test
    ServerName test.example.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =test.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

続いて httpd-le-ssl.conf も編集する
(これを編集する必要があるのに気づくのが大変だった)

# vi /etc/httpd/conf/httpd-le-ssl.conf
 <IfModule mod_ssl.c>
 <VirtualHost 192.168.0.1:443>
     DocumentRoot /var/www/html
     ServerName example.com
 SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
 SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
 Include /etc/letsencrypt/options-ssl-apache.conf
 SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
 </VirtualHost>

<VirtualHost 192.168.0.1:443>
     DocumentRoot /var/www/test
     ServerName test.example.com
 SSLCertificateFile /etc/letsencrypt/live/test.example.com/cert.pem
 SSLCertificateKeyFile /etc/letsencrypt/live/test.example.com/privkey.pem
 Include /etc/letsencrypt/options-ssl-apache.conf
 SSLCertificateChainFile /etc/letsencrypt/live/test.example.com/chain.pem
 </VirtualHost>
 </IfModule>

apache の再起動

# systemctl restart httpd

これで設定は完了です。

上記設定により本番用サイト example.com のドキュメントルートは /var/www/html、 テスト環境用 test.example.com のドキュメントルートは /var/www/test となっているので、 そこに各々ファイルを配置してブラウザから https:// でアクセスすれば、SSL対応されているのを確認できます。

参考