Hero Image

Configure Apache Virtual Hosts on Ubuntu 24.04

Configure Apache Virtual Hosts on Ubuntu 24.04

Step 1 – Create document root

mkdir -p /var/www/example.com/html
echo '<h1>Works!</h1>' > /var/www/example.com/html/index.html
chown -R www-data:www-data /var/www/example.com 2>/dev/null || \
chown -R apache:apache /var/www/example.com 2>/dev/null || true

Step 2 – Create virtual host file

Create /etc/apache2/sites-available/example.com.conf:

<VirtualHost *:80>
    ServerName   example.com
    ServerAlias  www.example.com
    DocumentRoot /var/www/example.com/html

    <Directory /var/www/example.com/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog  /var/log/apache2/example.com_error.log
    CustomLog /var/log/apache2/example.com_access.log combined
</VirtualHost>

Step 3 – Enable and reload

Enable the site:

a2ensite example.com.conf
systemctl reload apache2
a2ensite example.com.conf && systemctl reload apache2

Step 4 – Test

curl -H 'Host: example.com' http://localhost