TürkiyeVPS

Free Let's Encrypt SSL on Apache (Ubuntu and Debian)

Install Apache on Ubuntu 24.04 or Debian 12, set up a VirtualHost and get a free Let's Encrypt certificate with Certbot. HTTPS redirect, auto-renewal, .htaccess and common errors, step by step.

6 min read

Apache is still one of the most common web servers for WordPress and PHP applications that rely on .htaccess rules. This guide shows how to install Apache on Ubuntu 24.04 or Debian 12, publish your site with a VirtualHost and secure it with a free Let's Encrypt certificate using Certbot. By the end your site opens over HTTPS, plain HTTP is redirected automatically and the certificate renews on its own.

We use example.com in the examples; replace it with your own domain. We assume you run the commands as root or as a user with sudo. If you prefer Nginx, see our Nginx and Let's Encrypt SSL guide.

Before you start: what you need

  • A server running Ubuntu 24.04 or Debian 12 and SSH access (new to SSH? read What is SSH).
  • A domain name and a panel where you can edit its DNS records.
  • A public IPv4 address for the server with ports 80 and 443 reachable from the internet. Let's Encrypt connects to port 80 to validate your domain. Low-cost VPS plans that run behind a shared IP (NAT) with only an SSH port open do not have these ports; to publish a site on such a server you need a dedicated IPv4 address. We explain the difference in Dedicated IP or NAT IP?.

Step 1: Point DNS to the server

In your DNS panel create two A records, @ and www, both pointing to the server's IPv4 address. If the server has no IPv6, do not add an AAAA record, or validation may go to the wrong address. Check that the records have propagated:

dig +short example.com A
dig +short www.example.com A

If you see your server's IP, continue. We explain how DNS works in What is DNS.

Step 2: Install Apache

sudo apt update
sudo apt install -y apache2
systemctl status apache2 --no-pager

If you see active (running), opening http://SERVER_IP in a browser shows Apache's default "It works!" page.

Step 3: Allow ports 80 and 443 in the firewall

If you use UFW, allow SSH first and then Apache's two ports:

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw status

UFW is not installed by default on Debian 12; skip this step if you do not use it. If your SSH port is not 22, open it with ufw allow PORT/tcp first, or you will lock yourself out.

Step 4: Create the site directory

sudo mkdir -p /var/www/example.com/public
echo '<h1>example.com works</h1>' | sudo tee /var/www/example.com/public/index.html
sudo chown -R www-data:www-data /var/www/example.com

Step 5: Configure the VirtualHost

Create /etc/apache2/sites-available/example.com.conf:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public

    <Directory /var/www/example.com/public>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

AllowOverride All is needed for the .htaccess rules of applications such as WordPress. Enable the site, disable the default one, turn on mod_rewrite and test the configuration:

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl reload apache2

You should see Syntax OK. http://example.com now serves your page.

Step 6: Get the Let's Encrypt certificate with Certbot

Install Certbot with its Apache plugin and request the certificate:

sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

Certbot asks for an email address (for expiry warnings) and for you to accept the terms. It then validates your domain, creates an HTTPS VirtualHost called example.com-le-ssl.conf and adds a rule that redirects HTTP to HTTPS. Open https://example.com and check the padlock.

Step 7: Check automatic renewal

Let's Encrypt certificates are valid for 90 days; the Certbot package installs a timer that renews certificates before they expire. Test renewal in dry-run mode:

sudo certbot renew --dry-run
systemctl list-timers | grep certbot

If there are no errors, there is nothing else to do.

Common errors

Validation failed (Timeout / Connection refused). Port 80 is not reachable from outside. Check the firewall, that the server really has a public IP and that the DNS record points to that IP. On a VPS behind NAT port 80 is not open, so validation cannot pass.

"Could not find a VirtualHost" / ServerName error. Certbot cannot find a VirtualHost with a ServerName line. Make sure the file from step 5 has ServerName and ServerAlias and that the site is enabled with a2ensite.

AH00558 warning. "Could not reliably determine the server's fully qualified domain name" is harmless; to silence it run echo 'ServerName localhost' | sudo tee /etc/apache2/conf-available/servername.conf and sudo a2enconf servername.

Too many redirects (ERR_TOO_MANY_REDIRECTS). If your domain is proxied by Cloudflare with SSL mode "Flexible", Cloudflare talks HTTP to the server while Apache redirects to HTTPS, which loops. Set Cloudflare SSL to "Full (strict)".

.htaccess rules do not work. Check the AllowOverride All line and sudo a2enmod rewrite, then run sudo systemctl reload apache2.

Frequently Asked Questions

Is a Let's Encrypt certificate really free?

Yes. Let's Encrypt issues domain-validated (DV) certificates for free. They are valid for 90 days and Certbot renews them automatically; browsers trust them as much as paid DV certificates. We explain what SSL does in What is an SSL certificate.

Should I use Apache or Nginx?

If you run WordPress or a PHP application that relies on .htaccess rules, Apache is the least effort. If you need many concurrent connections on a small amount of RAM, or a reverse proxy, Nginx is lighter; the steps are in our Nginx guide.

Can I add SSL to several sites on one server?

Yes. Create a separate directory and VirtualHost file for each site, then run certbot --apache -d domain -d www.domain for each one.

I do not want to manage a server. Is there an easier way?

Yes. Our web hosting plans come with free SSL ready and an automatic daily backup of your account, so you never touch Apache, PHP or certificate settings. If you want root access and your own configuration, a VPS is the better fit.

Publish your site in Istanbul

If you want a website without managing a server, our web hosting plans come with PHP 8.3, MariaDB, free SSL and automatic daily backups. If you prefer to run Apache yourself, get a VPS and add a dedicated IPv4 address so ports 80 and 443 reach your server directly. You pay in USDT or LTC, and if you get stuck at any step, write to us in live chat.