Simplest way to setup Nginx + Let’s Encrypt + Ubuntu 16.04

Step 1: Install Nginx

  • sudo apt-get update
  • sudo apt-get install nginx

Step 2: Install Let’s Encrypt Client

  • sudo apt-get update
  • sudo apt-get install letsencrypt

Step 3: Obtain an SSL Certificate

  • sudo nano /etc/nginx/sites-available/default
  • Add to SSL server block
    location ~ /.well-known {
     allow all;
     }
  • sudo systemctl restart nginx

Step 4: Request an SSL certificate

It will save the files in /etc/letsencrypt/live/www.yourdomain.com/.

Step 5: Create a /etc/nginx/snippets/ssl.conf

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

ssl_protocols TLSv1.2;
ssl_ciphers EECDH+AESGCM:EECDH+AES;
ssl_ecdh_curve secp384r1;
ssl_prefer_server_ciphers on;

ssl_stapling on;
ssl_stapling_verify on;

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

Step 6: Configure Server Block

sudo nano /etc/nginx/sites-available/default

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name yourdomain.com www.yourdomain.com;

include /etc/nginx/snippets/letsencrypt.conf;

location / {
return 301 https://www.mydomain.com$request_uri;
}
}


server {
server_name www.yourdomain.com;
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server ipv6only=on;

ssl_certificate /etc/letsencrypt/live/www.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.yourdomain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/www.yourdomain.com/fullchain.pem;
include /etc/nginx/snippets/ssl.conf;

root /var/www/yourdomain.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}


server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name yourdomain.com;

ssl_certificate /etc/letsencrypt/live/www.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.yourdomain.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/www.yourdomain.com/fullchain.pem;
include /etc/nginx/snippets/ssl.conf;

location / {
return 301 https://www.yourdomain.com$request_uri;
}
}

Then reload Nginx:
sudo systemctl reload nginx

Step 7: Test

Hola ! You Are Good Go..