HTTPS is the secure version of HTTP. Data between the browser and the server is encrypted, so nobody in the middle can read it. Certbot is a free program that requests an HTTPS certificate from Let’s Encrypt. Let’s Encrypt is a free certificate provider. This tutorial continues the Virtual Host tutorial. You already have an Apache vhost for contoh.dom. Now we add HTTPS to that vhost.

Warning: contoh.dom cannot get a certificate

Read this part first. contoh.dom is a local domain. In the previous tutorial, that domain pointed to 127.0.0.1, your own computer. Let’s Encrypt cannot issue a certificate for a local domain. Let’s Encrypt must be able to reach your server from the internet. Here is how: Let’s Encrypt sends a challenge over port 80. This challenge is called an HTTP-01 challenge. Your server must answer that challenge. If your server cannot be reached from the internet, the challenge fails and no certificate is issued.

So, for a real certificate you need:

  1. A real domain, for example yourdomain.com.
  2. DNS for that domain pointing to your server’s public IP.
  3. Ports 80 and 443 reachable from the internet.

Replace every yourdomain.com below with your own domain.

HTTP-01 challenge flow: Let’s Encrypt checks the domain over port 80, then the certificate is issued

1. Change ServerName to your real domain

Certbot reads the domain name from the Apache vhost. So first change ServerName in the vhost file to your real domain:

sudo nano /etc/httpd/conf/vhosts/contoh.dom

Change this line:

ServerName contoh.dom

To this:

ServerName yourdomain.com

Change ServerAlias to the same name. Then check the configuration:

sudo apachectl configtest

The result must be Syntax OK. If there is an error, fix the line it mentions. If it is OK, restart Apache:

sudo systemctl restart httpd

2. Install Certbot

Install Certbot and the Apache plugin. The plugin configures Apache automatically:

sudo pacman -S certbot certbot-apache

3. Request a certificate

Run Certbot with the Apache plugin:

sudo certbot --apache -d yourdomain.com

Certbot will ask a few things:

  1. An email address. Enter your active email.
  2. Agreement to the Let’s Encrypt terms. Answer yes.
  3. Whether to redirect HTTP to HTTPS. Choose the redirect. Visitors who open http:// will move to https:// automatically.

Certbot then changes the vhost configuration. It adds an HTTPS block (port 443) and reloads Apache. The certificate is stored in /etc/letsencrypt/live/yourdomain.com/.

To include www in the same certificate, add another -d:

sudo certbot --apache -d yourdomain.com -d www.yourdomain.com

Note: Certbot changes Apache configuration files. Back up your vhost file before running Certbot, so you can go back if something goes wrong.

Steps to install free HTTPS: install Certbot, run certbot –apache, HTTPS is active

4. Check HTTPS

Open this address in your browser:

https://yourdomain.com

A padlock icon must appear in the address bar. Click the padlock to see the certificate details. Also check the list of certificates on the server:

sudo certbot certificates

This command shows the domains and the expiry date of each certificate.

5. Automatic renewal

A Let’s Encrypt certificate is valid for 90 days. You do not need to renew it by hand. The Certbot package on Arch ships a systemd timer called certbot-renew.timer. The timer checks certificates twice a day. A certificate renews automatically when less than 30 days remain. Enable the timer:

sudo systemctl enable --now certbot-renew.timer

Check that the timer is running:

systemctl list-timers | grep certbot

A line for certbot-renew.timer must appear.

Renewal timeline: certificate issued on day 0, timer renews it on day 60, new certificate before day 90

6. Test renewal

Test renewal without actually renewing. This command only simulates:

sudo certbot renew --dry-run

If it works, a message says all simulated renewals succeeded. If it fails, read the error and check the log at /var/log/letsencrypt/letsencrypt.log. Run this test once after setup, so you know renewal will work when the time comes.

If it fails

  • Certbot says the domain cannot be verified. Check your domain’s DNS. Make sure the domain points to the server’s public IP, not 127.0.0.1. Wait for DNS to spread if you just changed it.
  • Port 80 is closed. The HTTP-01 challenge needs port 80. Open ports 80 and 443 in the firewall or router.
  • Apache is not running. Check Apache status with systemctl status httpd. Start it again with systemctl start httpd.

Hope this helps.

References

  1. https://wiki.archlinux.org/title/Certbot
  2. https://certbot.eff.org/
  3. https://letsencrypt.org/