Google さんが常時 SSL 化を推奨しているので、移転後の Ruby on Rails 用に HTTPS を開きたい(現行の WordPress は非 SSL のままにする)。
構築したてのメールサーバも平文通信になっているので、こちらも SSL 化してみよう。
環境
- OS : Ubuntu 16.04.4 LTS
- Web サーバ : nginx/1.12.2
- SMTP サーバ : Postfix 3.1.0
- IMAP/POP3 サーバ : Dovecot 2.2.22
SSL 証明書の取得
無料で SSL 証明書を発行してくれる Let's Encrypt を使用。
以下のサイトを参考にした。
- letsencryptをUbuntu16.04 + Nginxで使ってみた
- Let's Encrypt で Nginx にSSLを設定する
- let's encrypt の証明書を作ってみた
- cronの設定方法
Let's Encrypt クライアントのインストール
$ sudo apt-get install letsencrypt
SSL 証明書の発行
Let's Encrypt クライアントが80番ポートを使うので Web サーバを一旦停止する必要があるらしい。
$ sudo systemctl stop nginx
$ sudo letsencrypt certonly --standalone -d example.com -m root@example.com --agree-tos -n
IMPORTANT NOTES:
- If you lose your account credentials, you can recover through
e-mails sent to root@example.com.
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/example.com/fullchain.pem. Your cert will
expire on 2018-06-13. To obtain a new version of the certificate in
the future, simply run Let's Encrypt again.
- Your account credentials have been saved in your Let's Encrypt
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Let's
Encrypt so making regular backups of this folder is ideal.
- If you like Let's Encrypt, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
$ sudo systemctl start nginx
SSL 証明書の自動更新設定
root
ユーザで cron の設定。
有効期限が90日なので、2ヶ月に1回更新。
00 05 15 1,3,5,7,9,11 * systemctl stop nginx && letsencrypt renew --force-renewal && systemctl start nginx
/dev/null
へリダイレクトしていないので、SSL 証明書の自動更新時にroot@localhost
宛にメールが送られるはず。
Web サーバの設定
nginx の設定
/etc/nginx/conf.d/default.conf
に以下の内容を追記。
HTTPS を開くがとりあえず全部弾かせる。Rails を立ち上げた後に設定し直す予定。
server {
listen 443 ssl;
server_name example.com;
root /usr/share/nginx/html;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
location / {
deny all;
}
}
設定ファイルをテストして再起動。
$ sudo service nginx configtest
$ sudo systemctl restart nginx
動作確認
443番ポートが開いていることを確認。
$ netstat -an --tcp | grep 443
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN
Web ブラウザからhttps://example.com
にアクセス。ドメイン横に鍵マークが付き、nginxが403 Forbidden
を表示。想定通り。
メールサーバの設定
SSL 対応前のメールサーバの構築は以下のサイトを参考にしている。
SSL 対応は主に以下のサイトを参考にした。
Postfix の設定
/etc/postfix/master.cf
を編集。
SMTP を無効にし、SMTPS と Submission を有効にする。
#smtp inet n - y - - smtpd
submission inet n - y - - smtpd
-o smtpd_sasl_auth_enable=yes
smtps inet n - y - - smtpd
-o smtpd_tls_wrappermode=yes
設定ファイルのリロード。
$ sudo postfix reload
Dovecot の設定
/etc/dovecot/conf.d/10-master.conf
を編集。
IMAP、POP3 を無効にし、IMAPS、POP3S を有効にする。
service imap-login {
inet_listener imap {
port = 0
}
inet_listener imaps {
port = 993
ssl = yes
}
}
service pop3-login {
inet_listener pop3 {
port = 0
}
inet_listener pop3s {
port = 995
ssl = yes
}
}
port = 0
を明示的に指定しないと、無効とならない点に注意。
僕は POP3 を使用しないので、inet_listener pop3s
のport
も0
に指定している。
/etc/dovecot/conf.d/10-ssl.conf
を編集し、SSL 証明書を指定。
ssl = yes
ssl_cert = </etc/letsencrypt/live/example.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/example.com/privkey.pem
設定ファイルのリロード。
$ sudo dovecot reload
動作確認
25番、110番、143番が閉じており、465番、587番、993番、995番が開いていることを確認。
$ netstat -an --tcp | grep LISTEN
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:993 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:587 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:465 0.0.0.0:* LISTEN
tcp6 0 0 :::2222 :::* LISTEN
上記結果では995番が表示されていないが、これは POP3S を無効にしているため。10-master.conf
でpop3s
ポートを995
に指定していれば表示されるはず。
あとはメールクライアントでメールの設定をし、送受信ができることを確認。