Configuring Apache Virtual Hosts With SSL Certificates on CentOS
Apache virtual hosts let one CentOS server host several websites while keeping their document roots, logs, and security settings separate. Adding TLS certificates protects traffic between visitors and the server, enables HTTPS, and prevents browsers from displaying certificate warnings.
This guide uses httpd, mod_ssl, Firewalld, and Let’s Encrypt. The commands suit CentOS Stream and closely related RHEL-based systems, although package commands may vary slightly between CentOS 7 and newer releases.
Preparing Apache And DNS
Install Apache and the SSL module, then enable the service at boot:
sudo dnf install -y httpd mod_ssl
sudo systemctl enable --now httpd
On CentOS 7, use yum instead of dnf. Create a directory for the site and a simple test page:
sudo mkdir -p /var/www/example.com/public_html
echo '<h1>Example.com on Apache</h1>' | \
sudo tee /var/www/example.com/public_html/index.html
sudo chown -R apache:apache /var/www/example.com
Before requesting a certificate, point the domain’s DNS A record to the server’s public IPv4 address. For an Australian business using a .com.au domain, this is commonly managed through an auDA-accredited registrar. Confirm that example.com and www.example.com resolve correctly, particularly when the server is hosted in a Sydney or Melbourne data centre.
Creating The HTTP Virtual Host
Create /etc/httpd/conf.d/example.com.conf with a port 80 virtual host:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride None
Require all granted
</Directory>
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
The ServerName and ServerAlias values must match the names visitors use and the names included in the certificate. Check the configuration before reloading Apache:
sudo apachectl configtest
sudo systemctl reload httpd
If the result is Syntax OK, test the site with curl -I http://example.com. A server connected through an Australian NBN service may have a changing residential IP address, so a fixed public address or reliable dynamic DNS arrangement is important for production hosting.
Obtaining A Let’s Encrypt Certificate
Install Certbot from the Extra Packages for Enterprise Linux repository where required:
sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-apache
Permit web traffic through Firewalld before certificate validation:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Request and install the certificate with Apache integration:
sudo certbot --apache -d example.com -d www.example.com
Certbot can create the HTTPS virtual host and redirect HTTP traffic automatically. Review the generated configuration rather than assuming every directive suits the application. A certificate covers only the requested hostnames, so include every required subdomain at the time of issuance. Certificate renewal can be tested without changing live certificates:
sudo certbot renew --dry-run
Renewal timers generally run automatically. Confirm that outbound DNS and HTTPS access work from the server, especially when filtering traffic through a corporate firewall in Brisbane, Perth, or another Australian office.
Defining The SSL Virtual Host
A manually managed SSL virtual host can be placed in /etc/httpd/conf.d/example.com-ssl.conf:
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
<Directory /var/www/example.com/public_html>
AllowOverride None
Require all granted
</Directory>
ErrorLog /var/log/httpd/example.com-ssl-error.log
CustomLog /var/log/httpd/example.com-ssl-access.log combined
</VirtualHost>
The fullchain.pem file supplies the server certificate and intermediate chain, while privkey.pem contains the private key. Protect the key carefully; its permissions should prevent ordinary users from reading it. Apache needs permission to traverse the certificate directory, and SELinux must remain enabled rather than being disabled as a shortcut.
Apply changes with:
sudo apachectl configtest
sudo systemctl reload httpd
For administrators maintaining several Linux services, the same separation of configuration and logs is useful in other network roles; a related DHCP server guide demonstrates this style of structured system administration.
Troubleshooting And Hardening HTTPS
Check certificate presentation and the selected virtual host with:
openssl s_client -connect example.com:443 \
-servername example.com </dev/null 2>/dev/null | \
openssl x509 -noout -subject -issuer -dates
Use curl -I https://example.com to verify the response, and inspect /var/log/httpd/example.com-ssl-error.log when Apache returns a 403 or 500 error. Common causes include an incorrect DocumentRoot, missing DNS records, blocked port 443, an incomplete certificate chain, and SELinux labels that do not permit Apache to read website files.
For dynamic applications, add security headers only after testing their effect on the application. Keep Apache, OpenSSL, and CentOS packages updated, and avoid copying private keys into web directories or backup locations accessible through HTTP. Administrators who maintain mixed Linux and mobile development environments may also find a separate Android tutorial resource useful when diagnosing certificate behaviour in client applications.
A correctly configured deployment has matching DNS records, a dedicated virtual host, a valid certificate chain, open HTTP and HTTPS firewall services, and an automatic renewal path. The key point to remember is that Apache serves the right site only when DNS, hostnames, certificate names, filesystem permissions, SELinux, and Firewalld all agree.