Hosting several websites on one server does not require several servers, or even several IP addresses. Apache virtual hosts on Ubuntu let a single Apache installation serve different sites for different domain names, each with its own document root, logs, and configuration. This guide shows how to set up Apache virtual hosts on Ubuntu 24.04 from installation to HTTPS, including a second site so you can see the separation working.
By the end, you will have two domains served from one server, each isolated in its own directory, with HTTPS certificates from Let’s Encrypt and separate access and error logs. The same pattern scales to dozens of low-traffic sites on a single VPS.

Prerequisites for Apache Virtual Hosts on Ubuntu
You need a server running Ubuntu 24.04 with sudo access, Apache installed or installable, and at least one domain name pointing to the server. For this guide, the domains are site1.example.com and site2.example.com; substitute your own domains throughout.
Both domains must resolve to the server’s public IP before you request HTTPS certificates, because Let’s Encrypt validates ownership over HTTP. Check resolution first:
dig +short site1.example.com
dig +short site2.example.com
You also need a firewall that allows HTTP and HTTPS. Apache’s installer does not open ports for you, so handle that explicitly in the first step.
Step 1: Install Apache on Ubuntu 24.04
Install Apache from the Ubuntu repositories, enable it to start on boot, and allow web traffic through UFW:
sudo apt update
sudo apt install -y apache2
sudo systemctl enable --now apache2
sudo ufw allow 'Apache Full'
sudo systemctl status apache2
Confirm Apache serves its default page with curl -I http://localhost/, which should return a 200 response. The default site lives in /var/www/html and is defined in /etc/apache2/sites-available/000-default.conf. You will leave it disabled at the end, once your own virtual hosts are working.
Step 2: Create Document Roots and Test Pages
Create a separate document root for each site, with ownership set to your user account so you can deploy files without sudo for every edit. Add a simple index page to each so you can tell the sites apart during testing:
sudo mkdir -p /var/www/site1.example.com /var/www/site2.example.com
sudo chown -R $USER:$USER /var/www/site1.example.com /var/www/site2.example.com
echo "<h1>Site One</h1>" > /var/www/site1.example.com/index.html
echo "<h1>Site Two</h1>" > /var/www/site2.example.com/index.html
Keeping each site in its own directory is the core discipline of virtual hosting. Shared directories lead to shared mistakes: a deployment to one site overwriting another, or a permission change breaking both.
Step 3: Create the Apache Virtual Hosts Configuration
Apache virtual hosts are defined in files under /etc/apache2/sites-available/, one file per site. Create a virtual host for the first site, with its own server name, document root, and log files:
sudo nano /etc/apache2/sites-available/site1.example.com.conf
<VirtualHost *:80>
ServerName site1.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/site1.example.com
ErrorLog ${APACHE_LOG_DIR}/site1.example.com-error.log
CustomLog ${APACHE_LOG_DIR}/site1.example.com-access.log combined
</VirtualHost>
Create an identical file for the second site, changing only the server name, document root, and log names. Then enable both sites, disable the default site, test the configuration, and reload Apache:
sudo a2ensite site1.example.com.conf
sudo a2ensite site2.example.com.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
Visit both domains in a browser. Each should show its own test page. If both show the same page, Apache matched the wrong virtual host, which usually means a typo in a ServerName or a missing reload.
Step 4: Add HTTPS with Let’s Encrypt and Certbot
Plain HTTP virtual hosts are fine for testing but not for production. Certbot’s Apache plugin obtains a certificate for each domain and updates its virtual host configuration automatically:
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d site1.example.com -d site2.example.com
Choose the option to redirect HTTP to HTTPS when asked. Certbot creates separate SSL virtual host files for each domain, so the sites remain independent after the change. Verify both sites and the renewal timer:
curl -I https://site1.example.com/
curl -I https://site2.example.com/
sudo certbot renew --dry-run
Certificates renew automatically through a systemd timer. If the dry run fails, fix DNS or firewall access to port 80 before the real expiry date, not after.
Step 5: Harden and Maintain Apache Virtual Hosts
Disable directory listing so visitors cannot browse a site’s file structure when no index file exists, and hide the Apache version signature from error pages. Add these directives inside each virtual host or in a shared configuration snippet:
<Directory /var/www/site1.example.com>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ServerTokens Prod
ServerSignature Off
Set AllowOverride None unless a site genuinely needs .htaccess files; per-directory overrides slow Apache down and make configuration harder to audit. Review logs per site, not just globally, because separate logs are what make debugging one site without noise from the others possible:
sudo tail -f /var/log/apache2/site1.example.com-error.log
sudo tail -f /var/log/apache2/site2.example.com-access.log
Troubleshooting Apache Virtual Hosts on Ubuntu
If every domain shows the first site, Apache is falling back to its default virtual host. The first virtual host defined for an address becomes the default, so check that each ServerName is spelled correctly and that the site is actually enabled with a2ensite. List enabled sites with ls /etc/apache2/sites-enabled/.
If a site returns 403 Forbidden, check document root ownership and permissions: Apache runs as www-data and needs read access to the files and execute access to every parent directory. A root-owned document root with mode 700 is a common cause.
If Certbot fails, the usual causes are DNS not pointing at the server, port 80 blocked, or an existing redirect interfering with the HTTP-01 challenge. Test the challenge path manually and confirm the domain resolves from outside your network before retrying.
Conclusion
Apache virtual hosts on Ubuntu turn one server into a clean multi-site host: separate document roots, separate logs, separate certificates, and a configuration file per site that is easy to review and version. Add sites by copying the pattern, never by editing a shared configuration, and test with apache2ctl configtest before every reload. For related hardening, see our guides on UFW firewall setup on Ubuntu 24.04 and hardening SSH on Ubuntu 24.04. The official Apache virtual host documentation covers name-based, IP-based, and port-based hosting in full.