Let's Encrypt 証明書発行

Nid: 1170

1 . Certbot インストール

$ sudo apt-get update
$ sudo apt install python-certbot-nginx

2 . Diffie-Hellman Group 生成

$ sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

3 . Configuration Snippet 作成

$ sudo mkdir -p /var/lib/letsencrypt/.well-known
$ sudo chgrp www-data /var/lib/letsencrypt
$ sudo chmod g+s /var/lib/letsencrypt
$ ls -ld /var/lib/letsencrypt
drwxr-sr-x 3 root www-data 4.0K Dec 16 23:49 /var/lib/letsencrypt/

 

$ sudo vi /etc/nginx/snippets/ssl-params.conf
ssl_dhparam /etc/ssl/certs/dhparam.pem;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
ssl_prefer_server_ciphers on;

ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 30s;

add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;

4 . Nginx Server Blocks 設定

$ sudo vi /etc/nginx/sites-available/octaviadata.com
server {
    listen 80;
    server_name octaviadata.com www.octaviadata.com;
    include snippets/ssl-params.conf;
}
$ sudo ln -s /etc/nginx/sites-available/octaviadata.com /etc/nginx/sites-enabled/
$ sudo nginx -t && sudo nginx -s reload

80番での接続を確認。

5 . Let’s Encrypt SSL 証明書の取得

 

$ sudo certbot --nginx certonly --agree-tos -w /var/lib/letsencrypt/ -d octaviadata.com,www.octaviadata.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx

Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: octaviadata.com
2: www.octaviadata.com
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

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): 1

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://octaviadata.com and
https://www.octaviadata.com

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

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/octaviadata.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/octaviadata.com/privkey.pem
   Your cert will expire on 2019-03-16. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. 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

更新期限の確認

$ sudo openssl x509 -in /etc/letsencrypt/live/octaviadata.com/cert.pem -noout -dates
notBefore=Dec 16 23:01:27 2018 GMT
notAfter=Mar 16 23:01:27 2019 GMT

6 . SSL 設定

$ sudo vi /etc/nginx/sites-available/octaviadata.com
server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/html/octaviadata.com;
    index index.php index.html index.htm;
    server_name octaviadata.com www.octaviadata.com;

    ssl_certificate /etc/letsencrypt/live/octaviadata.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/octaviadata.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/octaviadata.com/chain.pem;
...
$ sudo nginx -t && sudo nginx -s reload

証明書内容をインターネットで確認

$ echo | openssl s_client -connect octaviadata.com:443 2>/dev/null | openssl x509 -noout -text | head
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            03:eb:34:e3:a3:97:91:1b:94:b1:d2:77:dd:6e:de:f5:59:c4
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
        Validity
            Not Before: Dec 16 23:01:27 2018 GMT
            Not After : Mar 16 23:01:27 2019 GMT

7 . 転送する場合の設定

80番->443番, www->non www

$ sudo vim /etc/nginx/sites-available/octaviadata.com

# force SSL
server {
    listen 80;
    listen [::]:80;
    server_name octaviadata.com www.octaviadata.com;
    include snippets/letsencrypt.conf;
    return 301 https://$server_name$request_uri;
}

# www.octaviadata.com -> octaviadata.com
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.octaviadata.com;
    ssl_certificate /etc/letsencrypt/live/octaviadata.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/octaviadata.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/octaviadata.com/chain.pem;
    include snippets/letsencrypt.conf;
    include snippets/ssl-params.conf;
    return 301 https://octaviadata.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/html/octaviadata.com;
    index index.php;
    server_name octaviadata.com;

    ssl_certificate /etc/letsencrypt/live/octaviadata.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/octaviadata.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/octaviadata.com/chain.pem;
    include snippets/letsencrypt.conf;
    include snippets/ssl-params.conf;
...

設定反映

$ sudo nginx -t && sudo nginx -s reload

curl で動作確認する例

$ curl -s -I -L http://www.octaviadata.com/ | grep -i ^location
Location: https://octaviadata.com/
$ curl -s -I -L http://octaviadata.com/ | grep -i ^location
Location: https://octaviadata.com/
$ curl -s -I -L https://www.octaviadata.com/ | grep -i ^location
location: https://octaviadata.com/

8 . 証明書自動更新設定

Nginx リロード設定を追記 (--renew-hook "systemctl reload nginx")

$ sudo vi /etc/cron.d/certbot
# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc.  Renewal will only occur if expiration
# is within 30 days.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && sleep $((RANDOM \% 3600)) && certbot -q renew --renew-hook "systemctl reload nginx"

エラーがないことを確認

$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/octaviadata.com.conf
-------------------------------------------------------------------------------
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for octaviadata.com
http-01 challenge for www.octaviadata.com
Waiting for verification...
Cleaning up challenges

-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/octaviadata.com/fullchain.pem
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/octaviadata.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
-------------------------------------------------------------------------------

IMPORTANT NOTES:
 - 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.