Today, I would like to write about how to do HTTPS for a website, without the need to buy a certificate and set it up via your DNS provider. Let’s begin.
What you will achieve by the end of this post:
- Every call to HTTP will be redirected to HTTPS via haproxy.
- HTTPS will be served with Haproxy and LetsEncrypt as the Certificate provider.
- Automatically update the certificate before its expiration.
- No need for IPTable rules to route 8080 to 80.
- Traffic to and from your page will be encrypted.
- This all will cost you nothing.
I will use a static website generator for this called Hugo which, if you know me, is my favorite generator tool. These instructions are for haproxy and hugo, if you wish to use apache and nginx for example, you’ll have to dig for the corresponding settings for letsencrypt and certbot.
What You Will Need
Haproxy can be found here: Haproxy. There are a number of options to install haproxy. I chose a simple
apt-get install haproxy.
Information about Let’s Encrypt can be found on their website here: Let’s Encrypt. Let’s Encrypt’s client is now called Certbot which is used to generate the certificates. To get the latest code you either clone the repository Certbot, or use an auto downloader:
Either way, I’m using the current latest version: v0.11.1.
This goes without saying, but that these operations will require you to have sudo privileges. I suggest staying in sudo for ease of use.
This means that the commands, I’ll write here, will assume you are in
sudo su mode thus no
sudo prefix will be used.
In order for your website to work under https this guide assumes that you have port 80 and 443 open on your router / network security group.
Single Server Environment
It is possible for haproxy, certbot and your website to run on designated servers. Haproxy’s abilities allows to define multiple server sources. In this guide, my haproxy, website and certbot will all run on the same server; thus redirecting to 127.0.0.1 and local ips. This is more convenient, because otherwise the haproxy IP would have to be a permanent local/remote ip. Or an automated script would have to be setup which is notified upon IP change and updates the ip records.
Creating a Certificate
Diving in, the first thing you will require is a certificate. A certificate will allow for encrypted traffic and an authenticated website. Let’s Encrypt which is basically functioning as an independent, free, automated CA (Certificate Authority). Usually, the process would be to pay a CA to give you a signed, generated certificate for your website, and you would have to set that up with your DNS provider. Let’s Encrypt has that all automated, and free of any charge. Neat.
So let’s get started. Clone the repository into
/opt/letsencrypt for further usage.
Generating the certificate
Make sure that there is nothing listening on ports: 80, 443. To list usage:
Kill everything that might be on these ports, like apache2 and httpd. These will be used by haproxy and certbot for challenges and redirecting traffic.
You will be creating a standalone certificate. This is the reason we need port 80 and 443 open.
Run certbot by defining the
--standalone flags. For domain validation you are going to use port 443, tls-sni-01 challenge.
The whole command looks like this:
If this displays something like, “couldn’t connect” you probably still have something running on a port it tries to use. The
generated certificate will be located under
a symlink to the latest version of the cert. It’s wise to not copy these away from here, since the live link is always updated to the latest version.
Our script will handle haproxy, which requires one cert file made from privkey + fullchain|.pem files.
Let’s Encrypt issues short lived certificates (90 days). In order to not have to do this procedure every 89 days, certbot provides a nifty
renew. However, for the cert to be generated, the port 443 has to be open. This means, haproxy needs to be stopped before
doing the renew. Now, you COULD write a script which stops it, and after the certificate has been renewed, starts it again, but certbot has
you covered again in that department. It provides hooks called
post-hook. Thus, all you have to write is the following:
If you would like to test it first, just include the switch
In case of success you should see something like this:
Put this script into a crontab to run every 89 days like this:
And you should be all set. Now we move on the configure haproxy to redirect and to use our newly generated certificate.
Like I said, haproxy requires a single file certificate in order to encrypt traffic to and from the website. To do this, we need to combine
fullchain.pem. As of this writing, there are a couple of solutions to automate this via a post hook on renewal. And also,
there is an open ticket with certbot to implement a simpler solution located here: https://github.com/certbot/certbot/issues/1201. I, for now,
have chosen to simply concatenate the two files together with
cat like this:
It will create a combined cert under
If haproxy happens to be running, stop it with
service haproxy stop.
First, save the default configuration file:
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.old. Now, overwrite the old one with this
new one (comments about what each setting does, are in-lined; they are safe to copy):
Save this, and start haproxy with
services haproxy start. If you did everything right, it should say nothing.
If, however, there went something wrong with starting the proxy, it usually displays something like this:
You can also gather some more information on what went wrong from
Starting the Server
Everything should be ready to go. Hugo has the concept of a baseUrl. Everything that it loads, and tries to access
will be prefixed with it. You can either set it through it’s
config.yaml file, or from the command line.
To start the server, call this from the site’s root folder:
Interesting thing here to note is
https and the port. The IP could be 127.0.0.1 as well. I experienced problems though
with not binding to network IP when I was debugging the site from a different laptop on the same network.
Once the server is started, you should be able to open up your website from a different browser, not on your local network, and see that it has a valid certificate installed. In Chrome you should see a green icon telling you that the cert is valid.
And that is all. The site should be up and running and the proxy should auto-renew your site’s certificate. If you happened to change DNS or change the server, you’ll have to reissue the certificate.
Thanks for reading! Any questions or trouble setting something up, please feel free to leave a comment.