
Apache HTTP Server can serve different websites according to the requested hostname. This guide creates one virtual host using Ubuntu 24.04 LTS and its Apache 2.4 package. It does not mix in CentOS/RHEL commands: a2ensite, www-data and APACHE_LOG_DIR belong to this Ubuntu workflow.

Step-by-step explanation
- Receive: Identify the address and port.
- Match: Use Host and, for HTTPS, SNI.
- Route: Select the matching VirtualHost configuration.
- Check: Test the default site too.
Prerequisites and scope
Use a new or separate test server, a sudo-capable account and a domain you control for public deployment. Replace site.example.com consistently with your own hostname. Do not install over an existing Nginx, Apache, control-panel or container server without first reviewing its configuration and port ownership:
cat /etc/os-release
sudo ss -ltnp '( sport = :80 or sport = :443 )' Validate the virtual host and response checks in a separate environment. Package installation, system services and a publicly trusted TLS certificate require additional acceptance checks on your server; providing commands is not a claim of complete environment testing.
1. Install Apache
sudo apt update
sudo apt install apache2
apache2 -v
sudo systemctl status apache2 --no-pager The Ubuntu package can start the service during installation. Preserve SSH access when changing firewall rules and allow only the web ports you need. Do not disable the firewall. HTTPS needs port 443; HTTP redirection and HTTP-01 certificate validation can require port 80.
2. Create a separate document root
The deployment account owns the example files. The web server does not receive write access to the entire content tree.
sudo install -d -o "$(id -u)" -g "$(id -g)" -m 0755 /var/www/fc-example/public
cat > /var/www/fc-example/public/index.html <<'HTML'
<!doctype html>
<html lang="en"><meta charset="utf-8"><title>Apache test</title>
<h1>Apache virtual host OK</h1></html>
HTML
chmod 0644 /var/www/fc-example/public/index.html If an application needs an upload or cache directory, grant access only to that directory according to its requirements. A static website does not need the web server to modify its files.
3. Configure the virtual host
If /etc/apache2/sites-available/fc-example.conf already exists, inspect and back it up instead of overwriting an existing site. For a new file, use:
<VirtualHost *:80>
ServerName site.example.com
DocumentRoot /var/www/fc-example/public
<Directory /var/www/fc-example/public>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/fc-example-error.log
CustomLog ${APACHE_LOG_DIR}/fc-example-access.log combined
</VirtualHost> Options -Indexes disables directory listings. AllowOverride None prevents .htaccess files from altering this static example. Applications such as WordPress may need rewrite rules; permit only the required directives within the relevant directory.
sudo a2ensite fc-example.conf
sudo apache2ctl configtest
sudo apache2ctl -S Do not reload until configtest reports Syntax OK. Confirm that -S maps the hostname to the intended file and port. Then run:
sudo systemctl reload apache2
curl --fail --resolve site.example.com:80:127.0.0.1 http://site.example.com/
curl -I --resolve site.example.com:80:127.0.0.1 http://site.example.com/not-found The first request should return the test heading; the second should return 404. --resolve changes DNS resolution only for that curl request, not public DNS. Seeing a default site at the server’s IP address does not prove that the hostname selects the correct virtual host.
4. Complete DNS and HTTPS
Point the real domain’s A record and any AAAA record to the intended server. If you also need a www name, include it consistently in DNS, ServerAlias and the certificate. This example uses one hostname.
For the Certbot Apache plugin packaged by Ubuntu:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d site.example.com
sudo apache2ctl configtest
sudo certbot renew --dry-run Use your real domain only after its validation requirements are satisfied. DNS-01 validation or an external load balancer requires that deployment’s own certificate workflow. Review the virtual-host changes made by Certbot, verify HTTP-to-HTTPS redirection and test renewal. HTTPS should work before entering application credentials.
Acceptance, troubleshooting and rollback
Check HTTPS from outside the server, the certificate chain, an internal page and a nonexistent page. Confirm directory listings are disabled, logs contain no unexplained errors and the web account cannot modify application code.
- Address already in use: identify the process holding port 80 or 443; do not stop an existing server indiscriminately.
- 403: inspect directory traversal permissions and
Requirerules instead of granting777. - Wrong website: check
apache2ctl -S, the Host header and DNS including AAAA records. - Failed reload: inspect
configtestandjournalctl -u apache2. Repeated restarts do not repair invalid configuration.
To roll back, restore the previous site configuration or disable only this new example with sudo a2dissite fc-example.conf. Review any additional file created by the certificate tool. Run configtest before a controlled reload. Do not remove other sites or bulk-delete content directories, and keep backup files outside the document root.
Official references
- Install Apache on Ubuntu
- Apache 2.4 virtual-host examples
- apachectl and configuration testing
- Directory Options directive
- Certbot Apache plugin and renewal
Technical content and sources reviewed on October 4, 2026. Recheck the relevant official documentation when a version or provider changes.
Leave a Reply