How to Set Up Apache Virtual Hosts on Ubuntu

September 26, 2019

Introduction

Apache virtual hosts are an efficient method for hosting multiple websites using a single web server and a single IP address. When the Apache server receives a request on a virtual host system, it reads the hostname reported in the client's HTTP header and uses it to direct the traffic to the correct domain.

This tutorial shows how to set up Apache Virtual Hosts on Ubuntu.

How to set up Apache virtual hosts on Ubuntu.

Prerequisites

Set Up Name-Based Apache Virtual Hosts on Ubuntu

Running an Apache virtual host on Ubuntu requires setting up a dedicated directory for website data and creating a configuration file in the Apache sites-available directory. Follow the procedure below to create a name-based virtual host on Ubuntu.

Step 1: Enable Apache Modules

Before configuring and starting a virtual host, ensure that the mod_rewrite module is enabled. The module allows rule-based rewriting of URL requests and provides a mechanism for setting up virtual hosts.

Enable mod_rewrite by executing the following command:

sudo a2enmod rewrite
Enabling the rewrite module in Apache.

Restart the Apache web server for the changes to take effect:

sudo systemctl restart apache2

If successful, the command provides no output.

Step 2: Create Directories

Each virtual host needs a directory for storing website-related data. Create the main directory and the public_html subdirectory by entering the following command:

sudo mkdir -p /var/www/[domain]/public_html

Replace [domain] with the domain name for the virtual host. The command below creates directories for the example.com domain:

sudo mkdir -p /var/www/example.com/public_html

Step 3: Create Website Files

The public_html subdirectory contains all the website data that Apache serves for the specific virtual host. The procedure below describes how to create a single index.html file for testing purposes:

1. Create index.html using a text editor. This article uses Nano:

sudo nano /var/www/[domain]/public_html/index.html

2. Add the following sample HTML:

<html>
  <head>
    <title>This is a test page.</title>
  </head>
  <body>
    <h1>The domain [domain] has been configured successfully.</h1>
  </body>
</html>
Creating a sample index.html page.

Save and exit the file.

Step 4: Change Directory Ownership and Permissions

Prevent any permission-related issues by setting the ownership of the domain's root directory to the current user. Perform this action by executing the steps below:

1. Enter the following chown command:

sudo chown -R $USER:$USER /var/www/[domain]

2. Use the chmod command to grant the write permissions only for the current user:

sudo chmod -R 755 /var/www

Step 5: Create Virtual Host Configuration File

Apache Virtual Host configuration files are stored in the /etc/apache2/sites-available directory. Proceed with the steps below to create and set up a configuration file for a virtual host:

1. Use a text editor to create a CONF file.

sudo nano /etc/apache2/sites-available/[filename].conf

There is no rule for naming the file, but the best practice is to name it the same as the domain it describes. For example, if the file configures example.com, create the file named example.com.conf:

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

2. Add the following configuration block to create a basic configuration file:

<VirtualHost *:80>

ServerAdmin webmaster@[domain]
ServerName [domain]
ServerAlias www.[domain]
DocumentRoot /var/www/[domain]/public_html

ErrorLog ${APACHE_LOG_DIR}/[domain]-error.log
CustomLog ${APACHE_LOG_DIR}/[domain]-access.log combined

</VirtualHost>

Replace [domain] with the virtual host's domain name:

Editing the sample website's configuration file.

The file contains the following values:

  • ServerAdmin - The administrator's email address.
  • ServerName - The domain name.
  • ServerAlias - All other domain aliases and subdomains.
  • DocumentRoot - The directory for serving website files.
  • ErrorLog, CustomLog - The log files locations.

Save the file and exit.

Step 6: Enable Virtual Host

Enable the virtual host by creating a symbolic link from the configuration file to the sites-enabled directory:

1. Use the following a2ensite command to enable the virtual host:

sudo a2ensite [domain]

Apache reads the configuration file and enables the website.

Enabling the sample website.

2. Verify the configuration file syntax by entering the following command:

sudo apache2ctl configtest

If the syntax is correct, the output confirms it.

Testing configuration syntax for the sample website.

3. Restart Apache to apply the changes:

sudo systemctl restart apache2

4. Go to a web browser and access the website. The browser shows the website's index.html.

Visiting the sample website in Firefox.

Note: To create additional virtual hosts, repeat Step 2 to Step 6 for each new host.

Step 7: Disable Default Virtual Host (Optional)

Apache default virtual host handles requests that do not match any other available server configurations. The default host is configured and enabled when the user installs Apache web server on the system.

If the default virtual host is disabled, Apache rejects the requests that do not match any configured virtual hosts. This setting increases security and allows the user to enforce stricter control over the server's behavior.

To disable the default virtual host, use the a2dissite command:

sudo a2dissite 000-default.conf
Disabling the default virtual host in Apache.

Reload the server for the changes to take effect:

sudo systemctl reload apache2

Step 8: Configure UFW Firewall (Optional)

Enabling a firewall increases security and prevents malicious or unnecessary network traffic from reaching the server. Follow the steps below to enable Ubuntu's default UFW firewall and set it up to allow Apache.

1. Enter the following command to enable the firewall.

sudo ufw enable
Enabling the UFW firewall.

2. Add the rules to allow access to Apache.

sudo ufw allow in "Apache Full"
Configuring UFW to accept connections to Apache.

UFW now lets the Apache traffic pass through the firewall.

Conclusion

By following the steps of this tutorial, you have successfully created and configured an Apache virtual host on Ubuntu. You can now create name-based virtual hosts and use one IP address to host multiple domains on a single server.

Another way to run multiple Apache instances on a single server is to use Docker containers. Read our tutorial to learn how to install Apache in Docker.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
What Is VDI and How Does It Work?
October 1, 2019

This article explains what a virtual desktop environment is and how it can be implemented. It also details...
Read more
How to Set Up Apache Virtual Hosts on Ubuntu 18.04
September 26, 2019

Name-based virtual hosts allow you to have a number of domains with the same IP address...
Read more
How To Install PHP On Ubuntu 20.04 or 22.04
November 24, 2022

PHP is a script-based server-side programming language. PHP is often used to automate server tasks and is the...
Read more
What is Server Virtualization? Definition and How it Works
February 24, 2019

A virtualized server allows one piece of hardware to be used as multiple virtual servers. Learn about Server...
Read more