Ubuntu

How To Set Up Apache Virtual Hosts on Ubuntu 24.04 LTS

Apache is one of the most widely used web server platforms, and for good reason. It’s highly flexible, robust, and well-supported, making it an excellent choice for hosting multiple websites on a single server. With Apache’s Virtual Hosts feature, you can easily configure your server to host numerous sites at once. In this blog post, we’ll walk through the steps to set up Apache Virtual Hosts on Ubuntu 24.04 LTS.

Prerequisites

Before we dive into the configuration, ensure you meet the following prerequisites:

  • You have a server running Ubuntu 24.04 LTS.
  • You have administrative (sudo) privileges.
  • Apache is already installed. If not, you can install it by running:
  sudo apt update
  sudo apt install apache2
  • You have basic knowledge of the command line.

Step 1: Enable Required Apache Modules

First, ensure that mod_rewrite and mod_vhost_alias are enabled in your Apache installation. These modules help with URL rewriting and virtual host configurations.

To enable these modules, run the following commands:

sudo a2enmod rewrite
sudo a2enmod vhost_alias

After enabling the modules, restart Apache:

sudo systemctl restart apache2

Step 2: Create Directory Structure for Your Websites

Next, let’s create a directory structure for hosting your websites. For this example, we’ll set up two virtual hosts: example1.com and example2.com.

sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html

Next, assign ownership of these directories to the www-data user (the default Apache user):

sudo chown -R www-data:www-data /var/www/example1.com/public_html
sudo chown -R www-data:www-data /var/www/example2.com/public_html

Now, set the appropriate permissions:

sudo chmod -R 755 /var/www

Step 3: Create Sample Index Files

For demonstration purposes, let’s create a simple index.html file in each website’s directory.

echo "<html><h1>Welcome to Example 1</h1></html>" | sudo tee /var/www/example1.com/public_html/index.html
echo "<html><h1>Welcome to Example 2</h1></html>" | sudo tee /var/www/example2.com/public_html/index.html

Step 4: Create Virtual Host Configuration Files

Next, we need to create configuration files for our virtual hosts. Apache stores its configuration files in the /etc/apache2/sites-available/ directory.

To create a configuration file for example1.com, run:

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

Then, add the following configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/example1.com/public_html

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

    ErrorLog ${APACHE_LOG_DIR}/example1.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example1.com-access.log combined
</VirtualHost>

Now, create a configuration file for example2.com:

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

Add the following configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example2.com
    ServerAlias www.example2.com
    DocumentRoot /var/www/example2.com/public_html

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

    ErrorLog ${APACHE_LOG_DIR}/example2.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example2.com-access.log combined
</VirtualHost>

Step 5: Enable Virtual Hosts

To enable these new virtual hosts, use the a2ensite command:

sudo a2ensite example1.com.conf
sudo a2ensite example2.com.conf

After enabling the sites, it’s a good practice to disable the default Apache site:

sudo a2dissite 000-default.conf

Step 6: Test Your Configuration

To ensure that your Apache configuration does not have any syntax errors, run:

sudo apache2ctl configtest

If everything is correct, you should see a message that says: Syntax OK.

Step 7: Restart Apache Again

Once the configuration is verified, restart Apache to apply the changes:

sudo systemctl restart apache2

Step 8: Update Your Hosts File (Optional)

While testing on a local machine, you may want to update your /etc/hosts file to point to your new virtual hosts. Use the following command:

sudo nano /etc/hosts

Add the following lines:

127.0.0.1 example1.com
127.0.0.1 example2.com

Step 9: Access Your Websites

Finally, open a web browser and visit http://example1.com and http://example2.com. You should see the welcome pages for both sites.

Conclusion

Congratulations! You have successfully set up Apache Virtual Hosts on Ubuntu 24.04 LTS. This powerful feature allows you to host multiple websites on a single server, making your configuration more efficient and resource-friendly. Don’t forget to keep your server and websites updated, and consider configuring SSL certificates for a more secure browsing experience. Happy hosting!