本ページでは、Let’s Encryptで証明書取得とapacheへの適用、メールサーバーへの適用、FTPサーバーへ適用し、SSL化する方法を説明。
Contents
1. 証明書を取得する (Let’s Encrypt)
1.1 事前準備
①mod_sslを有効にする
# a2enmod ssl |
②Let’s Encrypt証明書を取得するためのクライアントツールをインストール
# apt -y install certbot |
③Let’s Encrypt証明書の取得
Apache httpd や Nginx などの Webサーバーが稼働していることが前提となります。
作業を実施するサーバーで Webサーバーが稼働していない場合は ④の手順を実行。
また、インターネット側から、作業を実施するサーバー (証明書を取得したい FQDN のサーバー) の 80 ポート宛てにアクセス可能であることも前提です。
# certbot certonly –webroot -w /var/www/html/hoge.com -d hoge.com |
# [–webroot] 指定で稼働中 Web サーバーの公開ディレクトリ配下を認証用の一時領域に使用
# -w [ドキュメントルート] -d [証明書を取得したいFQDN] # FQDN (Fully Qualified Domain Name) : ホスト名.ドメイン名を省略なしで表記
# ドキュメントルートはバーチャルホストで複数のホスト定義がある場合、該当するホスト定義のものを指定
# ドキュメントルート指定の動作としては, 指定したドキュメントルート配下に
# [.well-known] ディレクトリが作成され, 認証用のファイルが自動的,一時的に設置されるのみ
# 初回のみメールアドレスの登録と利用条件への同意が必要
# 受信可能なメールアドレスを指定
Enter email address (used for urgent notices and lost key recovery) hoge@hoge.com < OK > <Cancel> # 利用条件に同意する <Agree > <Cancel> IMPORTANT NOTES: Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donate |
# [Congratulations] と表示されれば成功
# メッセージ中に記載の通り [/etc/letsencrypt/live/hoge.com/] 配下に次の証明書が取得されている
# cert.pem ⇒ SSLサーバー証明書(公開鍵含む)
# chain.pem ⇒ 中間証明書
# fullchain.pem ⇒ cert.pem と chain.pem が結合されたファイル
# privkey.pem ⇒ 公開鍵に対する秘密鍵
④Webサーバーが稼働していない場合のLet’s Encrypt証明書の取得
インターネット側から当作業を実施するサーバーの 80 ポート宛てにアクセス可能であることは前提となります
# [–standalone] 指定で 簡易 Webサーバー機能を使用
# -d [証明書を取得したいFQDN] # FQDN (Fully Qualified Domain Name) : ホスト名.ドメイン名を省略なしで表記
# 証明書を取得したいFQDNが複数ある場合は、-d [証明書を取得したいFQDN] を複数指定
# certbot certonly –standalone -d hoge.com |
Enter email address (used for urgent notices and lost key recovery) hoge@hoge.com < OK > <Cancel> # 利用条件に同意する <Agree > <Cancel> IMPORTANT NOTES: Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donate |
⑤取得済みの証明書を更新する
# 有効期限が 30日未満の証明書を全て更新
# 有効期限の残り日数に関わらず更新したい場合は [–force-renewal] を合わせて指定
# certbot [–force-renewal] renew |
2. Apache2にSSL/TLS(Let’s Encrypt) の設定
①Apache2のSSL関連設定ファイルの編集
# cd /etc/apache2/sites-available/ # cp default-ssl.conf hoge.com-ssl.conf # vi hoge.com-ssl.conf # 3行目:管理者アドレス変更 ServerAdmin hoge@hoge.com # 5行目:変更 DocumentRoot /var/www/html/hoge.com # 13,14行目:変更 ErrorLog ${APACHE_LOG_DIR}/hoge.com.error.log CustomLog ${APACHE_LOG_DIR}/hoge.com.access.log combined # 32,33行目:[1]で取得した証明書に変更 SSLCertificateFile /etc/letsencrypt/live/hoge.com/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/hoge.com/privkey.pem # 42行目:コメント解除して[1]で取得したチェインファイルに変更 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 # systemctl restart apache2 # a2dissite default-ssl.conf デフォルト無効化 # systemctl restart apache2 # 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 |
③httpからhttpsリダイレクト
1..htaccessファイル を作成する方法 .htaccessを/var/www/html/hoge.com/に作成して以下記入 RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 2./vhost-hoge.com.conf に記入の方法 # vi /etc/apache2/sites-available/vhost-hoge.com.conf RewriteEngine on RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] |
④設定反映と起動
# a2ensite vhost-hoge.com.conf Apache再起動 # systemctl restart apache2 |
3. メールサーバーにSSL/TLS(Let’s Encrypt) の設定
3.1 バーチャルホスト設定と証明書取得
①Apacheのバーチャルホストにメールホストを設定
# cd /etc/apache2/sites-available/ # vi vhost-hoge.com.conf |
<VirtualHost *:80> ~省略~ ServerName mail.hoge.com ServerAdmin hoge@hoge.com 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> |
②/var/www/html/にmail.hoge.comディレクトリー作成
③メールサーバー用のletsencrypt SSL証明書の取得
# certbot certonly –webroot -w /var/www/html/mail.hoge.com -d mail.hoge.com |
3.2 Postfix の設定
④Postfixの設定ファイルを修正
暗号化通信ができるよう SSL/TLS の設定をします。
SMTPS は 465/TCP, POP3S は 995/TCP, IMAPS は 993/TCP を使用します。
# vi /etc/postfix/main.cf # 最終行に追記 |
# vi /etc/postfix/master.cf submission inet n – y – – smtpd ←以下、コメントを外します。(SSL,STARTTLSどちらにも対応) smtps inet n – y – – smtpd |
設定に誤りがないかチェック(なにも表示されなければOKです)
# postfix check |
⑤Postfixの起動、自動起動の設定
# systemctl start postfix systemctl enable postfix |
3.3 Dovecot の設定
①Dovecotの設定ファイルを修正
# vi /etc/dovecot/conf.d/10-ssl.conf # 6行目:変更 |
・暗号化を使う imaps と pop3s を有効にして、平文で通信する imap と pop3 は「port = 0」を設定し無効にします。
# vi /etc/dovecot/conf.d/10-master.conf |
service imap-login { inet_listener imap { #port = 143 port = 0 } inet_listener imaps { #port = 993 #ssl = yes port = 993 ssl = yes } }service pop3-login { inet_listener pop3 { #port = 110 port = 0 } inet_listener pop3s { #port = 995 #ssl = yes port = 995 ssl = yes } } ・Dovecot SASL ライブラリの認証ソケットファイルを指定(113行目あたりです)service auth { (略) # Postfix smtp-auth #unix_listener /var/spool/postfix/private/auth { # mode = 0666 #} ↓ unix_listener /var/spool/postfix/private/auth { mode = 0666 user = postfix group = postfix } } |
・認証方式の設定 ※平文パスワードを許可していますが、SSL/TLSで暗号化されますので問題ありません
# vi /etc/dovecot/conf.d/10-auth.conf #disable_plaintext_auth = yes → disable_plaintext_auth = no |
・メールボックスの場所を指定
# 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 = |
4. FTP Vsftpd にSSL/TLS(Let’s Encrypt) の設定
① Vsftpdの設定ファイルを修正
# vi /etc/vsftpd.conf #次を最終行に追記 # add letsencrypt rsa_cert_file=/etc/letsencrypt/live/hoge.com/fullchain.pem rsa_private_key_file=/etc/letsencrypt/live/hoge.com/privkey.pem ssl_enable=YESforce_local_data_ssl=YES force_local_logins_ssl=YES ssl_tlsv1=YES ssl_sslv2=NO ssl_sslv3=NO#pasv port pasv_enable=YES pasv_min_port=21000 pasv_max_port=21010再起動 # systemctl restart vsftpd |
② Firewallの設定
ftpポート以外に固定したPASVポートを許可します。
# ufw allow 21000:21010/tcp # ufw reload |