Setup a Name-Based Virtual Host on Ubuntu 24.04lts using Apache2

Prerequisites

– Access to an Ubuntu LAMP web server running Apache2 with root/sudo access

– Run the following commands:

– sudo apt update

– sudo apt upgrade

– sudo apt autoremove

– sudo reboot

Introduction

I will use the IP of my server which is 192.168.1.81 and two local-non-routable domain names for this tutorial. The IP address of 192.168.1.81 is a private IP address, which means it is not accessible from the Internet. I’ll be using default.internal and lamp.internal domain names. .internal is not accessible from the Internet. .internal is a local-non-routable Top Level Domain Name (TLD).

By using a private IP address for my server and using .internal I can create a simple network that I run out of my home office.

You will need to use your server’s IP address in place of mine. You will also need to use the domains you have decided to use to replace the .internal domain names that I use.

All of this will make more sense once the tutorial has been completed.

I use Ubuntu 24.04LTS as my server operating system and this tutorial is oriented towards Ubuntu and Apache2.

I fully configure the Apache2 default virtual host so it can be copied and modified to create other virtual hosts. This makes life easier.

This tutorial is broken down into two separate parts and assumes you are following along with a fully functional Ubuntu 24.04LTS server that is configured to run Apache2 and the PHP programming language. We will not be accessing the MySQL data server, however it should be installed and be fully functional.

Part 1 : Build out the default Apache virtual Host

Step 1 Ensure That the Required Modules Are Enabled

You typically need mod_ssl for SSL support (if using HTTPS) and mod_rewrite for URL rewriting.

– sudo a2enmod rewrite

– sudo a2enmod ssl

Step 2: Configure the Default Apache Virtual Host file 000-default.conf

– cd /etc/apache2/sites-available/

– vdir

– sudo vi 000-default.conf

– replace the code with the following code:

<VirtualHost *:80>
    ServerAdmin webmaster@site1.com
    ServerName default.internal
    ###ServerAlias www.site1.com
    DocumentRoot /var/www/html

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

    ErrorLog ${APACHE_LOG_DIR}/default.internal.error.log
    CustomLog ${APACHE_LOG_DIR}/default.internal.access.log combined
</VirtualHost>

Step 3: Add the .htaccess File Into the Docroot of the Default Apache2

– cd /var/www/html

– sudo vi .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # Remove index.php from URL
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Step 4: Check Your Apache Configuration For Syntax Errors

– sudo apache2ctl configtest

If there are no errors, you should see Syntax OK.

Step 5: Restart Apache to Apply The Changes

– sudo systemctl restart apache2

Step 6: Set Ownership and Permissions

– sudo chown -R www-data:www-data /var/www/html

– sudo chmod -R 755 /var/www/html

Step 7: Add the phpinfo() file

– sudo vi info.php

<?php
    phpinfo();
?>

Step 8: Test in Browser Using the IP Address

– 192.168.1.81

Step 9: Add Domain default.internal to the Windows and macOS/Linux Hosts File

Windows

– Open Notepage in administrator mode

– change directory to c:/system32/drivers/hosts

– Add IP and Domain

– Save and Close Notepad

macOS/Ubuntu (192.168.1.50)

– vi /etc/hosts

– Add IP and Domain

Step 10: Replace the Default Apache index.html Code With the Following

– sudo mv index.html index.html-original

– sudo vi index.php

– copy below to the index.php file:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>default.internal</title>
    </head>
    <body>
        <h1>default.internal</h1>
        <h2>The following proves the mod_rewrite is working</h2>
        <?php
            echo $_SERVER['REQUEST_URI'];
        ?>
    </body>
</html>

Step 11: Test Using a Browser

– Test with the IP address of your server

– Test using the local-non-routable domain name

– default.internal/index.php

– default.internal/info.php

Part 2 : Steps to Create a Second Virtual Host on the Same Server

Step 1: Create a Directory for Your New Web Site

– sudo mkdir -p /var/www/lamp.internal/public_html

Step 2: Set the Appropriate Permissions for the New Directory

– sudo chown -R www-data:www-data /var/www/lamp.internal/public_html

Step 3: Copy the Default Virtual Host Configuration File to Your New Virtual Host

– sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/lamp.internal.conf

Step 4: Edit the Configuration File lamp.internal.conf

– cd /etc/apache2/sites-available/

– sudo vi lamp.internal.conf

Make changes to :

– ServerName default.internal to ServerName lamp.internal

– DocumentRoot /var/www/html to DocumentRoot /var/www/lamp.internal/public_html

– <Directory /var/www/html/> to <Directory /var/www/lamp.internal/public_html/>

– default.error.log to lamp.internal.error.log

– default.access.log lamp.internal.access.log

Step 5: Copy the .htaccess File From the Default Docroot to the New Host Docroot

– sudo cp /var/www/html/.htaccess /var/www/lamp.internal/public_html/

Step 6: Copy the index.php and info.php Files From the Default Host to the lamp.internal Host

– sudo cp /var/www/html/index.php /var/www/lamp.internal/public_html/

– sudo cp /var/www/html/info.php /var/www/lamp.internal/public_html/

Step 7: Modify index.php

– cd /var/www/lamp.internal/public_html

– sudo vi index.php

– modify the title tag and the h1 tag

– save

Step 8: Edit the lamp.internal index.html File

– cd /var/www/lamp.internal/public_html/

– vdir

Step 9: Set Ownership and Permissions

– sudo chown -R www-data:www-data /var/www/

– sudo chmod -R 755 /var/www

Step 10: Check Your Apache Configuration for Syntax Errors

– sudo apache2ctl configtest

If there are no errors, you should see Syntax OK.

Step 11: Enable the new Host

– sudo a2ensite lamp.internal

Step 12: Restart Apache to Apply the Changes

– sudo systemctl restart apache2

Step 13: Update Your Hosts File

For local testing, you can edit your Windows/macOS/Linux hosts file to point your domain names to your local server. Open the file:

Windows:

– Open Notepad as administrator

– c:/windows/system32/drivers/etc/hosts

– Add 192.168.1.81 lamp.internal

Linux/macOS:

– sudo vi /etc/hosts (edit Kubuntu using Putty)

– Add 192.168.1.81 lamp.internal

Step 14 : Test Using a Browser

– Test with the IP address of your server

– Test using the local-non-routable domain name

– Test using the lamp.internal/controller/action/arg1 domain name

– Test using the lamp.internal/info.php domain name

This proves everything is working correctly to include PHP and mod_rewrite

Conclusion

In this article and associated YouTube videos we learned how to completely configure the Apache2 default virtual host. We learned how to configure and test the Apache2 module mod_rewrite. We learned how to configure the mod_rewrite .htaccess file. We also learned how to copy the Apache2 virtual host configuration to make a new virtual host on the same server.