LoginSignup
10

More than 5 years have passed since last update.

Ubuntu 16.04 + nginx + Postfix + Dovecot 環境で SSL 通信を有効化

Posted at

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 を使用。
以下のサイトを参考にした。

Let's Encrypt クライアントのインストール

console
$ sudo apt-get install letsencrypt

SSL 証明書の発行

Let's Encrypt クライアントが80番ポートを使うので Web サーバを一旦停止する必要があるらしい。

console
$ 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回更新。

crontab
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 を立ち上げた後に設定し直す予定。

/etc/nginx/conf.d/default.conf
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;
  }
}

設定ファイルをテストして再起動。

console
$ sudo service nginx configtest
$ sudo systemctl restart nginx

動作確認

443番ポートが開いていることを確認。

console
$ 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 を有効にする。

/etc/postfix/master.cf
#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

設定ファイルのリロード。

console
$ sudo postfix reload

Dovecot の設定

/etc/dovecot/conf.d/10-master.confを編集。
IMAP、POP3 を無効にし、IMAPS、POP3S を有効にする。

/etc/dovecot/conf.d/10-master.conf
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 pop3sport0に指定している。

/etc/dovecot/conf.d/10-ssl.confを編集し、SSL 証明書を指定。

/etc/dovecot/conf.d/10-ssl.conf
ssl = yes

ssl_cert = </etc/letsencrypt/live/example.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/example.com/privkey.pem

設定ファイルのリロード。

console
$ sudo dovecot reload

動作確認

25番、110番、143番が閉じており、465番、587番、993番、995番が開いていることを確認。

console
$ 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.confpop3sポートを995に指定していれば表示されるはず。

あとはメールクライアントでメールの設定をし、送受信ができることを確認。

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
10