Digitra

LINUXサーバの設定やプログラムのことなどを中心にブログを書いています。

EC2でLet's Encrypt + Nginx を試してみる


Let's Encryptという誰でも無料で使えるSSL/TLS証明書発行サービスがPublic Betaを始めました。
https://letsencrypt.org/


前回、AWSのEC2インスタンスでNgnix+PHP7環境を作ったので、この環境にLet's Encryptの証明書を入れてみます。



Gitクライアントを入れて、letsencryptのライブラリを持ってくる。

 
sudo yum install git
git clone https://github.com/letsencrypt/letsencrypt


Nginxを一旦停止する

 
sudo service nginx stop


証明書を発行する

./letsencrypt-auto certonly --standalone -d [ドメイン名] --debug


※Nginxが既に動いていると怒られる。(stopする)
ドメインの設定が完了してないと怒られる。(nslookup出来るか確認)

メールアドレス、ドメインを設定する。

Nginxの設定

 
    # HTTPS server
    #
    server {
        listen       443;
        server_name  dev.digitra.net;
        root         html;

        ssl                  on;
        #ssl_certificate      cert.pem;
        ssl_certificate      /etc/letsencrypt/live/[ドメイン名]/cert.pem;
        #ssl_certificate_key  cert.key;
        ssl_certificate_key  /etc/letsencrypt/live/[ドメイン名]/privkey.pem;

        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

        #charset koi8-r;

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

        # redirect server error pages to the static page /40x.html
        #
        error_page 404 /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
            include        fastcgi_params;
        }
   }

Nginxを起動する

$ sudo service ngnix start

 

証明書を更新する

Let's Encryptの証明書の有効期限は90日となっている。(推奨は60日)
なので、定期的に証明書を更新する必要がある。

以下の様なスクリプトをcronに仕込んで自動更新するようにする。

#スクリプト(letsencrypt_renew.sh)
#!/bin/sh
/home/ec2-user/letsencrypt/letsencrypt-auto certonly --webroot -w /var/www/html -d [ドメイン名] --renew-by-default --debug
service httpd restart
#cronの設定
00 05 01 * * /home/ec2-user/letsencrypt_renew.sh

 

ハマったところ

自動更新がうまくいかない
 - The following errors were reported by the server:

   Domain: [ドメイン名]
   Type:   unauthorized
   Detail: Invalid response from http://[ドメイン名]/.well-known
   /acme-challenge/l14dd-AOtBbzXxxxxx7r1xMTzPaDzJnTBuTMkwfRTv4s
   [ドメインのIP]: 404

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A record(s) for that domain
   contain(s) the right IP address.

 こんな感じのエラーが出ていて、自動更新ができていなかった。

よくよくチェックしてみるとスクリプトのwebrootのディレクトリ指定が間違っていて、.well-knownディレクトリが間違ったところに生成されていた。

webrootの指定を正しく指定すると正常に更新ができた。