Tutorial

How To Secure Apache with Let's Encrypt on Ubuntu 16.04

Published on April 21, 2016

Developer Advocate

How To Secure Apache with Let's Encrypt on Ubuntu 16.04
Not using Ubuntu 16.04?Choose a different version or distribution.
Ubuntu 16.04

Introduction

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.

Prerequisites

In order to complete this guide, you will need:

  • An Ubuntu 16.04 server with a non-root sudo-enabled user, which you can set up by following our Initial Server Setup guide
  • The Apache web server installed with one or more domain names properly configured through Virtual Hosts that specify ServerName.

When you are ready to move on, log into your server using your sudo-enabled account.

Step 1 — Install the Let’s Encrypt Client

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:

  1. sudo add-apt-repository ppa:certbot/certbot

You’ll need to press ENTER to accept. Afterwards, update the package list to pick up the new repository’s package information:

  1. sudo apt-get update

And finally, install Certbot from the new repository with apt-get:

  1. sudo apt-get install python-certbot-apache

The certbot Let’s Encrypt client is now ready to use.

Step 2 — Set Up the SSL Certificate

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:

  1. sudo certbot --apache -d example.com

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:

  1. sudo certbot --apache -d example.com -d www.example.com

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.

Step 3 — Verifying Certbot Auto-Renewal

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:

  1. sudo certbot renew --dry-run

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.

Conclusion

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.

Learn more about our products

About the author(s)

Erika Heidi
Erika HeidiDeveloper Advocate
See author profile

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
30 Comments
Leave a comment...

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!

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

Unfortunately, it doesn’t seem to work like this.

I did it on mine as follows:

./letsencrypt-auto --apache -d example.com -d www.example.com -d foo.example.com -d bar.example.com -d baz.example.com

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.

I get the following error on a fresh installed ubuntu 16.04 server:

Checking for new version...
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 /home/christian/.loc...ncrypt/bin/python2.7 - setuptools pkg_resources pip wheel failed with error code 1

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:

./letsencrypt-auto --apache -d example.com -d www.example.com

I get this error:

Requesting root privileges to run letsencrypt...
   /root/.local/share/letsencrypt/bin/letsencrypt --apache -d example.com -d www.example.com
The apache plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError()

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.

./letsencrypt-auto --apache -d myfirstdomain.com -d www.myfirstdomain.com -d seconddomain.com -d www.seconddomain.com

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 :

To                         Action      From
--                         ------      ----
80/tcp                     ALLOW       Anywhere
123/tcp                    ALLOW       Anywhere
80/tcp (v6)                ALLOW       Anywhere (v6)
123/tcp (v6)               ALLOW       Anywhere (v6)

So I resolved it by enlarging the HTTP ports allowed :

$ sudo ufw delete allow '80/tcp'
$ sudo ufw allow 'Apache Full'

Now :

To                         Action      From
--                         ------      ----
123/tcp                    ALLOW       Anywhere
Apache Full                ALLOW       Anywhere
123/tcp (v6)               ALLOW       Anywhere (v6)
Apache Full (v6)           ALLOW       Anywhere (v6)

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.

The following errors were reported by the server:

   Domain: joelarce.com
   Type:   connection
   Detail: Failed to connect to 73.164.1.67: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.```

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.

johners@JohnersBlogIPV6:/opt/letsencrypt$ sudo git pull
remote: Counting objects: 597, done.
remote: Compressing objects: 100% (98/98), done.
remote: Total 597 (delta 423), reused 358 (delta 358), pack-reused 141
Receiving objects: 100% (597/597), 218.72 KiB | 0 bytes/s, done.
Resolving deltas: 100% (453/453), completed with 126 local objects.
From https://github.com/letsencrypt/letsencrypt
   d05f53b..f544b03  master     -> origin/master
 * [new branch]      2558-test-fix -> origin/2558-test-fix
 * [new branch]      all-together-now -> origin/all-together-now
 * [new branch]      dedupe     -> origin/dedupe
 * [new branch]      doc-plugins-anchor -> origin/doc-plugins-anchor
   65605f7..44cf404  documentation_cleanup -> origin/documentation_cleanup
 * [new branch]      issue_2558 -> origin/issue_2558
 * [new branch]      issue_3282 -> origin/issue_3282
   e4f963d..5bf9b3a  multiple_vhosts -> origin/multiple_vhosts
 * [new branch]      nginx-space-preservation2 -> origin/nginx-space-preservation2
 * [new branch]      revert-3268-dialog-autosize -> origin/revert-3268-dialog-autosize
Updating 45e47f6..f544b03
error: Your local changes to the following files would be overwritten by merge:
        letsencrypt-auto
Please, commit your changes or stash them before you can merge.
Aborting

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

<VirtualHost *:80>
    ServerName example.com
    Redirect / https://example.com/
</VirtualHost>

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 post

The short summary of what I did is:

sudo apt-get purge --auto-remove letsencrypt

sudo apt update
sudo apt install git

sudo git clone https://github.com/certbot/certbot /opt/certbot
sudo ln -s /opt/certbot/certbot-auto /usr/bin/certbot

sudo certbot certonly -d example.com -d www.example.com

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

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

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

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.