Skip to content
Default image

Configure Apache Virtual Host

January 26, 2023

In the previous post I’ve talked about installing Apache2 web server.

Apache is configured by default to host one website served from the /var/www/ directory.

The welcome page that we accessed to test the installation is located in /var/www/html directory but we can make use of virtual host in order to host multiple applications or websites (for example one-site.com and another-site.com) on the same server.

Now let’s create a directory structure in the /var/www/ for our site.

First thing we need to do is to create the directory for our website. I will give it a default name testing.dev. Inside this directory I will create another folder named public_html where the web files will be located.

sudo mkdir -p /var/www/testing.dev/public_html

We are using the mkdir command with the -p(–parents) option in order to create parent directories. No error will be shown if the directories already exists.

This directory we just created is owned by the root user, in order to allow a regular user to modify the files we change the ownership with the help of the following command:

sudo chown -R $USER:$USER /var/www/testing.dev/public_html

Using the $USER environment variable we assigned ownership of the directory to the current user.

The next command will set read access permission for the general web directory:

sudo chmod -R 755 /var/www

Time to add our index.html page

sudo nano /var/www/testing.dev/public_html/index.html

And to add the html code

As I am creating this on localhost, I will modify the host file in order to map the localhost address to my testing.dev environment, also avoiding registering the domain name.

sudo nano /etc/hosts

The next step is to create a virtual host file for our website. This file will represent the configuration used by our virtual host and it will also manage how Apache will respond to domain requests.

We will use the default virtual host file 000-default.conf which we will copy in order to create a virtual host file for our domain.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/testing.dev.conf

Please note that each virtual host file has to end in .conf

Open the testing.dev.conf file as root with you preferred editor and you will see the default settings which we are going to modify for our domain

ServerAdmin - an email that the administrator can receive emails through

ServerName - the base domain to match

ServerAlias - other names that should match, for example www.site.com

DocumentRoot - this reflects where the root directory is created

ErrorLog -  found in /var/log/apache2/error.log - shows all error reported by the HTTP server

AccessLog - found in /var/log/apache2/access.log - shows every page served and every file loaded by the server

Now that the virtual host file is created, let’s enable it. We will use the following command:

sudo a2ensite testing.dev.conf

a2ensite is a script that enables the specified site. It does this by creating symlinks within /etc/apache2/sites-enabled.

Before reloading Apache in order to activate the new configuration let’s disable the default site 000-default.conf

sudo a2dissite 000-default.conf

a2dissite disables a site by removing the symlinks created by a2ensite.

Let’s check the config file for syntax error and reload Apache

sudo apache2ctl configtest

sudo systemctl reload apache2

Open a broweser and access the page or run the curl command in the terminal

As the h1 from our page is stating…it works:).

We can create multiple domains on our Apache server, just by following the steps above.

Hope you find this post helpful.