Creating an Apache website – a VHOST for your Domain

In this post we are going to create a virtual host for your website. But what is a virtual host? A virtual host or VHOST is a website configuration file.

The adjective virtual refers to the fact that a Apache web server can host more than website, so you can create more than one of these configuration files.

The instructions in this post are intended for Debian and Ubuntu based Apache2 web servers. Debian has its own way of organizing the Apache folder and the configuration files regarding.

Since Ubuntu is a derivative of Debian, the instructions here are also valid for Ubuntu. The combined market share of Linux websites for Debian and Ubuntu is almost 60%, so you are in a good company running a website on Debian or Ubuntu.

Creating a Apache website takes three steps:

  1. create the VHOST file
  2. create the web folder
  3. enable the website

Before we take off, it might be a good idea to get a little familiar with Apache.

Structure of the Apache Directory

We start with a brief exploration of the Apache Directory.

Login to your server and jump to the Apache folder:

To get a (long) list of the content of this directory, enter:

And what you should be something like this:

The most relevant items for our purpose are:

  • apache2.conf
  • mods-available/enabled
  • sites-available/enabled

The apache2.conf file is the main Apache configuration file for versions 2.0 and up.

sites-available and sites-enabled

Let us have a look at the sites-available folder:

The terminal replies with:

The 000-default.conf and default-ssl.conf are the two default configuration files for virtual hosts of the Apache web server. We can use these VHOST files as a template for our own virtual hosts.

The availability of a configuration file does not mean that the site regarding is also up and running.

To check which websites are running, type:

The server responds with:

At the moment, there is only site enabled: 000-default.conf. This site is enabled as soon as you install Apache. You can see it when you drop the IP address of your server in the address bar of a browser.

The -> sign directly after 000-default.conf indicates that this is a symlink.

A symbolic link, or symlink for short, is the Linux equivalent of a Windows shortcut.

Here the symlink links to the 000-default.conf file in the sites-available folder.

mods-available and mods-enabled

The Apache web server is deployed in many different situations.

To keep your Apache configuration lean and mean, it is advised to enable only those Apache modules required for your specific situation.

These Apache modules are located in the mods-available directory.

To get an impression, enter:

I count 130 modules. You will understand that loading a module requires system resources. The more modules you load, the bigger the impact on the performance of the server. That is exactly why you should only enable those modules required for your server environment.

Just as with enabling sites, enabling a module places a symlink in the mods-enabled folder to the appropriate module in the mods available directory.

Let us checkout the enabled modules:

This time, I count 29 modules. So, from the available modules, only about one out of five is actually enabled – loaded.

Creating the VHOST

As mentioned earlier, we will use the default VHOST files as a template to create a configuration file for our website.

We can simply copy the default file, but we have to do that with sudo.

Here, I will be using the domain name hostingcaddie.com, you should of course replace this by your own domain name.

Let us move to the sites-available folder for the job at hand:

Now copy the default configuration file to create a new one:

We just created a VHOST for the domain hostingcaddie.com.

Let us check the available sites:

Now we see:

Indeed, hostingcaddie.com has been created and is included in the listing of available sites.

Now we have to edit our VHOST:

This is what the default VHOST file looks lie:

We are going to update this file for our situation.

Please note that lines starting with a # are comments, intended for us humans. Apache will ignore these lines.

That is exactly why we have to remove the # just before ServerName.

Replace www.example.com with your naked domain name, the domain without www. So for example, so that is becomes:

Add on a blank line, directly below the ServerName:

What we have achieved, is that no matter whether a visitor to our site enter hostingcaddie.com or www.hostingcaddie.com, he or she will arrive on this site.

Replace the e-mail address next to ServerAdmin with yours, i.e.

Apache will display this e-mail address in error message, so it will be exposed to your visitors when something is amiss.

The DocumentRoot tells Apache where the files of the website are located, so replace the /var/www/html for example with /var/www/hostingcaddie/public_html:

When you do not like typing at the command line, you can replace hostingcaddie with for example ‘hc’ or ‘hccom’. Just make sure it makes sense to you.

Put the cursor on a blank line below the DocumentRoot, and enter:

Save the file with Ctrl+O.

Exit with Ctrl+X.

You have just created your first VHOST. My copy – without the sheer of comments preceded by the # sign – looks like this:

We are finished with the VHOST for the website HostingCaddie.

Creating the Document Root

The next step is creating the document root, the folder where we are going to put the files of the website.

We can make the sub directory and all necessary parent directories in one hit by adding the -p (for parents) parameter:

Make sure to use the document root as stated in your VHOST file.

Next, we need to create a simple file to test our server configuration and VHOST:

Enter a text that you would like to display on the homepage of this site. This is just a temporary file for testing purposes that will be replaced with the files of your website.

For example, you could enter the domain name between header 1 tags:

<h1>www.hostingcaddie.com</h1>

Save with Ctrl+O

Exit with Ctrl+X

Done.

Enabling the VHOST

We have a VHOST, we have a web folder – the document root. We even have a simple index.html in that web folder. Hence, we have everything to test our website.

Before we can use the website, we have to enable it with the a2ensite command:

You can read a2ensite as ‘apache2 enable site’. As you probably will have noticed, you can leave off the .conf extension.

Apache checks the enabled configuration files alphabetically, so an enabled 000-default.conf will be executed before hostingcaddie.com.conf.

Since we are not ready yet to direct the domain name to the IP address of our new web server, we can simply disable the default website – so that our VHOST becomes the first and only one:

Indeed, you can read a2dissite as ‘apache2 disable site’.

We have changed the configuration of the web server, but Apache will continue working with the previous configuration until we restart or reload Apache:

When you enter the IP address of the server, you should see the text that you have entered in the index.html file above. Everything is ready to create or move the web files.

More details