How to setup Let's Encrypt for Nginx on Ubuntu 18.04 (including IPv6, HTTP/2 and A+ SLL rating)
https://gist.github.com/cecilemuller/a26737699a7e70a7093d4dc115915de8
How to setup Let's Encrypt for Nginx on Ubuntu 18.04 (including IPv6, HTTP/2 and A+ SLL rating)
Virtual hosts
Let's say you want to host domains first.com
and second.com
.
Create folders for their files:
Create a text file /etc/nginx/sites-available/first.conf
containing:
Create a text file /etc/nginx/sites-available/second.conf
containing:
Note that only the first domain has the keywords default_server
and ipv6only=on
in the listen
lines.
Replace the default virtual host:
Check that Nginx is running:
Expected results at this stage:
http://first.com
andhttp://www.first.com
serve the files from/var/www/first
http://second.com
andhttp://www.second.com
serve the files from/var/www/second
https://www.first.com
andhttps://www.second.com
don't work yet
Certbot
Install Certbot for Nginx:
Setup the certificates & convert Virtual Hosts to HTTPS:
It will ask for:
an email address
agreeing to its Terms of Service
which domains to use HTTPS for (it detects the list using
server_name
lines in your Nginx config)whether to redirect HTTP to HTTPS (recommended) or not
You could stop here if all you want is HTTPS as this already gives you an A
rating and maintains itself.
Test your site with SSL Labs using https://www.ssllabs.com/ssltest/analyze.html?d=www.YOUR-DOMAIN.com
Expected results at this stage:
http://first.com
redirects tohttps://first.com
http://second.com
redirects tohttps://second.com
http://www.first.com
redirects tohttps://www.first.com
http://www.second.com
redirects tohttps://www.second.com
https://first.com
andhttps://www.first.com
serve the files from/var/www/first
https://second.com
andhttps://www.first.com
serve the files from/var/www/second
Automatic renewal
There is nothing to do, Certbot installed a cron task to automatically renew certificates about to expire.
You can check renewal works using:
You can also check what certificates exist using:
HTTP/2
first.conf
should now look something like this, now that Certbot edited it:
Certbot didn't add HTTP/2 support when it created the new server blocks, so replace these lines:
by this:
There is already an open Github issue requesting Certbot to add http2
automatically, so hopefully this step can soon be removed.
Stronger settings for A+
Trusted certificate
The HTTPS server
blocks in first.conf
and second.conf
contain these lines, added by Certbot:
The stronger settings use OCSP Stapling, so each virtual host will need a ssl_trusted_certificate
as well.
Add this line (using the folder name that Certbot generated for your domain) after the ssl_certificate_key
line:
SSL
Now let's edit the shared SSL settings at /etc/letsencrypt/options-ssl-nginx.conf
. It most likely looks like this initially:
If you tested with SSL Labs, you probably noticed that quite a few ciphers were flagged as "weak".
So replace the contents of the file with:
Conclusion
You could further improve using content-specific features like Content Security Policy
and Subresource Integrity
, and Brotli compression to replace gzip.
Online testing tools:
Useful links:
DavidODonovan commented on Jan 19, 2017
gerchen commented on Jan 21, 2017
kakopappa commented on Jan 24, 2017 • edited
andresmgsl commented on Jan 29, 2017
MCeddy commented on Mar 3, 2017
boriscy commented on Mar 21, 2017
texeltexel2009 commented on Mar 21, 2017
rlam3 commented on Mar 24, 2017 • edited
cecilemuller commented on Jun 2, 2017
hshhhhh commented on Jun 8, 2017 • edited
MastroMicio commented on Jun 10, 2017
blbwd commented on Jun 17, 2017
cecilemuller commented on Jun 19, 2017 • edited
blbwd commented on Jun 20, 2017
cecilemuller commented on Jun 20, 2017 • edited
blbwd commented on Jun 20, 2017
cecilemuller commented on Jun 20, 2017
RobinCsl commented on Jun 28, 2017 • edited
dinofizz commented on Jul 5, 2017
josethomazini commented on Jul 8, 2017
rajeevkannav commented on Jul 25, 2017 • edited
restmount commented on Aug 1, 2017
astr0naugh7 commented on Aug 6, 2017 • edited
diegoddox commented on Aug 14, 2017
crw commented on Aug 17, 2017
crw commented on Aug 17, 2017
pankaj884 commented on Aug 21, 2017
jwilleke commented on Aug 28, 2017
frenchbread commented on Aug 29, 2017 • edited
nadj commented on Sep 8, 2017 • edited
crspybits commented on Sep 11, 2017
yifeikong commented on Oct 8, 2017 • edited
blbwd commented on Oct 9, 2017
cecilemuller commented on Oct 9, 2017 • edited
cecilemuller commented on Oct 9, 2017
blbwd commented on Oct 9, 2017
cecilemuller commented on Oct 9, 2017 • edited
nemchik commented on Oct 31, 2017
cecilemuller commented on Nov 2, 2017 • edited
mobambi commented on Nov 14, 2017
eldoy commented on Jan 5, 2018
dinvisible1 commented on Jan 16, 2018
salfredogonzalez commented on Jan 17, 2018
tinusg commented on Jan 17, 2018 • edited
dheerajbhaskar commented on Jan 19, 2018
besnik commented on Jan 24, 2018
yuryroot commented on Jan 25, 2018
kiritAyya commented on Jan 25, 2018
giantas commented on Jan 25, 2018 • edited
spn89 commented on Feb 4, 2018
vwal commented on Feb 9, 2018 • edited
jerome-diver commented on Feb 20, 2018
jerome-diver commented on Feb 20, 2018
sebthemonster commented on Feb 23, 2018
devansvd commented on Mar 4, 2018 • edited
OsoianMarcel commented on Mar 12, 2018
someburner commented on Mar 22, 2018
rob-gonz commented on Mar 31, 2018
slavensaka commented on Apr 3, 2018
roppa commented on Apr 3, 2018
namaggarwal commented on Apr 22, 2018
kv9991 commented on Apr 25, 2018
OsoianMarcel commented on May 10, 2018
junjielee commented on May 21, 2018
ghost commented on May 22, 2018
LarryWachira commented on May 30, 2018
umarali commented on May 30, 2018
maikdiepenbroek commented on Jun 27, 2018
bennetcq commented on Jun 29, 2018 • edited
cecilemuller commented on Jun 29, 2018 • edited
alexanderpetrenz commented on Jul 3, 2018 • edited
MagePsycho commented on Jul 15, 2018
MagePsycho commented on Jul 16, 2018
JamesSwift commented on Sep 13, 2018
markomilivojevic commented on Nov 18, 2018
shipsource commented on Dec 25, 2018 • edited
WeeHong commented on Feb 4, 2019
robsch commented on Feb 13, 2019 • edited
adamblackxo commented on Feb 23, 2019
natharas commented on Mar 2, 2019
martondob commented on Apr 8, 2019
cecilemuller commented on May 10, 2019
billpliske commented on Jun 30, 2019
carlostiberiojr commented on Aug 3, 2019
rajeshkumaravel commented on Aug 20, 2019
AlbertSuarez commented on Sep 2, 2019
wallysoncarvalho commented on Feb 1, 2020
peterneubart commented on Apr 24, 2020
emjayess commented on Apr 25, 2020
tpaullee commented on May 20, 2020
hitjethva commented on Aug 15, 2020
Last updated