Apache virtual hosts allow you to host multiple websites on one server. This guide creates a virtual host configuration for a custom domain on Ubuntu 24.04 LTS.

Tested and valid on:

  • Ubuntu 24.04 LTS

Prerequisites

  • Ubuntu 24.04 LTS server
  • Apache installed
  • A domain name pointed to your server’s IP

Step 1 – Create the Document Root

Create the directory structure:

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/
sudo chmod -R 755 /var/www/example.com

Step 2 – Create a Sample Page

Create a test page:

nano /var/www/example.com/public_html/index.html

Add:


  example.com
  

Apache is serving example.com!

Step 3 – Create the Virtual Host Config

Create a new config file:

sudo nano /etc/apache2/sites-available/example.com.conf

Add:


    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

Step 4 – Enable the Virtual Host

Enable the site and reload Apache:

sudo a2ensite example.com.conf
sudo systemctl reload apache2

Step 5 – Disable the Default Site (optional)

If this is your only site:

sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Step 6 – Test Configuration

Check for errors:

sudo apache2ctl configtest

Step 7 – Verify in Browser

Navigate to http://example.com — you should see your test page.

Conclusion

Your Apache virtual host is configured on Ubuntu 24.04 LTS. Secure it with Let’s Encrypt SSL to serve traffic over HTTPS — covered in the next tutorial in this series.