zuntan02のはてなブログ

備忘録的なものです。時々職場の技術者ブログにも転記してますが、メインはこちらで。

AmazonLinux+NginxにLet's Encrypt

AWS EC2で運用しているWebサイト(Nginx)にLet's EncryptでSSL証明書設置したメモ。

【作業】

証明書発行用ドキュメントルート用意

Let's Encryptで証明書発行(ドメイン使用権認証)用のファイルを設置するドキュメントルートが必要です。
ここでは、既存のvhostについて、Let's encrypt が webroot 更新で使う部分だけを別のディレクトリに設定しています。

mkdir /srv/www/letsencrypt_webroot
chown nginx:nginx /srv/www/letsencrypt_webroot

# nginx conf
cd /etc/nginx/conf.d
vi hogehoge.conf
以下を追記する

location /.well-known/ {
    root  /srv/www/letsencrypt_webroot;
}
nginx書式テスト&反映

nginx -t
service nginx reload

Let's Encrypt インストール

1. AWSのセキュリティグループを設定

→サーバに適用されているセキュリティグループのインバウンドのHTTP(80)、HTTPS(443)を許可(0.0.0.0/0)にしておきます。

2. certbotのダウンロード&配置&権限設定
curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto
chmod 700 /usr/bin/certbot-auto
3. 証明書の発行

cerbotのコマンドで証明書が自動生成されます。
※2018年1月までワイルドカード証明書は発行できないらしいので、今回はhogehoge.comとwww.hogehoge.comの、www有無それぞれで発行します。

certbot-auto certonly --webroot -w /srv/www/letsencrypt_webroot -d hogehoge -d www.hogehoge --email admin@hogehoge --debug

→ 不足している必須モジュールがインストールされる

※ --debugについて:
これつけないと、AmazonLinuxでは以下のメッセージが出るもよう。

FATAL: Amazon Linux support is very experimental at present...
if you would like to work on improving it, please ensure you have backups
and then run this script again with the --debug flag!
Alternatively, you can install OS dependencies yourself and run this script
again with --no-bootstrap.

※そのほかcertbot-autoのオプションについては下端にメモしておきました。

※その他のエラーが出た場合の対応メモ
エラー1

/usr/bin/certbot-auto: line 700: virtualenv: command not found

→ virtualenvは「/usr/bin/virtualenv-2.7」以下に存在したので、update-alternativesでリンクを張る

# update-alternatives設定
update-alternatives --install /usr/bin/pip pip /usr/bin/pip-2.7 27
update-alternatives --install /usr/bin/virtualenv virtualenv /usr/bin/virtualenv-2.7 27

# 確認
update-alternatives --display pip
update-alternatives --display virtualenv

エラー2

Error: couldn't get currently installed version for /opt/eff.org/certbot/venv/bin/letsencrypt:

→参考
Let's Encrypt 証明書更新時のエラー対処 Amazon Linux AMI - とうふの雑記帳



以下対話式で進める

-------------------------------------------------------------------------------
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-staging.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: 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: Y

(省略)

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/hogehoge/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/hogehoge/privkey.pem
   Your cert will expire on 2017-12-04. 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"
(省略)
4. 生成された証明書の確認

以下のディレクトリに証明書が生成されます。

/etc/letsencrypt/live/hogehoge/
5.nginx側に設定
server {
    listen 443 ssl;
    ssl on;
(省略)
    ssl_certificate     /etc/letsencrypt/live/hogehoge/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/hogehoge/privkey.pem;
(省略)
6.確認

https://hogehoge/
https://www.hogehoge/
SSL通信できていることを確認
https://www.ssllabs.comで確認してランクAであることを確認

7.証明書の自動更新

certbot-auto renew

# →crontabに設置する:例
crontab -e

50 3 * * 0 /usr/bin/certbot-auto renew --post-hook "/etc/init.d/nginx reload" > /path/to/log 2>&1

追記
設置環境により証明書更新時に以下の様なエラーを確認した

Failed to find executable service in expanded PATH: /usr/bin:/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Unable to find post-hook command service in the PATH.
(PATH is /usr/bin:/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin)

上記はpost-hookが実行するコマンドにpathが通っていないから。
(ex:service nginx reloadなどのばあい、/sbin/service/nginxなどとする必要がある)

【注意】
https://への総301転送を行っている様なサイトの場合、.well-knownだけはhttpに落とさせる必要がある模様
例)

     # for lets encrypt
         location /.well-known/ {
             root  /hoge/fuga/letsencrypt_webroot;
         }

     # 301 redirect from old URL to new URL
         location / {
             return 301 https://hogefuga.jp$request_uri;
         }

のようにしておくとよい

certbot-auto オプション説明

certonly

SSL/TLS サーバ証明書の取得のみを行います。

--webroot

→ウェブサーバの DocumentRoot ディレクトリ以下に認証用のファイルを設置することでドメイン使用権者の認証を行って、SSL/TLS サーバ証明書を取得します。
Certbot クライアントを実行するコンピュータでウェブサーバ(httpd)が動作中であり、ウェブサーバを停止することなく SSL/TLS 証明書を発行したい場合には、Webroot プラグインを利用する必要がある(https://letsencrypt.jp/docs/using.html***webroot

--webroot-path WEBROOT_PATH もしくは -w WEBROOT_PATH

→"public_html" や "webroot" のパスを指定します。

-d DOMAIN もしくは --domains DOMAIN もしくは --domain DOMAIN

SSL/TLS サーバ証明書の取得を申請するドメイン名を指定します。

--post-hook POST_HOOK

証明書を取得・更新する試みが終わった後にシェルで実行するコマンドを指定します。
これは、証明書を取得・更新する試みが行われた場合のみ実行されます。