How to set up virtual hosts in Apache on Linux Ubuntu

Apache handles multiple websites by breaking them up into their own separate config files. This system makes it very easy to start or stop single websites, by adding or removing a symbolic link to the sites specific config file. A single apache server can handle any number of websites, if the server can handle the load.

Each config file has a domain, and a directory on the server that gets served to people who visit that domain.

This guide will explain the process of setting up virtual hosts, so that visitors get the right content depending on which domain they request.

Prerequisites

You will need a Linux server (in this case Ubuntu) with apache installed. To install apache on Ubuntu, run this command:

$ sudo apt-get install apache2

You will also need at least one domain pointing to the server, or configure your own computer’s host file to point a specific domain to the servers IP.

Create the Directories

We will start by creating the directory structure that will hold the content for the different sites.

Each website will have its own document root directory in /var/www. Now we create two directories for our example-sites:

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

Grant permissions

Now that the correct directories are created, we need to look over their permissions.

Make the www-data user and group own the directories:

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

Change the permissions so that everyone can read the files, but only the owners are allowed to write:

$ sudo chmod -R 755 /var/www

Create the virtual host files

Apache includes a default host file. Copy it to get started on a new one:

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Now we need to add two directives to the new file. Open it with your favorite text editor, and add the following lines:

ServerName example.com
ServerAlias www.example.com

This makes sure that the site will respond to the specified domains. If you want more names, just keep on adding ServerAliases.

We also need to change which directory this config file will serve:

DocumentRoot /var/www/example.com/public_html

Now, repeat the process of creating another config-file, but for your other sites. In our case, test.com:

<VirtualHost *:80>
    ServerAdmin admin@test.com
        ServerName test.com
    ServerAlias www.test.com
    DocumentRoot /var/www/test.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Once this file is saved, it’s time to enable both:

$ sudo a2ensite example.com.conf
$ sudo a2ensite test.com.conf

Disable the default vhost:

$ sudo a2dissite 000-default.conf

To apply the changes, restart apache2:

$ sudo systemctl restart apache2

Now, the webserver should respond with the correct content, depending on which hostname the request contains.

Leave a Reply

Your email address will not be published. Required fields are marked *