Contents
1. Apache2 : SSL/TLS の設定
Let’s Encryptで取得したSSL証明書をapacheへ適用する方法を説明。
ドメイン名 : hoge.comとする
1.1 Apache2の設定
①Apache2設定ファイルの編集
デフォルトのconfファイルをリネームしてコピー(例として「hoge.com-ssl.conf」)
# cd /etc/apache2/sites-available/ # cp default-ssl.conf hoge.com-ssl.conf # vi hoge.com-ssl.conf # 3行目:管理者アドレス変更 ServerAdmin <メールアドレス> # 32,33行目:取得したLet’s Encrypt証明書に変更 SSLCertificateFile /etc/letsencrypt/live/hoge.com/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/hoge.com/privkey.pem # 42行目:コメント解除して取得したLet’s Encrypt証明書チェインファイルに変更 SSLCertificateChainFile /etc/letsencrypt/live/hoge.com/chain.pem # a2ensite hoge.com-ssl.conf ← 有効化 Enabling site hoge.com-ssl.conf. To activate the new configuration, you need to run: systemctl reload apache2# a2dissite default-ssl.conf ← デフォルト無効化 |
②SSLモジュールを有効
# a2enmod ssl Considering dependency setenvif for ssl: Module setenvif already enabled Considering dependency mime for ssl: Module mime already enabled Considering dependency socache_shmcb for ssl: Enabling module socache_shmcb. Enabling module ssl. See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates. To activate the new configuration, you need to run: systemctl restart apache2 |
Apache2再起動
# systemctl restart apache2 |
1.2 httpからhttpsリダイレクト
①「.htaccess」ファイルを作成する方法
.htaccessを/var/www/html/korodes.com/に作成して以下記入 RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] |
②vhost-hoge.com.conf に記入の方法
# vi /etc/apache2/sites-available/vhost-korodes.com.conf 最初の行に下記を追加 RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] |
②設定反映
# a2ensite vhost-korodes.com.conf # a2dissite 000-default.conf ← デフォルト無効化 # systemctl restart apache2 ← Apache2再起動 |
2.メールサーバー : SSL/TLS の設定
暗号化通信ができるよう SSL/TLS の設定をします。
SMTPS は 465/TCP, POP3S は 995/TCP, IMAPS は 993/TCP を使用します
2.1 バーチャルホストの設定とメール用証明書取得
①メールホストのバーチャルホスト設定
# cd /etc/apache2/sites-available/ # vi vhost-hoge.com.conf<VirtualHost *:80> ~省略~ ServerName mail.hoge.com ServerAdmin <メールアドレス> DocumentRoot /var/www/html/mail.hoge.com ~省略~ ErrorLog ${APACHE_LOG_DIR}/mail.hoge.com.error.log CustomLog ${APACHE_LOG_DIR}/mail.hoge.com.access.log combined ~省略</VirtualHost> |
②ディレクトリー作成
# vi /var/www/html/mail.hoge.com |
③letsencrypt SSL証明書の取得
# certbot certonly –webroot -w /var/www/html/mail.hoge.com -d mail.hoge.com |
2.2 Postfix の設定
①「main.cf」の編集
# vi /etc/postfix/main.cf # 最終行に追記 |
②「master.cf」の編集
# vi /etc/postfix/master.cf submission inet n – y – – smtpd ←以下、コメントを外します。(SSL,STARTTLSどちらにも対応) smtps inet n – y – – smtpd |
③設定に誤りがないかチェック
# postfix check なにも表示されなければOKです |
④Postfixの起動と自動起動
# systemctl start postfix # systemctl enable postfix |
2.3 Dovecotの設定
①「10-ssl.conf」の編集
# vi /etc/dovecot/conf.d/10-ssl.conf # 6行目:変更 |
②「10-master.conf」の編集
# vi /etc/dovecot/conf.d/10-master.conf service imap-login { inet_listener imap { inet_listener imaps { } service pop3-login { inet_listener pop3s { } ・Dovecot SASL ライブラリの認証ソケットファイルを指定(113行目あたりです) service auth { ↓ # Postfix smtp-auth } |
③認証方式の設定
# vi /etc/dovecot/conf.d/10-auth.conf #disable_plaintext_auth = yes ↓ disable_plaintext_auth = no auth_mechanisms = plain ↓ auth_mechanisms = plain login |
④メールボックスの場所を指定
# vi /etc/dovecot/conf.d/10-mail.conf #mail_location = ↓ mail_location = maildir:~/Maildir |
⑤ログの出力先を変更
# vi /etc/dovecot/conf.d/10-logging.conf #log_path = syslog ↓ log_path = /var/log/dovecot/dovecot.log |
⑥ログの出力先作成
# mkdir /var/log/dovecot |
⑦Dovecotを起動と自動起動の設定
# systemctl start dovecot # systemctl enable dovecot |
⑧認証ソケットファイルが作成されているのを確認
# ls -F /var/spool/postfix/private/auth —(下記表示があればOK)— /var/spool/postfix/private/auth = |
3.FTPサーバーにSSL/TLS の設定
Webサーバ暗号化で取得したLet’s Encryptでftpの転送も暗号化します。
3.1 Vsftpd設定
①設定ファィル編集
# vi /etc/vsftpd.conf —(下記を最終行に追記)— # add letsencrypt #pasv port |
②Vsftpd再起動
# systemctl restart vsftpd |
3.2 Firewall設定
ftpポート以外に固定したPASVポートを許可します
# ufw allow 21000:21010/tcp |