<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oyewumi Boluwatife Emmanuel</title>
    <description>The latest articles on DEV Community by Oyewumi Boluwatife Emmanuel (@bolexzy).</description>
    <link>https://dev.to/bolexzy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1061562%2F6aa42990-3aa0-43b4-a97e-17d811834a2b.jpeg</url>
      <title>DEV Community: Oyewumi Boluwatife Emmanuel</title>
      <link>https://dev.to/bolexzy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bolexzy"/>
    <language>en</language>
    <item>
      <title>Setting up SSL Certificates for HAProxy with Let’s Encrypt</title>
      <dc:creator>Oyewumi Boluwatife Emmanuel</dc:creator>
      <pubDate>Sat, 08 Apr 2023 21:54:06 +0000</pubDate>
      <link>https://dev.to/bolexzy/setting-up-ssl-certificates-for-haproxy-with-lets-encrypt-46gl</link>
      <guid>https://dev.to/bolexzy/setting-up-ssl-certificates-for-haproxy-with-lets-encrypt-46gl</guid>
      <description>&lt;p&gt;In this tutorial, I’ll be sharing how I configured my HolbertonBnB web servers at ALX with &lt;a href="https://letsencrypt.org/" rel="noopener noreferrer"&gt;Let’s Encrypt&lt;/a&gt; and &lt;a href="https://www.haproxy.com/" rel="noopener noreferrer"&gt;HAproxy&lt;/a&gt; SSL termination.&lt;/p&gt;

&lt;p&gt;Let’s Encrypt is a new Certificate Authority (CA) that offers an accessible way to acquire and install free TLS/SSL certificates for web servers, allowing secure communication through encrypted HTTPS. One of the tools offered by Let’s Encrypt is Certbot, a software client that streamlines the certificate acquisition and installation process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before following this tutorial, you’ll need to know/have a few things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.cloudflare.com/learning/ssl/what-is-ssl/" rel="noopener noreferrer"&gt;What is SSL?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/HTTPS" rel="noopener noreferrer"&gt;What is HTTPS?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.digitalocean.com/glossary/ssl-termination/" rel="noopener noreferrer"&gt;What is SSL Termination?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Haproxy: This can be installed through various methods, but for this tutorial, we will be using the simple simple &lt;code&gt;apt-get install haproxy&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Secure Sockets Layer, SSL, is a protocol for establishing encrypted and authenticated links between networked computers in order to keep internet connections secure and to safeguard sensitive data.&lt;br&gt;
SSL termination reduces the load on your servers while speeding up and simplifying data exchanges. SSL termination allows your application to handle more connections at a time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also, you must own or control the registered domain name that you wish to use the certificate with.&lt;/p&gt;

&lt;p&gt;Let’s move on to installing Certbot, the Let’s Encrypt client software, once you have completed all the prerequisites.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Install Certbot
&lt;/h2&gt;

&lt;p&gt;The first step to obtaining an SSL/TLS certificate is to install Certbot software on your server. Let’s Encrypt’s client is now called &lt;a href="https://github.com/certbot/certbot" rel="noopener noreferrer"&gt;Certbot&lt;/a&gt; which is used to generate the certificates. To get the latest code you either clone the repository Certbot or use apt-get:&lt;/p&gt;

&lt;p&gt;Using apt-get install, first, update the local package index:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ sudo apt update
$ sudo apt install -y certbot python3-certbot-nginxbash


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 2 — Obtaining a Certificate
&lt;/h2&gt;

&lt;p&gt;Diving in, the first thing you will require is a certificate. Let’s Encrypt offers multiple plugins to obtain SSL certificates, most of the plugins will only help you with obtaining a certificate that you must manually configure your web server to use.&lt;/p&gt;

&lt;p&gt;These plugins are called “authenticators” because they authenticate whether a server should be issued a certificate, without installing it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Generating the certificate:
&lt;/h3&gt;

&lt;p&gt;The Standalone plugin is a straightforward method for acquiring SSL certificates. It operates by launching a small web server (default on port 80) on your server, which Let’s Encrypt CA uses to verify your server’s identity and issue the certificate. However, to use this method, port 80 must be available.&lt;/p&gt;

&lt;p&gt;Make sure that there is nothing listening on port 80. To list usage:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ netstat -na | grep ':80.*LISTEN'
# Kill everything that might be on this port
$ sudo service haproxy stop
$ sudo certbot certonly --standalone -d www.example.com --non-interactive --agree-tos --email example@gmail.com


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwsb9e74wjlghuxvptcnn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwsb9e74wjlghuxvptcnn.png" alt="If everything is successful, certbot will print an output message like this ⬆️ You will want to note the path and expiration date of your certificate, which was highlighted in the example output above."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After obtaining the cert, you will have the following PEM-encoded files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cert.pem: Your domain’s certificate&lt;/li&gt;
&lt;li&gt;chain.pem: Let’s Encrypt chain certificate&lt;/li&gt;
&lt;li&gt;fullchain.pem: a combination of cert.pem and chain.pem&lt;/li&gt;
&lt;li&gt;privkey.pem: the private key to your certificate.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The files themselves are placed in a subdirectory in /etc/letsencrypt/archive. However, Certbot creates symbolic links to the most recent certificate files in the /etc/letsencrypt/live/your_domain_name directory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 3: Configure HAProxy to Accept Encrypted Traffic
&lt;/h2&gt;

&lt;p&gt;To configure HAProxy to accept encrypted traffic for your subdomain, follow these steps:&lt;/p&gt;

&lt;p&gt;When setting up SSL termination with HAProxy, you need to combine fullchain.pem and privkey.pem into one file.&lt;/p&gt;

&lt;p&gt;first, create the directory where the combined file will be placed, &lt;code&gt;/etc/haproxy/certs&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$ sudo mkdir -p /etc/haproxy/certs


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# Next, create the combined file with this cat command (substitute the highlighted example.com with your domain name):

$ DOMAIN='example.com' sudo -E bash -c 'cat /etc/letsencrypt/live/$DOMAIN/fullchain.pem /etc/letsencrypt/live/$DOMAIN/privkey.pem &amp;gt; /etc/haproxy/certs/$DOMAIN.pem'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# Secure access to the combined file, which contains the private key, with this command:

$ sudo chmod -R go-rwx /etc/haproxy/certs


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It will create a combined cert under &lt;code&gt;/etc/haproxy/certs/example.com.pem&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Haproxy configuration
&lt;/h3&gt;

&lt;p&gt;If haproxy happens to be running, stop it with service haproxy stop.&lt;/p&gt;

&lt;p&gt;First, save the default configuration file: &lt;code&gt;cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.old&lt;/code&gt;. &lt;br&gt;
Now, overwrite the old one with this new one (comments about what each setting does, are in-lined; they are safe to copy):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

### Frontend Sections
frontend www-http
   bind *:80
   # Adds http header to end of end of the HTTP request
   reqadd X-Forwarded-Proto:\ http
   # Sets the default backend to use which is defined below with name 'www-backend'
   default_backend www-backend


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# Add a frontend to handle incoming HTTPS connections
frontend www-https
    # Bind 443 with the generated letsencrypt cert.
    bind *:443 ssl crt /etc/haproxy/certs/domain.pem
    # set x-forward to https
    reqadd X-Forwarded-Proto:\ https
    # Select a Challenge
    acl letsencrypt-acl path_beg /.well-known/acme-challenge/
    # Use the challenge backend if the challenge is set
    use_backend letsencrypt-backend if letsencrypt-acl
    default_backend www-backend


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Backend Sections
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

backend www-backend
   # ssl_fc: Returns true when the front connection was made via an SSL/TLS transport
   redirect scheme https code 301 if !{ ssl_fc }
   server www-1 www_1_private_IP:80 check
   server www-2 www_2_private_IP:80 check

backend letsencrypt-backend
   # Lets encrypt backend server
   server letsencrypt 127.0.0.1:54321


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Save this, and start haproxy with &lt;code&gt;sudo service haproxy restart&lt;/code&gt;. If you did everything right, it should say nothing. Be sure to validate the config with &lt;code&gt;haproxy -c -f /etc/haproxy/haproxy.cfg&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, ensure its running:&lt;br&gt;
&lt;code&gt;$ sudo service haproxy status&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once your 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.&lt;br&gt;
And that is all. HAProxy is now using a free Let’s Encrypt TLS/SSL certificate to securely serve HTTPS traffic.&lt;br&gt;
If you have any questions or encounter any issues during the setup process, please leave a comment below. Thank you for reading!&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources:
&lt;/h3&gt;

&lt;p&gt;Helpful blog posts that inspired this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://skarlso.github.io/2017/02/15/how-to-https-with-hugo-letsencrypt-haproxy/" rel="noopener noreferrer"&gt;This post&lt;/a&gt; by Skarlso&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.digitalocean.com/community/tutorials/how-to-secure-haproxy-with-let-s-encrypt-on-ubuntu-14-04" rel="noopener noreferrer"&gt;This tutorial&lt;/a&gt; by digital ocean&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://github.com/Bolexzy" rel="noopener noreferrer"&gt;Boluwatife Oyewumi&lt;/a&gt;
&lt;/h2&gt;

</description>
      <category>security</category>
      <category>loadbalancing</category>
      <category>haproxy</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
