CentOS7

Webサーバー間通信内容暗号化(nginx+certbot)

Let's Encryptは無料で利用できて商用利用可能な証明書発行サービスです。https化に必要なSSL証明書が無料で発行されるなんて、良い時代になりましたね。

しかし証明書の有効期限は90なのでその度に、更新作業は面倒なので、自動化してしまいましょう。 またサブドメインにも対応可能可能なようにワイルドカードで証明書を作成します。

Lets's Encryptの導入

[root@localhost ~]# cd /usr/local/ ← ディレクトリ移動
[root@localhost local]# git clone https://github.com/certbot/certbot ← Certbotクライアントダウンロード
[root@localhost local]# cd ← ディレクトリ移動
[root@localhost ~]# /usr/local/certbot/certbot-auto -n ← Certbotクライアントインストール

nginxを一旦停止

[root@localhost ~]# systemctl stop nginx

SSL証明書発行

[root@localhost ~]# /usr/local/certbot/certbot-auto certonly --manual -d *.hoge.com -m <メールアドレス> --agree-tos --manual-public-ip-logging-ok --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory
指示に従ってすすめていきます。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N ←Nを入力してエンター

Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for hoge.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.hoge.com with the following value: ←DNSに登録するTXTレコード

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ←DNSに登録するTXTレコードのバリュー値(ワンタイムトークン)

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue ←DNSにTXTレコードを登録するまで待機する

DNSサーバーにTXTレコード追加

運用中のDNSサーバーにcertbotから指定されたTXTレコードを追加します。

例えば「hoge.com」にSSL証明書を発行する場合は、bind9の「hoge.com」ゾーン設定ファイルに、

_acme-challenge IN TXT [ワンタイムトークン]

を追記して、DNSを再起動し情報が更新されたタイミングでTXTレコードを引くとワンタイムトークンを返すようになる。

[root@localhost ~]# dig _acme-challenge.hoge.com txt
; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> _acme-challenge.hoge.com txt
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60133
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;_acme-challenge.hoge.com. IN TXT

;; ANSWER SECTION:
_acme-challenge.hoge.com. 300 IN TXT "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

;; Query time: 36 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: 日 8月 26 10:23:04 JST 2018
;; MSG SIZE rcvd: 114

と返ってくるようになったらOK。

「;; ANSWER SECTION:」にワンタイムトークンが返ってこないうちに先に進むと失敗する。

返ってくるようになったら、リターンを押す

Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/hoge.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/hoge.com/privkey.pem
Your cert will expire on 2018-11-24. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- Your account credentials have been saved in your Certbot
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 Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

dhparam用ファイルの作成

nginxSSLを設定する際に、必要なパラメーターとして”ssl_dhparam”があります。これはnginx1.11.0から必須になったパラメーターで、鍵交換アルゴリズムで使用される素数を格納するファイルになります。

このファイルはopensslコマンドで作成する必要があります。ファイルを保存するパスは”/etc/nginx/ssl/”を指定します。

ファイルの保存用ディレクトリ作成

[root@localhost ~]# mkdir /etc/nginx/ssl

opensslコマンドによるファイルの作成

[root@localhost ~]# openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
................................................+..................................................
....................+..............................................................................
【略】
........................................+..........................................+...............
.............++*++*

サーバーの性能によっては、かなり時間が掛かりますが、途中で止めずに暫く待ってみて下さい。エラーが表示されずにプロンプトに戻れば、コマンドの実行は完了です。

Webサーバ(Ngninx)の設定

[root@localhost ~]# vi /etc/nginx/sites-available/hoge.com.conf
server {
listen 80;
server_name hoge.com www.hoge.com;
rewrite ^ https://$server_name$request_uri? permanent;
}

server {
listen 443 ssl;
server_name hoge.com www.hoge.com;
access_log /var/log/nginx/hoge.com-access.log main;
error_log /var/log/nginx/hoge.com-error.log;
root /home/www/hoge.com;

ssl_certificate /etc/letsencrypt/live/hoge.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/hoge.com/privkey.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets on;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
index index.php index.html;
}

location ~ \.php$ {
root /home/www/hoge.com
try_files $uri $uri/ =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@localhost ~]# systemctl restart nginx ←nginx再起動

cronで自動更新の設定

Certbotのサーバー証明書の有効期間は3ヶ月のため、毎月自動でサーバー証明書を更新するようにする。

[root@localhost ~]# vi /etc/cron.monthly/certbot ← サーバー証明書自動更新スクリプト作成
#!/bin/sh
log=`mktemp`
code=0

#
# 証明書更新
#
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 -A 1 webroot_map /etc/letsencrypt/renewal/${conf} | grep = | awk '{print $3}'`

# 証明書更新
/usr/local/certbot/certbot-auto 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
/usr/local/certbot/certbot-auto certonly -a standalone \
-d ${domain} --renew-by-default >> ${log} 2>&1
[ $? -ne 0 ] && cat ${log}
fi
fi
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}
[root@localhost ~]# chmod +x /etc/cron.monthly/certbot ← サーバー証明書自動更新スクリプトへ実行権限付加

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-08-30 (木) 18:08:48