アフォでも出来る

アフォでも出来るように記録します(IT系)

OCIでメールサーバー構築ーその4 Webサーバーhttps化

メールサーバー構築するのにWebサーバーをある程度作り込むのは,メールサーバーの設定をWebから操作したいからです。メーリングリストやアカウントの追加等々をWebから行えるようにします。そうすれば,Linuxがまったく分からない人をメールの管理者にする事もできます。

 

Certbotの導入(無料の証明書を発行して自動更新)

sudo rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y install python2-certbot-apache

サーバー証明書取得

sudo certbot certonly --webroot -w /var/www/html -m test@testtest.pgw.jp -d testtest.pgw.jp --agree-tos

グリーンの引数は前から,ドキュメントルート,メールアドレス,サイトURLです。と言ってもメールアドレスは何でもよい。サイトURLは事前にパブリックIPアドレスと関連付けておく事。尋ねられたらy。

 

・mod_sslインストール

sudo yum -y install mod_ssl

 

Apacheの設定

sudo vi /etc/httpd/conf.d/ssl.conf

75行目くらいを以下のように。脆弱性のある暗号化は使用しない。
SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1

100行目くらいを以下のように調整。グリーン部分は上記でセットしたURL。

SSLCertificateFile /etc/pki/tls/certs/localhost.crt

  ↓

SSLCertificateFile /etc/letsencrypt/live/testtest.pgw.jp/cert.pem

 

107行目くらいを以下のように調整。グリーン部分は上記でセットしたURL。

SSLCertificateKeyFile /etc/pki/tls/private/localhost.key

  ↓

SSLCertificateKeyFile /etc/letsencrypt/live/testtest.pgw.jp/privkey.pem

 

116行目くらいにコメントアウトしてある行があるので,その下に以下を挿入。

#SLCertificateChainFile /etc/pki/tls/certs/server-chain.crt

  ↓

SSLCertificateChainFile /etc/letsencrypt/live/testtest.pgw.jp/chain.pem

 

80行目くらいをコメントアウト

SSLCipherSuite HIGH:3DES:!aNULL:!MD5:!SEED:!IDEA

  ↓

#SSLCipherSuite HIGH:3DES:!aNULL:!MD5:!SEED:!IDEA

94行目に以下を挿入(コメントアウトした2行にそれぞれ対応)

SSLCipherSuite CDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH

+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
SSLHonorCipherOrder on

 

最終行が</VirtualHost>なので,その上に以下を挿入

Header always set Strict-Transport-Security "max-age=15768000"

 

64行目,65行目くらいを以下のように調整。

ErrorLog logs/ssl_error_log

  ↓

ErrorLog logs/error_log

TransferLog logs/ssl_access_log

  ↓

TransferLog logs/access_log

 

Apache再起動

sudo systemctl reload httpd

 

Certbotの証明書の自動更新

スクリプト作成

sudo vi /etc/cron.monthly/certbot

以下を貼り付け。

#!/bin/bash
log=`mktemp`
code=0

which certbot > /dev/null 2>&1
if [ $? -eq 0 ]; then
CERTBOT=`which certbot`
else
CERTBOT=`which certbot-auto`
fi

#
# 証明書更新
#
for conf in `ls /etc/letsencrypt/renewal/`
do
# ドメイン名取得
domain=`echo ${conf}|sed -e 's/\([^ ]*\)\.conf/\1/p' -e d`

# 認証方式取得
authenticator=`grep authenticator /etc/letsencrypt/renewal/${conf}|awk '{print $3}'`

if [ ${authenticator} = 'webroot' ]; then
# Web認証の場合

# ドキュメントルート取得
webroot=`grep webroot_path /etc/letsencrypt/renewal/${conf}|grep =|awk '{print $3}'|awk -F '[,]' '{print $1}'`

# 証明書更新
${CERTBOT} certonly --webroot \
-w ${webroot} -d ${domain} --renew-by-default >> ${log} 2>&1
[ $? -ne 0 ] && cat ${log}
else
# スタンドアロン認証の場合

# 証明書更新
lsof -i:80 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo 'Webサーバー稼働中のためスタンドアロン認証不可'
else
${CERTBOT} certonly -a standalone \
-d ${domain} --renew-by-default >> ${log} 2>&1
[ $? -ne 0 ] && cat ${log}
fi
fi

# 古い証明書を削除
find /etc/letsencrypt/archive/${domain}/ -mtime +30 -delete

done

#
# 証明書更新反映
#

# Webサーバー設定再読込み
lsof -i:443 > /dev/null 2>&1
if [ $? -eq 0 ]; then
rpm -q systemd > /dev/null 2>&1
if [ $? -eq 0 ]; then
systemctl reload httpd
else
/etc/rc.d/init.d/httpd reload > /dev/null 2>&1
fi
fi

# SMTPサーバー設定再読込み
lsof -i:465 > /dev/null 2>&1
if [ $? -eq 0 ]; then
rpm -q systemd > /dev/null 2>&1
if [ $? -eq 0 ]; then
systemctl reload postfix
else
/etc/rc.d/init.d/postfix reload > /dev/null 2>&1
fi
fi

# IMAPサーバー設定再読込み
lsof -i:995 > /dev/null 2>&1
if [ $? -eq 0 ]; then
rpm -q systemd > /dev/null 2>&1
if [ $? -eq 0 ]; then
systemctl reload dovecot
else
/etc/rc.d/init.d/dovecot reload > /dev/null 2>&1
fi
fi

#
# ログをsyslogへ出力後削除
#
cat ${log}|logger -t `basename ${0}` ; rm -f ${log}

実行権限付与。

sudo chmod +x /etc/cron.monthly/certbot

 【参考サイト】

centossrv.com