How To Set Up Apache Virtual Host on Ubuntu


Before you begin this tutorial, you need to have Apache installed in order to work through these steps. If you haven’t already done so, you can get Apache installed on your server through apt-get:

After these steps are complete, we can get started.

For the purposes of this guide, my configuration will make a virtual host for This will be referenced throughout the guide, but you should substitute your own domains or values while following along.

I will show how to edit your local hosts file later on to test the configuration if you are using dummy values. This will allow you to test your configuration from your home computer, even though your content won’t be available through the domain name to other visitors.

Step One — Create the Directory Structure

The first step that we are going to take is to make a directory structure that will hold the site data that we will be serving to visitors.

Our document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the /var/www directory. We will create a directory here for the virtual host we plan on making.

Within this directory, we will create a public_html file that will hold our actual files. This gives us some flexibility in our hosting.

For instance, for our site, we’re going to make our directory like this:

Step Two — Grant Permissions

Now we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directory, we can change the ownership by doing this:

The $USER variable will take the value of the user you are currently logged in as when you press “ENTER”. By doing this, our regular user now owns the public_html subdirectory where we will be storing our content.
We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:

Your web server should now have the permissions it needs to serve content, and your user should be able to create content within the necessary folders.

Step Three — Create Demo Pages for Each Virtual Host

We have our directory structure in place. Let’s create some content to serve.

We’re just going for a demonstration, so our page will be very simple. We’re just going to make anindex.php page for this site.

Let’s start with We can open up an index.php file in our editor by typing:

In this file, we will put a simple statement that indicates the site it is connected to. My file looks like this:

Save and close the file when you are finished. You now have the page necessary to test the virtual host.

Step Four — Create New Virtual Host Files

Virtual host files are the files that specify the actual configuration of our virtual hosts and dictate how the Apache web server will respond to various domain requests.

Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create a virtual host file for our domain.

The default Ubuntu configuration requires that each virtual host file end in .conf.

Create the First Virtual Host File

Start by copying the file for the domain:

Open the new file in your editor with root privileges:

The file will look something like this (I’ve removed the comments here to make the file more approachable):

As you can see, there’s not much here. We will customize the items here for our first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.

First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.

After this, we need to add two directives. The first, called ServerName, establishes the base domain that should match for this virtual host definition. This will most likely be your domain. The second, calledServerAlias, defines further names that should match as if they were the base name. This is useful for matching hosts you defined, like www:

The only other thing we need to change for a basic virtual host file is the location of the document root for this domain. We already created the directory we need, so we just need to alter the DocumentRootdirective to reflect the directory we created:

In total, our virtualhost file should look like this:

Save and close the file.

Step Five — Enable the New Virtual Host Files

Now that we have created our virtual host file, we must enable that. Apache includes some tools that allow us to do this.

We can use the a2ensite tool to enable our site like this:

When you are finished, you need to restart Apache to make these changes take effect:

You will most likely receive a message saying something similar to:

This is a harmless message that does not affect our site.

<h2 “>Step Six — Set Up Local Hosts File (Optional)

If you haven’t been using actual domain names that you own to test this procedure and have been using some example domains instead, you can at least test the functionality of this process by temporarily modifying the hosts file on your local computer.

This will intercept any requests for the domains that you configured and point them to your VPS server, just as the DNS system would do if you were using registered domains. This will only work from your computer though, and is simply useful for testing purposes.

Make sure you are operating on your local computer for these steps and not your VPS server. You will need to know the computer’s administrative password or otherwise be a member of the administrative group.

If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:

If you are on a Windows machine, you can find instructions on altering your hosts file here.

The details that you need to add are the public IP address of your VPS server followed by the domain you want to use to reach that VPS.

For the domains that I used in this guide, assuming that my VPS IP address is, I could add the following lines to the bottom of my hosts file:

This will direct any requests for on our computer and send them to our server at This is what we want if we are not actually the owners of these domains in order to test our virtual hosts.

Save and close the file.

Step Seven — Test your Results

Now that you have your virtual host configured, you can test your setup easily by going to the domains that you configured in your web browser:

You should see a page that looks like this:



If this site works well, you’ve successfully configured the virtual host on the same server.

If you adjusted your home computer’s hosts file, you may want to delete the lines you added now that you verified that your configuration works. This will prevent your hosts file from being filled with entries that are not actually necessary.


If you followed along, you should now have a single server handling two separate domain names. You can expand this process by following the steps we outlined above to make additional virtual hosts.

There is no software limit on the number of domain names Apache can handle, so feel free to make as many as your server is capable of handling.

How to Enable MOD_REWRITE in Ubuntu

A little note first: This is the most basic way to enable mod_rewrite. However, it’s not the best way. The more correct way would be to setup a vhost and change its config, but unless you are a professional and know exactly what you do this thing will do the job perfectly.

Please note that Ubuntu 14.04 LTS comes with Apache 2.4, while Ubuntu 12.04 LTS came with Apache 2.2. This new version introduced different default config filenames and in general some differences.

Activate the mod_rewrite module with

and restart the apache

To use mod_rewrite from within .htaccess files (which is a very common use case), edit the default VirtualHost with

Search for “DocumentRoot /var/www/html” and add the following lines directly below:

Save and exit the nano editor via CTRL-X, “y” and ENTER.

Restart the server again:

Voila! To check if mod_rewrite is installed correctly, check your phpinfo() output.

How To Set Up Apache Virtual Host on Windows


As a webmaster, upon creating your first site, you may want to install a program like XAMPP. You then put your site in the /htdocs/ directory and it is then accessible via the URL: http://localhost/


But will happen if you start to develop several sites simultaneously ?

You end up with a multitude of sub folders, and URLs to extension, for example:


Which will act as the site root url in the development phase.

The problem is that absolute and relative URLs do not work when you host your site (live) and sometimes you may be confronted to lengthy urls to access the site, etc. …) then this tutorial will be helpful.

The objective of the tutorial

We will create virtual host and use short addresses such as:


  • This tutorial was written for people working under Windows environment, and using XAMPP server. But you will probably have no difficulty to implement this method with other software.
  • Assuming you have installed XAMPP under C:xampp.
  • Assuming you have placed your current project under C:xampphtdocsMyProject.
  • You need a text editor, such as Notepad.

Set the host for windows

Open the file: C:WindowsSystem32driversetchosts
At the end of the file you should have something like this: localhost
::1 localhost

Add the following line to the end of it or below the above written lines

And save the file.

Note: If you can not edit the file, verify that it is not read-only. Under Vista or Windows 7, you may encounter more problems with the UAC. Make a copy of the file on your desktop, set the write permissions, make the change, then put the file in read-only, and overwrite the original file.

Configuring Apache

Find the httpd.conf file in the apache folder inside xampp.


Open the above file and add the following lines,

It is essential to properly put the two host definitions. 1st one to retain your original localhost definition, and the 2nd one for your desired host name.

Save the file.
Restart your Apache server.