Setting up Virtual Hosts for Apache in Debian 7 Wheezy

In this tutorial I’ll be setting up a virtual host for rastaval.com 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 company1.rastaval.com and company2.rastaval.com) 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. (rastaval.com’s ip address is already pointing to the server where i wish the site to be hosted.)

sudo mkdir -p /var/www/rastaval.com/www_data

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/rastaval.com/www_data

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/rastaval.com/www_data/index.html

Here is a basic HTML code for your starting page.

<html>
<head>
<title>www.rastaval.com</title>
</head>
<body>
<h1>It should Work!</h1>
</body>
</html>

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/rastaval.com

5. Enable Virtual Hosts

Edit the newly created V-Host file.

sudo nano /etc/apache2/sites-available/rastaval.com

It will be the Virtual Host of our domain.

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

ServerName rastaval.com
ServerAlias www.rastaval.com

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>
ServerAdmin webmaster@rastaval.com
ServerName rastaval.com
ServerAlias www.rastaval.com
[…]

The next step is to specify the path of the directory where rastaval.com 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/rastaval.com/www_data

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

sudo a2ensite rastaval.com

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, rastaval.com from a different computer. To double check, you may also type into the browser it’s ip. (Example: http://192.186.184.158/)

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.

Cheers!

Set a domain as your localhost.

 xampp

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.

 domain

You can use your desired domain instead of using http://localhost,  like www.rastaval.com 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:

c:\WINDOWS\system32\drivers\etc

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

3. Find the following texts:

127.0.0.1 localhost

Add some configuration below on it:

127.0.0.1 rastaval.com
127.0.0.1 www.rastaval.com

It should now look like this:

127.0.0.1 localhost
127.0.0.1 rastaval.com
127.0.0.1 www.rastaval.com

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:

C:\xampp\apache\conf\extra

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 www.rastaval.com in my localhost, it looked like this:

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

<VirtualHost *:80>
DocumentRoot “C:/xampp/htdocs/rastaval”
ServerName www.rastaval.com
</VirtualHost>

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:

<?php
$host = $_SERVER[‘HTTP_HOST’];
?>

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

<?php
phpinfo();
?>

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

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, http://www.rastaval.com

testing

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