How to install SSL certificate for Nginx server in Amazon Linux

How to install SSL certificate for Nginx server in Amazon Linux

- cd /home/ec2-user

- chmod a+x ./certbot-auto

- ./certbot-auto certonly --standalone --debug -d yourdomain.com


- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/yourdomain.com/fullchain.pem. Your cert will
expire on yyyy-mm-dd. To obtain a new version of the certificate in
the future, simply run Certbot again.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt:
Donating to EFF:

IMPORTANT NOTES:- Congratulations! Your certificate and chain have been saved at/etc/letsencrypt/live//fullchain.pem. Your cert willexpire on. To obtain a new version of the certificate inthe future, simply run Certbot again.- 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

# Certificate
/etc/letsencrypt/live/yourdomain.com/ /cert.pem

# Full Chain
/etc/letsencrypt/live/yourdomain.com/ /fullchain.pem

# Private Key
/etc/letsencrypt/live/yourdomain.com/ /privkey.pem

...
http {
...

server {
listen 80;
server_name yourdomain.com;


# Automatically route HTTP to HTTPS
return 301
}
}

location /{# Automatically route HTTP to HTTPSreturn 301 https://$server_name$request_uri

server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate "/etc/letsencrypt/live/yourdomain.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/yourdomain.com/privkey.pem";

add_header Strict-Transport-Security "max-age=31536000";
#other headers

location / {
autoindex on;
root /yourdomain.com/build/; #root path of your domain's index file
index index.html;
try_files $uri $uri/ /index.html;
}
}
}

- sudo service nginx restart

Add cron job to renew certificate like:

0 8 28 */3 * /home/ec2-user/certbot-auto renew

10 8 28 */3 * service nginx restart

# Runs at 8AM on 28th of every third month

If renew fails, then stop nginx and do the renew process again