{"id":192,"date":"2023-05-26T12:26:17","date_gmt":"2023-05-26T12:26:17","guid":{"rendered":"https:\/\/koner.mywire.org\/?page_id=192"},"modified":"2024-02-11T18:19:18","modified_gmt":"2024-02-11T18:19:18","slug":"how-to-secure-apache-with-lets-encrypt","status":"publish","type":"page","link":"https:\/\/koner.mywire.org\/index.php\/how-to-secure-apache-with-lets-encrypt\/","title":{"rendered":"How To Secure Apache with Let&#8217;s Encrypt"},"content":{"rendered":"\n<p>Let\u2019s Encrypt is a Certificate Authority (CA) that facilitates obtaining and installing free&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs\">TLS\/SSL certificates<\/a>, thereby enabling encrypted HTTPS on web servers. It streamlines the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx.<\/p>\n\n\n\n<p>In this guide, you\u2019ll use&nbsp;<a href=\"https:\/\/certbot.eff.org\/\">Certbot<\/a>&nbsp;to obtain a free SSL certificate for Apache on Raspberry Pi and make sure this certificate is set up to renew automatically.<\/p>\n\n\n\n<p>This tutorial uses a separate virtual host file instead of Apache\u2019s default configuration file for setting up the website that will be secured by Let\u2019s Encrypt.&nbsp;We recommend&nbsp;creating new Apache virtual host files for each domain hosted in a server because it helps to avoid common mistakes and maintains the default configuration files as a fallback setup.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-1-installing-certbot\">Step 1 \u2014 Installing Certbot<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-22-04#step-1-installing-certbot\"><\/a><\/h4>\n\n\n\n<p>To obtain an SSL certificate with Let\u2019s Encrypt, you need to install the Certbot software on your server. You\u2019ll use the default Ubuntu package repositories for that.<\/p>\n\n\n\n<p>First, update the local package index:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo apt update<\/strong><\/code><\/pre>\n\n\n\n<p>You need two packages:&nbsp;<code>certbot<\/code>, and&nbsp;<code>python3-certbot-apache<\/code>. The latter is a plugin that integrates Certbot with Apache, making it possible to automate obtaining a certificate and configuring HTTPS within your web server with a single command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo apt install certbot python3-certbot-apache<\/strong><\/code><\/pre>\n\n\n\n<p>You will be prompted to confirm the installation by pressing&nbsp;<code>Y<\/code>, then&nbsp;<code>ENTER<\/code>.<\/p>\n\n\n\n<p>Certbot is now installed on your server. In the next step, you\u2019ll verify Apache\u2019s configuration to make sure your virtual host is set appropriately. This will ensure that the&nbsp;<code>certbot<\/code>&nbsp;client script will be able to detect your domains and reconfigure your web server to use your newly generated SSL certificate automatically.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-2-checking-your-apache-virtual-host-configuration\">Step 2 \u2014 Checking your Apache Virtual Host Configuration<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-22-04#step-2-checking-your-apache-virtual-host-configuration\"><\/a><\/h4>\n\n\n\n<p>To automatically obtain and configure SSL for your web server, Certbot needs to find the correct virtual host within your Apache configuration files. Your server domain name(s) will be retrieved from the&nbsp;<code>ServerName<\/code>&nbsp;and&nbsp;<code>ServerAlias<\/code>&nbsp;directives defined within your&nbsp;<code>VirtualHost<\/code>&nbsp;configuration block.<\/p>\n\n\n\n<p>If you followed the&nbsp;virtual host setup step in <a href=\"https:\/\/koner.mywire.org\/index.php\/how-to-setup-a-raspberry-pi-apache-web-server\/\" data-type=\"URL\" data-id=\"https:\/\/koner.mywire.org\/index.php\/how-to-setup-a-raspberry-pi-apache-web-server\/\">How to Setup a Raspberry Pi Apache Web Server<\/a>, you should have a VirtualHost block set up for your domain at&nbsp;<code>\/etc\/apache2\/sites-available\/<mark>your_domain<\/mark>.conf<\/code>&nbsp;with the&nbsp;<code>ServerName<\/code>&nbsp;and also the&nbsp;<code>ServerAlias<\/code>&nbsp;directives already set appropriately.<\/p>\n\n\n\n<p>To confirm this is set up, open the virtual host file for your domain using&nbsp;<code>nano<\/code>&nbsp;or your preferred text editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo nano \/etc\/apache2\/sites-available\/<mark>your_domain<\/mark>.conf<\/strong><\/code><\/pre>\n\n\n\n<p>Find the existing&nbsp;<code>ServerName<\/code>&nbsp;and&nbsp;<code>ServerAlias<\/code>&nbsp;lines. They should be listed as follows:<\/p>\n\n\n\n<p>\/etc\/apache2\/sites-available\/your_domain.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\nServerName <mark>your_domain<\/mark>\nServerAlias <mark>www.your_domain<\/mark>\n...\n<\/code><\/pre>\n\n\n\n<p>If you already have your&nbsp;<code>ServerName<\/code>&nbsp;and&nbsp;<code>ServerAlias<\/code>&nbsp;set up like this, you can exit your text editor and move on to the next step. If your current virtual host configuration doesn\u2019t match the example, update it accordingly. If you\u2019re using&nbsp;<code>nano<\/code>, you can exit by pressing&nbsp;<code>CTRL+X<\/code>, then&nbsp;<code>Y<\/code>&nbsp;and&nbsp;<code>ENTER<\/code>&nbsp;to confirm your changes, if any. Then, run the following command to validate your changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo apache2ctl configtest<\/strong><\/code><\/pre>\n\n\n\n<p>You should receive&nbsp;<code>Syntax OK<\/code>&nbsp;as a response. If you get an error, reopen the virtual host file and check for any typos or missing characters. Once your configuration file\u2019s syntax is correct, reload Apache so that the changes take effect:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo systemctl reload apache2<\/strong><\/code><\/pre>\n\n\n\n<p>With these changes, Certbot will be able to find the correct VirtualHost block and update it.<\/p>\n\n\n\n<p>Next, you\u2019ll update the firewall to allow HTTPS traffic.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-3-allowing-https-through-the-firewall\">Step 3 \u2014 Allowing HTTPS Through the Firewall<\/h4>\n\n\n\n<p>To allow the <strong>Apache Full<\/strong> profile through the firewall on a Raspberry Pi web server using <code><strong>ufw<\/strong><\/code>, you can follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Enable <code>ufw<\/code>: If <code>ufw<\/code> is not already enabled, enable it by running the following command:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>sudo ufw enable<\/strong><\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>Allow Apache Full profile: <code>ufw<\/code> comes with predefined application profiles, and the <strong>&#8220;Apache Full&#8221;<\/strong> profile allows both HTTP (port 80) and HTTPS (port 443) traffic. Run the following command to allow the Apache Full profile:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>sudo ufw allow 'Apache Full'<\/strong><\/code><\/pre>\n\n\n\n<p>This command enables incoming traffic on ports 80 and 443 for both HTTP and HTTPS.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>Verify the rules: You can check the status of <code>ufw<\/code> and verify that the Apache Full profile rule is correctly configured by running the following command:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>sudo ufw status<\/strong><\/code><\/pre>\n\n\n\n<p>Ensure that the rules allowing ports 80 and 443 are listed in the output, associated with the &#8220;Apache Full&#8221; profile.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>Test the HTTP and HTTPS connections: Verify that both HTTP and HTTPS traffic is allowed by accessing your website over both protocols (e.g., http:\/\/yourdomain.com and https:\/\/yourdomain.com) from a client machine. If the connections are successful, it means the firewall is allowing the Apache Full profile.<\/li>\n<\/ol>\n\n\n\n<p>Please note that by allowing the Apache Full profile, you&#8217;re opening up ports 80 and 443 to incoming traffic, which includes both HTTP and HTTPS. It&#8217;s important to ensure that your Apache server is properly configured to handle HTTPS traffic with valid SSL certificates. Additionally, consider implementing other security measures like rate limiting, filtering by source IP, or enabling a Web Application Firewall (WAF) to enhance security for your web server.<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-22-04#step-3-allowing-https-through-the-firewall\"><\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-4-obtaining-an-ssl-certificate\">Step 4 \u2014 Obtaining an SSL Certificate<\/h4>\n\n\n\n<p>To obtain an SSL certificate for your Raspberry Pi web server, you can use Certbot, a popular tool that automates the process of obtaining and installing SSL certificates from Let&#8217;s Encrypt. Here&#8217;s how you can do it:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Install Certbot: Begin by installing Certbot on your Raspberry Pi by running the following commands:\n<ul class=\"wp-block-list\">\n<li><code><strong>sudo apt update<\/strong> <\/code><\/li>\n\n\n\n<li><code><strong>sudo apt install certbot<\/strong><\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Choose the Certbot plugin: Certbot supports various plugins for different web servers. Since you&#8217;re using Apache on your Raspberry Pi, you can use the Certbot Apache plugin. Install the plugin by running the following command:\n<ul class=\"wp-block-list\">\n<li><code><strong>sudo apt install python3-certbot-apache<\/strong><\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Obtain the SSL certificate: Run Certbot with the Apache plugin to automatically obtain and install the SSL certificate for your domain. Execute the following command:\n<ul class=\"wp-block-list\">\n<li><code><strong>sudo certbot --apache <\/strong><\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Certbot will guide you through the process, prompting you to select the domain you want to secure and providing options to configure your SSL settings.<\/li>\n\n\n\n<li>Configure HTTPS settings: Certbot will automatically configure Apache with the SSL certificate. However, depending on your specific requirements, you may need to make additional adjustments to your Apache configuration. For example, you might want to redirect all HTTP traffic to HTTPS or enable specific SSL protocols and ciphers. Modify your Apache configuration files accordingly.<\/li>\n\n\n\n<li>Test SSL configuration: Restart Apache to apply the changes and test your SSL configuration. Make sure your website loads correctly over HTTPS without any certificate warnings or errors. You can use online SSL testing tools like SSL Labs (<a href=\"https:\/\/www.ssllabs.com\/ssltest\/\">https:\/\/www.ssllabs.com\/ssltest\/<\/a>) to check the security and validity of your SSL certificate.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"592\" src=\"https:\/\/koner.mywire.org\/wp-content\/uploads\/2024\/02\/ssllabs-scan_ok.jpg\" alt=\"\" class=\"wp-image-346\" srcset=\"https:\/\/koner.mywire.org\/wp-content\/uploads\/2024\/02\/ssllabs-scan_ok.jpg 1024w, https:\/\/koner.mywire.org\/wp-content\/uploads\/2024\/02\/ssllabs-scan_ok-300x173.jpg 300w, https:\/\/koner.mywire.org\/wp-content\/uploads\/2024\/02\/ssllabs-scan_ok-768x444.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>By following these steps, you can obtain and install an SSL certificate for your Raspberry Pi web server using Certbot. Remember to regularly monitor and renew your SSL certificate to maintain a secure configuration.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5 \u2014 Verifying Certbot Auto-Renewal<\/h4>\n\n\n\n<p>Let\u2019s Encrypt\u2019s certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process, as well as to ensure that misused certificates or stolen keys will expire sooner rather than later.<\/p>\n\n\n\n<p>The&nbsp;<code>certbot<\/code>&nbsp;package you installed takes care of renewals by including a renew script to&nbsp;<code>\/etc\/cron.d<\/code>, which is managed by a&nbsp;<code>systemctl<\/code>&nbsp;service called&nbsp;<code>certbot.timer<\/code>. This script runs twice a day and will automatically renew any certificate that\u2019s within thirty days of expiration.<\/p>\n\n\n\n<p>To check the status of this service and make sure it\u2019s active, run the following:<\/p>\n\n\n\n<p><strong>sudo systemctl status certbot.timer<\/strong><\/p>\n\n\n\n<p>To test the renewal process, you can do a dry run with&nbsp;<code>certbot<\/code>:<\/p>\n\n\n\n<p><strong>sudo certbot renew &#8211;dry-run<\/strong><\/p>\n\n\n\n<p>Certbot keeps track of its renewal attempts and logs them in the system. You can review the logs to verify the renewal status. The log location may vary depending on your Linux distribution, but some common locations are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\/var\/log\/letsencrypt\/letsencrypt.log<\/code><\/li>\n\n\n\n<li><code>\/var\/log\/letsencrypt\/renew.log<\/code><\/li>\n\n\n\n<li><code>\/var\/log\/syslog<\/code><\/li>\n<\/ul>\n\n\n\n<p>Open the appropriate log file using a text editor and search for renewal-related entries. Look for successful renewal messages or any error messages that might indicate a problem with the renewal process.<\/p>\n\n\n\n<p>If you don\u2019t receive any errors, you\u2019re 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\u2019s Encrypt will send a message to the email you specified, warning you when your certificate is about to expire.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"step-4-obtaining-an-ssl-certificate\"><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-22-04#step-4-obtaining-an-ssl-certificate\"><\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s Encrypt is a Certificate Authority (CA) that facilitates obtaining and installing free&nbsp;TLS\/SSL certificates, thereby enabling encrypted HTTPS on web servers. It streamlines the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-192","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/comments?post=192"}],"version-history":[{"count":14,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/192\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/pages\/192\/revisions\/347"}],"wp:attachment":[{"href":"https:\/\/koner.mywire.org\/index.php\/wp-json\/wp\/v2\/media?parent=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}