Amazon Linux + Nginx のマルチドメイン環境に無料SSL証明書Let’s EncryptでHTTPS化する
最近色々なレンタルサーバー等でも利用できるようになった無料SSL証明書のLet’s EncryptですがAmazon Linux + Nginxの環境でも利用できるようにしたいと思います。
少し前に比べて導入がとても簡単になりました。
今では日本語ドメインにも対応しています。
バーチャルホストで複数のドメインを設定している環境で導入する方法になります。(シングルドメインでも手順は同じです)
現時点ではAmazon Linuxは正式にサポートされていませんが、以下の手順で導入可能です。
前提条件:
1.Amazon Linux + Nginxでサイトが構築されている事
2.DNSレコードの設定が完了して、例:http://hogehoge.com/でアクセスできる事
certbot-autoのダウンロード
今回は、certbot-autoを/usr/local/binにダウンロードします。
[ec2-user@ip-172-11-1-1 ~]$ sudo su←suになっておく [ec2-user@ip-172-11-1-1 ~]# cd /usr/local/bin←ディレクトリ移動 [ec2-user@ip-172-11-1-1 bin]# wget https://dl.eff.org/certbot-auto←ダウンロード [ec2-user@ip-172-11-1-1 bin]# chmod a+x certbot-auto←certbot-autoに実行権限を付ける
certbot-autoを実行する
実行時には必ず-nginx -debugオプションを付けるようにしてください。
それぞれのオプションの説明
–nginx
certbotはnginxやapacheの設定ファイルを自動で確認します。
このオプションを付けないとデフォルト設定のapacheの設定ファイルを見に行ってしまうため、
対象ドメインを正しく取得できずにエラーになる可能性があります。
–debug
Amazon Linuxはcertbotで正式サポートされていない為、このオプションが無いと止まってしまいます。
[ec2-user@ip-172-11-1-1 bin]# ./certbot-auto --nginx --debug←/usr/local/binのディレクトリで実行します。 Complete!←Pythonの環境等を自動で作成してくれました。 ~~~~~~~ ←ここから5つの質問に答えます。 Enter email address (used for urgent renewal and security notices) (Enter 'c' tocancel): hoge@hogehoge.com←通知用メールアドレスを入力してEnter ------------------------------------------------------------------------------- Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree in order to register with the ACME server at https://acme-v01.api.letsencrypt.org/directory ------------------------------------------------------------------------------- (A)gree/(C)ancel:A←利用規約の同意でA ------------------------------------------------------------------------------- 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 EFF and our work to encrypt the web, protect its users and defend digital rights. ------------------------------------------------------------------------------- (Y)es/(N)o: N←非営利団体からのメールを受け取るかとあるのでN Which names would you like to activate HTTPS for? ------------------------------------------------------------------------------- 1: hogehoge.com 2: hoge.net ------------------------------------------------------------------------------- Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel): 1,2←証明書を発行するドメインを選択します。 複数の場合はカンマ区切りで数字を区切ります。1つの場合は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←HTTPでのアクセスをHTTPSにリダイレクトさせるには2を選択します。 ------------------------------------------------------------------------------- Congratulations! You have s~~~~~~~~←Congratulationが表示されたら完了しています。
確認してみると、Nginxの設定ファイルや、バーチャルホスト用のconfファイルのserverディレクトリに自動的に設定が追加されています。
以上でSSLの設定が完了していますので、ブラウザでhttpsでアクセスして確認してください。
cert-bot/letsencrypt nginx `UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe3 in position 12: ordinal not in range(128)`
インストール時にErrorが出て、ログにこちらのエラーメッセージが表示されている場合。
Nginxの設定ファイル(nginx.conf)やバーチャルホストの設定ファイル(****.conf)にコメント等で日本語が使われていないか確認してください。
日本語が使われていた場合、削除するか英語に変更してください。
自動更新設定
Let’s Encryptは3ヶ月ごとに更新が必要となる為、cronを使って自動更新するようにします。
[ec2-user@ip-172-11-1-1 ~]$ sudo su←suになっておく [ec2-user@ip-172-11-1-1 ~]# crontab -e←cron設定 05 00 01 * * service nginx stop && /usr/local/bin/certbot-auto renew --force-renew --debug --nginx && service nginx start←毎月1日の0時5分に更新するようにする :wq
以上で毎月更新されます。
最初のうちは1ヶ月ごとに確認するようにしてください。