Setting up Virtual Hosts for Apache in Debian 7 Wheezy

In this tutorial I’ll be setting up a virtual host for you can change that domain for your own.

Apache’s Virtual Hosts
The term Virtual Host refers to the practice of running more than one web site (such as and on a single machine. Virtual hosts can be “IP-based”, meaning that you have a different IP address for every web site, or “name-based”, meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.

Set Up
In order for you to follow the whole tutorial, you must have a root access or a sudoer account to the server.

Assuming you have a fresh installed machine that has no apache2 installed on it. To do so, use the following command:

sudo apt-get install apache2

1. Creating the Directory
Create the directory where our webfiles will reside. This working directory will be our “Document Root” for the Apache Virtual Configuration. In creating a directory with your ssh command we have to add -p option so it would automatically create including the parents of the new directory.

Note: Please make sure that the DNS of the domain or its IP address is pointing and to the server where you are setting up the Virtual Host. (’s ip address is already pointing to the server where i wish the site to be hosted.)

sudo mkdir -p /var/www/

Note: If you want to use an unapproved domain name to test the process, you may refer to my XAMPP-APACHE virtual host configuration.

2. Grant apache the ownership to the Document Root.

sudo chown -R www-data:www-data /var/www/

Next, make the directory readable to everyone/public.

sudo chmod -R 755 /var/www

chmod with option -R means you are granting READ permission to files and directories recursively

3. Create your test page with a valid starting filename.

index.html is commonly used as a starting page, you should put this file inside your “Document Root” directory.

sudo nano /var/www/

Here is a basic HTML code for your starting page.

<h1>It should Work!</h1>

Save & Exit.

4. Create a new Virtual Host File

You may duplicate or copy the default V-Host file and change its filename for your domain.

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/

5. Enable Virtual Hosts

Edit the newly created V-Host file.

sudo nano /etc/apache2/sites-available/

It will be the Virtual Host of our domain.

To begin, insert a line for the ServerName and ServerAlias under the ServerAdmin line.


The ServerName specifies the domain name that the virtual host uses.
Setting up the ServerAlias would make our site accessible with or without www.
It should now looks like this:

<VirtualHost *:80>

The next step is to specify the path of the directory where will get its web data. Misconfigured path of your “Document Root” will never let the Apache Server Pull up the webfiles.
The correct path of our defined “Document Root” looks like this:

DocumentRoot /var/www/

Save and Exit.
Finally we are now ready to activate our Virtual Host with the following command:

sudo a2ensite

6. Restart our Web Server
Modification of the configuration will only take effect after Apache is completely restarted.

sudo service apache2 restart

Defining our domain name to our server’s HOSTS file is no longer necessary since the IP of our domain resolves to our server’s IP.

7. Checking our work.
Access our domain, from a different computer. To double check, you may also type into the browser it’s ip. (Example:

The previously-made index.html should be parsed or be accessed in either way.

You should see this in our browser:

It should Work!!

We can host multiple sites, with multiple domains on a server with single IP address. All we have to do is to segregate every domain to its corresponding “Document Root” and simply repeat this procedure above.


Set a domain as your localhost.


XAMPP is a free and open source cross-platform web server package, consisting mainly of the Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP and Perl programming languages.


You can use your desired domain instead of using http://localhost,  like as a sample. It will even work even without internet connection.

Here are the simple steps:

1. Install XAMPP in your local computer. You can get it here for free. XAMPP

2. After a successful installation, you need to modify your “hosts” file.

It can be found in these directories:


Edit the “hosts” file with your favorite text editor.

3. Find the following texts: localhost

Add some configuration below on it:

It should now look like this: localhost

4. The next thing to do is to create a directory for your webfiles in the htdocs directory in xampp. Example of a directory name is “rastaval” (without double quotes).

I personally suggest to choose a directory name  for a specific domain.

5. The next thing to do is to explore the following directories:


Find edit httpd-vhosts.conf with your text editor.

6. Find #NameVirtualHost *:80:

Remove # to uncomment the line.

7. Create a virtual host. As what I did on my configuration with the domain in my localhost, it looked like this:

<VirtualHost *:80>
DocumentRoot “C:/xampp/htdocs/rastaval”

<VirtualHost *:80>
DocumentRoot “C:/xampp/htdocs/rastaval”

NOTE:* Be sure everyline of the configuration is not commented or prefixed with #.
Be sure you have created separately for the with www and non-www domains.

8. To check if it is working, create a file and save it as index.php

Edit index.php and enter this simple php function:

$host = $_SERVER[‘HTTP_HOST’];

<h1 style=”text-align:center;”>
PHP configuration of <?php echo $host; ?></h1>


Save it and it should be placed inside the rastaval directory to make it as a starting page for our domain

9. Now, run your XAMPP Control Panel, and tick Svc and Click the corresponding Start button for Apache. If you wish to use a database for your site, then you need to run MySQL as well. Ticking Svc means, the application will run as a service and everytime your computer starts, XAMPP will automatically run.

10. Open your favorite web browser, and I recommend Firefox, Flock or Google Chrome to use as your default web browser.
Type in the address bar,


11. And you’re done. If you wish to create more domains in your localhost, you can just repeat the procedure. Happy Hosting 😀