How to Set up an Apache Virtual Hosts on Ubuntu Linux

This post shows students and new users steps to configure an Apache Virtual Host file on Ubuntu Linux. A Virtual Host is a feature with Apache that allows users to run more than one website on a single server.

A Virtual Host file contains configuration directives for website, including a site’s document root, security policies, SSL certificate configuration, and much more. Each website that is configured within an Apache Virtual Host operates independently of each other with separate and unique settings.

Apaches Virtual Host feature allows webmaster to maximize a server resources by running multiple websites on one host instead of multiple hosts running multiple websites.

How to create website directory structures on Ubuntu Linux

When you’re running multiple websites on a single host, each website will have its own document root. A document root is the directory where the website files for a domain are stored and served in response to requests.

Below is an example of a directory structure for multiple website with unique content and domains.

/var/www/
├── example.com
│   └── public_html
├── example.net
│   └── public_html

As you can see above, each domain will have its own folder with a document root in included.

Example: /var/www/domain/public_html

Run the commands below to create directory for example.com domain with its document root.

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

Each document root will need an index.html file that will be shown to clients. Run the commands below to create an index.html file for the example.com domain.

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

Then copy and paste the content below into the file and save. Below is just a basic HTML file for testing purposes.

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

Once you save the file, we’re ready to configure Apache Virtual Host to reference this content. To avoid any permission issues, change the ownership of the domain document root directory and all files within the directory to the apache user (www-data) :

sudo chown -R www-data: /var/www/example.com

How to create an Apache Virtual Host file on Ubuntu Linux

Now that you’ve created a domain content at the directory above, go and configure Apache Virtual Host configuration file for the domain content above.

On Ubuntu Linux, Apache Virtual Hosts configuration files are located in the /etc/apache2/sites-available directory.

To create a virtual host file in the sites-available directory for our content above, run the commands below to create a site specifics Virtual Host configuration file

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

An example configuration that should work with most environments is shown below. Copy and paste the content below into the file above and save.

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

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

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

Once the file is saved, you can go and enable it to become a virtual host.

To enable the new virtual host file, you use the a2ensite helper script which creates a symbolic link from the virtual host file to the sites-enabled directory.

Run the commands below to enable the configuration file for our domain.

sudo a2ensite example.com.conf

After that, run the commands below to restart Apache service.

sudo systemctl restart apache2

Once Apache is restarted, you now browse to the server hostname or IP address and it should display the content file we created above.

Repeat this for other domains and Virtual Host files you want to create. You can create as many virtual host file as you can as long as the server is able to run them all.

That should do it!

Conclusion:

This post showed you how to create Apache Virtual Hosts on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.