Developer Advocate
This tutorial will show you how to set up a TLS/SSL certificate from Let’s Encrypt on an Ubuntu 16.04 server running Apache as a web server.
SSL certificates are used within web servers to encrypt the traffic between the server and client, providing extra security for users accessing your application. Let’s Encrypt provides an easy way to obtain and install trusted certificates for free.
In order to complete this guide, you will need:
ServerName
.When you are ready to move on, log into your server using your sudo-enabled account.
Let’s Encrypt certificates are fetched via client software running on your server. The official client is called Certbot, and its developers maintain their own Ubuntu software repository with up-to-date versions. Because Certbot is in such active development it’s worth using this repository to install a newer version than Ubuntu provides by default.
First, add the repository:
You’ll need to press ENTER
to accept. Afterwards, update the package list to pick up the new repository’s package information:
And finally, install Certbot from the new repository with apt-get
:
The certbot
Let’s Encrypt client is now ready to use.
Generating the SSL certificate for Apache using Certbot is quite straightforward. The client will automatically obtain and install a new SSL certificate that is valid for the domains provided as parameters.
To execute the interactive installation and obtain a certificate that covers only a single domain, run the certbot
command like so, where example.com is your domain:
If you want to install a single certificate that is valid for multiple domains or subdomains, you can pass them as additional parameters to the command. The first domain name in the list of parameters will be the base domain used by Let’s Encrypt to create the certificate, and for that reason we recommend that you pass the bare top-level domain name as first in the list, followed by any additional subdomains or aliases:
For this example, the base domain will be example.com
.
If you have multiple virtual hosts, you should run certbot
once for each to generate a new certificate for each. You can distribute multiple domains and subdomains across your virtual hosts in any way.
After the dependencies are installed, you will be presented with a step-by-step guide to customize your certificate options. You will be asked to provide an email address for lost key recovery and notices, and you will be able to choose between enabling both http
and https
access or forcing all requests to redirect to https
. It is usually safest to require https
, unless you have a specific need for unencrypted http
traffic.
When the installation is finished, you should be able to find the generated certificate files at /etc/letsencrypt/live
. You can verify the status of your SSL certificate with the following link (don’t forget to replace example.com with your base domain):
https://www.ssllabs.com/ssltest/analyze.html?d=example.com&latest
You should now be able to access your website using a https
prefix.
Let’s Encrypt certificates only last for 90 days. However, the certbot package we installed takes care of this for us by running certbot renew
twice a day via a systemd timer. On non-systemd distributions this functionality is provided by a cron script placed in /etc/cron.d
. The task runs twice daily and will renew any certificate that’s within thirty days of expiration.
To test the renewal process, you can do a dry run with certbot
:
If you see no errors, you’re all set. When necessary, Certbot will renew your certificates and reload Apache to pick up the changes. If the automated renewal process ever fails, Let’s Encrypt will send a message to the email you specified, warning you when your certificate is about to expire.
In this guide, we saw how to install a free SSL certificate from Let’s Encrypt in order to secure a website hosted with Apache. We recommend that you check the official Let’s Encrypt blog for important updates from time to time, and read the Certbot documentation for more details about the Certbot client.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Unfortunately, it doesn’t seem to work like this.
I did it on mine as follows:
And when I test my server by going to ssllabs.com/ssltest/analyze.html?d=example.com, it says
Subject baz.example.com
Common names baz.example.com
Alternative names bar.example.com example.com foo.example.com www.example.com
It seems like it took the last parameter given, and is displaying the alternative names in alphabetical order, although with my sample size of one I can’t say that for certain.
But other than that (and it really doesn’t matter all that much, anyway – all the domains are encrypted just fine), I was really impressed with the whole process. Both with how easy Let’s Encrypt is to use, and with the succinctness of this tutorial. It gets an A on SSL Labs, and I was only able to get to a B using other SSL providers, and that after a hell of a lot of work.
Thanks a heap for providing this.
Please try this link and it should help you: https://www.wordpresstutorialsadvanced.in/setup-ssl-certificates-amazon-lightsail/
I get the following error on a fresh installed ubuntu 16.04 server:
Any suggestions on that? Thanks.
The same here…
Hi there… try this in your server export LC_ALL=“en_US.UTF-8” export LC_CTYPE=“en_US.UTF-8”
at least it work for me this that problem :)
Hi,
After I enter this step:
I get this error:
Note - I was literally testing this out, and used the example.com domain.
Any idea what the issue might be?
Thank you for a great post!
Any chance you are using ServerPilot? I am, and I got the same error you did. I ended up following this tutorial instead, and it worked perfectly:
https://bjoernfranzen.com/how-to-set-up-a-letsencrypt-ssl-certificate-for-your-wordpress-website-on-a-digital-ocean-server-managed-with-a-serverpilot-free-account/
In the Let’s Encrypt tutorial for nginx, there was a section on adding the Diffie-Hellman Group cert as well, resulting in a A+ grading from ssllabs. How would one go about doing the same with the apache server? (This, as well as the tutorials for older versions of Ubuntu skip this section)
I believe you need to enable HSTS response headers to get an A+ rating from SSL Labs.
Please, for the sake of the letsencrypt servers, don’t just copy the crontab config from this article, but rather put in some other arbitrary numbers (another weekday & time)! Otherwise thousands of clients will renew exactly at the same time every Monday at 2:30 am, which will potentially DDoS the servers.
This should be put in the article itself as a heads up!
Came to to make sure someone mentioned this. Please pick a random time and day, it doesn’t matter when, but I suspect the Let’s Encrypt server admins dread 2:30AM on Mondays by this point.
HI, thanks for the tutorial. i’m not sure why the https://www domain got 403 forbidden error You don’t have permission to access / on this server. , the rest is perfect. so i can access https://domain.com , but again the problem 403 shows in https://www.domain.com
thanks
Guys, solved the problem by adding the conf file @ etc/apache2/sites-available/000-default-le-ssl.conf
i added this both ServerName domain.com ServerAlias www.domain.com
cheers
does this work also for ubuntu 32bit 14.04 ?
just a heads up that this generates an additional config file for your virtual host, with -le-ssl.conf appended on the end. I didn’t realise and spent ages editing my virtualhosts config file to allow htaccess override and it wasn’t working. I was editing:
/etc/apache2/sites-enabled/website.com.conf
when i should’ve edited:
/etc/apache2/sites-enabled/website.com-le-ssl.conf
Hopefully that’ll save someone else a headache!
I was wondering how you’d go about adding another domain to the base domain so that a single certificate is valid for multiple domains. I set up a base domain already, but wasn’t sure exactly how I’d go about adding additional domains in the future. If the base domain I already created and added is myfirstdomain.com (I also added www.myfirstdomain.com when setting this up), is the following command correct? Not sure if I need to just basically add all domains all over again.
Thanks!
I am getting an error message. Kindly help Creating virtual environment… Traceback (most recent call last): File “/usr/lib/python3/dist-packages/virtualenv.py”, line 2363, in <module> main() File “/usr/lib/python3/dist-packages/virtualenv.py”, line 719, in main symlink=options.symlink) File “/usr/lib/python3/dist-packages/virtualenv.py”, line 988, in create_environment download=download, File “/usr/lib/python3/dist-packages/virtualenv.py”, line 918, in install_wheel call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT) File “/usr/lib/python3/dist-packages/virtualenv.py”, line 812, in call_subprocess % (cmd_desc, proc.returncode)) OSError: Command /root/.local/share/letsencrypt/bin/python2.7 - setuptools pkg_resources pip wheel failed with error code 1 root@misg:/opt/letsencrypt# cd root@misg:~# cd /etc/letsencrypt/live -bash: cd: /etc/letsencrypt/live: No such file or directory
i am getting this error ```Creating virtual environment… Traceback (most recent call last): File “/usr/lib/python3/dist-packages/virtualenv.py”, line 2363, in <module> main() File “/usr/lib/python3/dist-packages/virtualenv.py”, line 719, in main symlink=options.symlink) File “/usr/lib/python3/dist-packages/virtualenv.py”, line 988, in create_environment download=download, File “/usr/lib/python3/dist-packages/virtualenv.py”, line 918, in install_wheel call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT) File “/usr/lib/python3/dist-packages/virtualenv.py”, line 812, in call_subprocess % (cmd_desc, proc.returncode)) OSError: Command /root/.local/share/letsencrypt/bin/python2.7 - setuptools pkg_resources pip wheel failed with error code 1
I had the same problem and I solved this by doing the following:
~ # apt-get install python-pip ~ # pip install setuptools Traceback (most recent call last): File “/usr/bin/pip”, line 11, in <module> sys.exit(main()) File “/usr/lib/python2.7/dist-packages/pip/init.py”, line 215, in main locale.setlocale(locale.LC_ALL, ‘’) File “/usr/lib/python2.7/locale.py”, line 581, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting
… then …
~ # export LC_ALL=“en_US.UTF-8” -bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory
~ # export LC_CTYPE=“en_US.UTF-8” ~ # pip install setuptools Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/lib/python2.7/dist-packages You are using pip version 8.1.1, however version 9.0.1 is available. You should consider upgrading via the ‘pip install --upgrade pip’ command.
… then …
~ # pip install --upgrade setuptools Collecting setuptools Downloading setuptools-29.0.1-py2.py3-none-any.whl (472kB) 100% |████████████████████████████████| 481kB 3.0MB/s Installing collected packages: setuptools Found existing installation: setuptools 20.7.0 Not uninstalling setuptools at /usr/lib/python2.7/dist-packages, outside environment /usr Successfully installed setuptools-29.0.1 You are using pip version 8.1.1, however version 9.0.1 is available. You should consider upgrading via the ‘pip install --upgrade pip’ command. now it works. So I assume “setuptools” in Ubuntu 16.04 LTS are just too old.
And all that worked for me!
Thanks for the great article! I have a Ubuntu 12.04 server, and these instructions mostly worked for it. I found that I first had to stop the Apache server, then edit /etc/letsencrypt/options-ssl-apache.conf to comment out the “SSLCompression” line therein. Hope this helps someone.
I get the error :
Failed authorization procedure. MYDOMAIN.com (tls-sni-01): urn:acme:error:connection :: The server could not connect to the client to verify the domain :: Failed to connect to MY_IP:443 for TLS-SNI-01 challenge
IMPORTANT NOTES:
The following errors were reported by the server:
Domain: MYDOMAIN.com Type: connection Detail: Failed to connect to MY_IP:443 for TLS-SNI-01 challenge
To fix these errors, please make sure that your domain name was entered correctly and the DNS A record(s) for that domain contain(s) the right IP address. Additionally, please check that your computer has a publicly routable IP address and that no firewalls are preventing the server from communicating with the client. If you’re using the webroot plugin, you should also verify that you are serving files from the webroot path you provided.
I had this problem and resolved it. The connexion was stopped by the UFW firewall :
Make a
$ sudo ufw status
My UFW config was :
So I resolved it by enlarging the HTTP ports allowed :
Now :
WARNING do not do this TUTO after the other one about SSL, to create a Self-Signed SSL certificate: https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-apache-in-ubuntu-16-04
I didi it and it completely broke my Apache config, I had crazy infinite redirect loops, and impossible to understand what was going on.
So I had to reinstall completely my droplet with a snapshot (happy to have done one before SSL tuto).
Whenever I try to run the command “./letsencrypt-auto --apache -d mydomain.com” I get this error.
Firewall issue ? See just above my answer to previous problem on connection.
Nop thats not my problem. I did try the link you suggested and I actually did it with no issues except that I cannot access my owncloud which is why I am trying to do all this. It acts as is loading but then it just stops.
Same here. have you found a solution? ---- update— Found the solution, you just have to add your domain in config.php
I had a working web app but after following this tutorial I get “Index of /” (which has my project files) when I use https. and when I use plain HTTP I get a blank “Index of /”
Same here. Nathan, did you manage to figure it out? Mind sharing?
@nathanjseeley or @jakewashere having the same problem. did you fix it? I’m hoping the original files will pop it in an hour or two, as it’s been only 10 minutes since this setup.
Hello,
It’s a great tutorial. Thanks a lot. I have a problem that, after I use the following command:
./letsencrypt-auto --apache -d example.com -d www.example.com
and choose “forcing all requests to redirect to https”,
when i input the “example.com” in the browser, it redirect to HTTPS form like “https://example.com” and works fine. but when I input the “www.example.com” in the browser, it still using the HTTP form “http://www.example.com”, instead of HTTPS form, and I can access “http://www.example.com” successfully.
Could you please help? Thanks in advance.
Tutorial works perfectly on Debian Jessie 8.5.
Thank you!
I followed this guide a few months back and everything is working correctly. However, I’m unable to update the Let’s Encrypt client using the command sudo git pull. Here is the output from my SSH session when running the command.
Any idea on what I should do to get my client to update without it losing my configuration (if that even happens)?
When I head to my domain name it won’t let me prepend www to my https:// URI. Netflix is https://www.netflix.com, where mine is still https://example.com rather than https://www.example.com.
In my VirtualHost I have the ServerName as www.example.com & ServerAlias as example.com. I’ve added certificates to both www.example.com & example.com. with the delimiter being -d. What could the issue be?(I’m using my actual domain name not example.com …). Thanks for any help in advance!
you need to redirect the domain in your virtual host to your https
couldnt you just do:
<VirtualHost *:443> ServerName example.com </VirtualHost>
?
source: https://wiki.apache.org/httpd/ExampleVhosts
After running into renewal errors I’ve been advised that
sudo apt-get install python-letsencrypt-apache
is outdated and that I should update to certbot. See this nginx-specific postThe short summary of what I did is:
NB: my apache sites were already configured to look in /etc/letsencrypt/live/example/fullchain.pem
But I had to remove the line from my
/etc/apache2/sites-available/default-ssl.conf
Include /etc/letsencrypt/options-ssl-apache.conf
since that not longer exists, and trying to include it prevents apache from starting.
I’m still working on automating the renewal process as that seems to fail if apache is running… which is normally should be if the droplet is serving the site.
How can I use this with CloudFlare? Currently I have to have my site on ‘Pause’ in order for the certificate to show.
How do you add another domain to the certificate after you have allredy set it up? How do you delete cirtificate?
Worked perfectly for me!
Thanks.
First paragraph says “… on an Ubuntu 14.04 server …” I think it’s a typo and should be 16.04
Im trying to create a clean installation with Wordpress and LAMP After the Cert is setup and wordpress is almost ready to install.
I go to my Https website and i get “error to many redirects”
I add to my .htaccess
how Should i fix this?
Excellent! Thank you for taking the time to write this out, very helpful and easy to understand.
As a note, for the contab, Certbot seems to recommend certbot-auto renew --quiet --no-self-upgrade
so I assume the same params would apply to letsencrypt or what not…
Auto renewal is not working. Perhaps something is missing in the tutorial?
Try 30 2 * * 1 export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin && /usr/bin/letsencrypt renew >> /var/log/le-renew.log
Source: https://community.letsencrypt.org/t/letsencrypt-renew-not-working-with-cronjob-manually-it-works/16738/9
https://github.com/certbot/certbot/issues/1833
Yes, auto-renewal is not working with this tutorial. You have to add the export path. I’m wait for another renewal period to see if this is fixed, but it should look like this, please let me know if this worked for anyone else.
30 2 * * 1 export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin && /usr/bin/letsencrypt renew >> /var/log/le-renew.log
Source: https://community.letsencrypt.org/t/letsencrypt-renew-not-working-with-cronjob-manually-it-works/16738/9
https://github.com/certbot/certbot/issues/1833
As a newbie to all things Ubuntu/Wordpress, I really appreciate the above article but hoped to ask the community to clarify a few things. I already have a functioning Wordpress site running on ubuntu 16.04 server with Wordpress installed using the really helpful DO one click install. I also have 5 domains all re-directing to my primary domain using CNAME aliases activated within the DO control panel. It all works well but I hope to make use of LetsEncrypt to enforce secure only access. Having (unsuccessfully) played around with activating LetsEncrypt on one of the subdomains, I hoped someone could help me with the following queries:
Apols for the queries, hoping to get some nudges in the right direction:)
Correct on the redirect on the primary domain, but as shown on point 4 of your question you have to add the www.xxx.com and the xxx.com.
Best practice to use a virtual hosts file for your domain. I think this is used by lets enrypt, if you have more than one domain added on the file, you can add another domain to with letsencrypt and manage multiple hosts / sites, which i’m sure you will do at some point.
3 ) I cant remember the bit about creating new directories but i’m sure your content stays where it should be in the wordpress install on the root you specified.
External scripts can make this difficult to do. Like I said, a plugin might be the quickest way of solving it, but it depends on how many external scripts, where from and how easy/difficult it is to change them to be https.
Set up the lets encrypt certificate and then remedy the scripts that are not secure. There is a tool to help check this. https://www.sslshopper.com/ssl-checker.html
Thanks for the great tutorial again.
Generating certificates this way is great, but it leaves you open to your cron accidentally breaking or your renewal script not working as you expected. To get around this you should check your SSL certificate regularly to ensure that it’s been renewed using a service like TrackSSL that notifies you if your certificate is close to expiration.
A good recommendation. I had renewal fail on a client’s site. Not good. Seems like less of a hassle to set a calendar reminder to do it manually every 2 months, rather than wait for it to fail and have your client call you in 90 days.
Hi, I’m having problems using Crontab for Auto Renewal (NoInstallationError)
Using this cron command
Produces this error:
2017-04-10 02:30:02,656:DEBUG:letsencrypt.cli:Requested authenticator apache and installer apache 2017-04-10 02:30:02,657:DEBUG:letsencrypt.plugins.disco:No installation (PluginEntryPoint#apache): Traceback (most recent call last): File “/usr/lib/python2.7/dist-packages/letsencrypt/plugins/disco.py”, line 103, in prepare self._initialized.prepare() File “/usr/lib/python2.7/dist-packages/letsencrypt_apache/configurator.py”, line 152, in prepare raise errors.NoInstallationError NoInstallationError 2017-04-10 02:30:02,660:DEBUG:letsencrypt.display.ops:No candidate plugin 2017-04-10 02:30:02,660:DEBUG:letsencrypt.display.ops:No candidate plugin 2017-04-10 02:30:02,660:DEBUG:letsencrypt.cli:Selected authenticator None and installer None 2017-04-10 02:30:02,660:INFO:letsencrypt.cli:Could not choose appropriate plugin: The apache plugin is not working; there may be problems with your existing configuration. The error was: NoInstallationError()
Could anyone shed any light on this problem?
This is not working for me. Any suggestions?
is this applicable to the servers with multiple domains? or only single domain? For example im hosting multiple WP on my VPS using multiple domains. each one has its own domain. How can i apply this to all of the websites and domains?
Step 1 is missing the update of the distribution: sudo apt-get dist-upgrade
otherwise will always show there are n packages that can be updated when logging in…
And nearly 3 months after initial success, I am back!:) Or should i say :(
My certs created a few months ago are up for renewal. I’ve tried running the standard
sudo letsencrypt renew
to no avail. I get the following error message:Hoping someone else has seen that and can help advise?
Do I need to run this in ‘interactive mode’ and point the update utility to my ‘000-default.conf’ file (which does contain the multiple domains covered under the initial cert)? Or do I need to update the letsencrypt conf file. On examination, the apache_vhost_root looks to be pointing to the correct folder…
Yes! For the first time, I managed to get this working myself. I’ll share the outcome with the community in case it helps someone else.
My issue was, I was assuming my multi-domain setup was built around the following file which contained all my aliases correctly
000-default.conf
. However, I added the multiple aliases todefault-ssl.conf
and renew worked a treat.Incidentally, while researching this, I found the following command which helped me test my changes before committing (and therefore, build confidence before hitting the ‘scary red button)’!. The following allows for a dry run:
sudo letsencrypt renew --agree-tos --dry-run
Hope this helps a newbie like me in the future…
Beautiful tutorial. Thanks! :)
Where does the crontab file belong?
When I typed “sudo crontab -e”, I got this:
I chose nano, but the file was saved at this location: /tmp/crontab.1BwnQ0/crontab
My understanding is that will disappear upon reboot, correct? So where does the crontab file belong? Thanks in advance!
I found that this one works best for me. https://www.wordpresstutorialsadvanced.in/setup-ssl-certificates-amazon-lightsail/ It also has a video tutorials
Hello
Why do you explicitly define the path as “/usr/bin/certbot” with crontab?
Here it uses “certboot renew” only… https://certbot.eff.org/#ubuntuxenial-apache
Thank you for the great document! c3y
I have a problem in the auto renew step, after adding the following code:
I got this error message :
hi
i was using this tutorial and got the following error: Failed authorization procedure. topladylingerie.co.ke (tls-sni-01): urn:acme:err or:connection :: The server could not connect to the client to verify the domain :: Connection refused, www.topladylingerie.co.ke (tls-sni-01): urn:acme:error:c onnection :: The server could not connect to the client to verify the domain :: Connection refused
IMPORTANT NOTES:
The following errors were reported by the server:
Domain: topladylingerie.co.ke Type: connection Detail: Connection refused
Domain: www.topladylingerie.co.ke Type: connection Detail: Connection refused
To fix these errors, please make sure that your domain name was entered correctly and the DNS A record(s) for that domain contain(s) the right IP address. Additionally, please check that your computer has a publicly routable IP address and that no firewalls are preventing the server from communicating with the client. If you’re using the webroot plugin, you should also verify that you are serving files from the webroot path you provided. ==============
And here is the ufw status: Status: active
To Action From
Apache Full ALLOW Anywhere OpenSSH ALLOW Anywhere 80 ALLOW Anywhere 443 ALLOW Anywhere Apache Full (v6) ALLOW Anywhere (v6) OpenSSH (v6) ALLOW Anywhere (v6) 80 (v6) ALLOW Anywhere (v6) 443 (v6) ALLOW Anywhere (v6) ==============
I don’t think so that this has anything to do with DNS A record… I am doing this first time and had googled it but no result. and event tried the cases on this page. still error…
Can anyone help?
Hello everyone
i’m just a newbie and i really don’t understant what you are saying :) i just open my blog with one click wordpress thing. i read your tutorial and i skip the Prerequisites part of your tutorial and now i have ssl certificate i dont have any problem for now but i wonder could it be problem in the future? i really waiting your answer Sorry for bad english:(
This comment has been deleted
I chosed not to force the redirect ( to see if it was really working ), and it is working great. Thanks for the tuto!
The thing is now I really Do want to force the redirect.
Shall I run the config command again to force de redirect or is any other command to do so? ( maybe editing some file ? )
Thanks in advance :)
I followed the tutorial with no problems, yet it still shows says people are connecting to an unsecure site. How would I go about fixing this?
Thanks a lot, Erika!
Hi, i’m sorry but the first command :
sudo add-apt-repository ppa:certbot/certbot
didn’t work :
sudo: add-apt-repository: command not found