Amazon Linux2とLet's EncryptでSSL対応サーバを0から爆速構築


どんな話?

以前Amazon Linux2でLet's Encrypt使おうとしたらコケた話という記事で
certbotを書き換えて認証なんてことをしちゃてたわけなのですが、これをする必要はもうありません。

EPEL7を有効化してcertbotと一緒にpython2-certbot-apache もしくは python2-certbot-nginxをインストール
あとはcertbotコマンドを実行するだけです。
すっごい楽ですね!
実際、新規環境で試した際は5分もかかりませんでした。

また、python-certbot-○○は他のディストリビューションでも利用可能ですので参考にしていただけると思います。

ということでご紹介します。

実際にやってみる

Amazon linux2インスタンスの立ち上げとドメイン割当までは完了しているものとします。
また、Apacheでの設定方法についてはAWSのドキュメントに記載がありますので、今回はnginxにします。

nginxのインストール

今回はサクッとextrasレポジトリからインストールします。

コマンドを実行してnginxのバージョンを確認
今回は1.12.1。

$ amazon-linux-extras list
0  ansible2                 available    [ =2.4.2  =2.4.6 ]
2  httpd_modules            available    [ =1.0 ]
3  memcached1.5             available    [ =1.5.1  =1.5.16 ]
4  nginx1.12                available    [ =1.12.2 ]
5  postgresql9.6            available    [ =9.6.6  =9.6.8 ]

nginx1.12をインストール。

$ sudo amazon-linux-extras install nginx1.12

nginxを起動して終了。

$ sudo systemctl enable nginx
$ sudo systemctl start nginx

EPEL7の有効化

EPEL7をダウンロードして、

$ sudo wget -r --no-parent -A 'epel-release-*.rpm' http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/

リポジトリパッケージをインストール。

$ sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm

最後にEPEL7を有効化。

$ sudo yum-config-manager --enable epel*

certbotのインストール

$ sudo yum install certbot python2-certbot-nginx

Let's Encryptの証明書を取得と設定

以下のコマンドで証明書取得を開始。

$ sudo certbot --nginx

アドレスを聞かれるので入力。
このアドレスに重要な連絡が来るようになります。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

ライセンスへの同意を求められるので従うことができる場合はaを入力。
当然ですが同意しないと使えません。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: a

キャンペーンメール等を送るためEFFとアドレスを共有してもいいか聞かれる。
同意できればy、同意できなければn。
これはどちらでもオッケーです。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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

nginxをインストールしたてのまっさらな状態だとドメイン名は設定されていないのでここで聞かれる。
利用するドメイン名を入力。

No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated)  (Enter 'c' to cancel): example.com

ドメイン認証、鍵取得、nginxへ鍵設定までが実施されるので待機。
設定はnginx.confに書き込まれる。

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/nginx.conf

HTTPアクセスをHTTPSにリダイレクト設定をするか聞かれる。
設定しない場合は1、設定しない場合2を入力。
今回は2を選択してみましたが、ドメイン直下のリダイレクトがうまくいかなかったので
個人的には1を選んで自分で設定する方法がおすすめです。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/nginx.conf

以下のように成功メッセージが出れば完了。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2019-09-25. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot 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

自動更新設定

メッセージにもあるようにcertbot renewコマンドで証明書更新ができます。
AWSのドキュメントでは--no-self-upgradeをつけるようになっているので安定性を考えるとそちらがベストでしょう。

これをCronにでも突っこんでおけば何もしなくても更新されるのでやっておきましょう。

crontabコマンドを実行して

$ sudo crontab -u root -e

以下のように書き加えればOKです。この例では毎日0:00と12:00にroot権限で実行されます。
(AWSでは1:39と13:39でした。golangの"2009-11-10 23:00:00 UTC"のようなおもしろい理由があるんでしょうか…?)

0 0,12 * * * root certbot renew --no-self-upgrade

最後に設定されているか確認すれば完了です。

$ sudo crontab -u root -l
0 0,12 * * * root certbot renew --no-self-upgrade

最後に

簡単、爆速ですね。
extpectコマンドも併せて使ってやれば、ほとんどすることなくなっちゃうんじゃないでしょうか?
これが無料でできるなんていい時代だなー!