RapidSSL+NginxでA+環境を作る
RapidSSLとNginxでSSL LabsにてA+が出る環境を作るための方法を紹介します。
以下、やることです。
- Certbotのインストール
- SSL証明書の作成
- dhparamの作成
- Nginxの設定ファイル作成
※Nginxがインストールされている前提で話を進めます。
Certbotのインストール
証明書をインストールするためにCertbotをインストールします。
$ curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto $ chmod 700 /usr/bin/certbot-auto
SSL証明書の作成
以下を実行して、証明書の作成を行う。nginxに設定を追加するか聞かれるがお好みで。
$ cd /usr/bin $ ./certbot-auto --nginx -d ドメイン名 --email info@ドメイン名
dhparamの作成
DH鍵交換に使用するファイルを作成します。今回は/etc/dhparam/ドメイン名以下に作成。
$ cd /etc/dhparam/ドメイン名 $ sudo openssl dhparam 2048 -out dhparam.pem
Nginxの設定ファイル作成
私はよくRailsを使用するので、Railsの設定が残っていますが、Nginxの設定ファイルは以下のようになります。
upstream unicorn { server unix:/var/www/アプリケーション名/current/tmp/sockets/unicorn.sock; } server { listen 80; server_name ドメイン名; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name ドメイン名; ssl_certificate /etc/letsencrypt/live/ドメイン名/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ドメイン名/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4"; ssl_prefer_server_ciphers on; ssl_dhparam /etc/dhparam/ドメイン名/dhparam.pem; add_header Strict-Transport-Security "max-age=15768000; includeSubdomains"; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; client_max_body_size 100m; error_page 404 /404.html; error_page 500 502 503 504 /500.html; try_files $uri/index.html $uri @unicorn; root /var/www/アプリケーション名/current/public; location ~ ^/assets/ { root /var/www/アプリケーション名/current/public; } location @unicorn { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Proto https; proxy_redirect off; proxy_pass http://unicorn; } }
おまけ:更新の自動化
RapidSSLは3ヶ月ごとに更新が必要なので、Cronで自動更新ができるようにしておきます。 Cronで実行するコマンドが正しく実行されるかチェックします。
$ sudo /usr/bin/certbot-auto renew --post-hook "sudo /etc/init.d/nginx restart"
こちらを実行すると以下のような結果が得られるかと思います。
Saving debug log to /var/log/letsencrypt/letsencrypt.log ------------------------------------------------------------------------------- Processing /etc/letsencrypt/renewal/ドメイン名.conf ------------------------------------------------------------------------------- Cert not yet due for renewal ------------------------------------------------------------------------------- The following certs are not due for renewal yet: /etc/letsencrypt/live/ドメイン名/fullchain.pem expires on 2018-06-17 (skipped) No renewals were attempted. No hooks were run. -------------------------------------------------------------------------------
こちらが表示されることを確認したら、/etc/cron.d以下にcertbotというファイルを以下の内容で作成します。
※毎月1日の朝3時に実行
0 3 1 * * root /usr/bin/certbot-auto renew --post-hook "/etc/init.d/nginx restart"