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:
- A real domain, for example
yourdomain.com. - DNS for that domain pointing to your server’s public IP.
- Ports 80 and 443 reachable from the internet.
Replace every yourdomain.com below with your own domain.
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:
- An email address. Enter your active email.
- Agreement to the Let’s Encrypt terms. Answer yes.
- Whether to redirect HTTP to HTTPS. Choose the redirect. Visitors who open
http://will move tohttps://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.
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.
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 withsystemctl start httpd.
Hope this helps.
Comments