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!

ONE

one

smiles filled with charms cast in each face
arms were tightened as it securely embrace
eyes were vigilant and heads that nod
as their feet were covered with mud.

tweets of the birds, and a whispering snore
synced with the pillows that fell in the floor
sneak and creep as water was being stolen
things as they are remembered all over again

wet shoulders, swollen eyes and crampy limbs,
days of darkness and sadness never ends
one would come, another one will wait
as if realizing what’s for their fate

checking time, check the pocket, seeks the wallet
never been present a hero off of his head
utters, laughter, and gestures,
deceived a monitoring collector

(para wel!)

another one was hurt and one regretted
why this gift came to life so late
one still do and another one’d like to
one came to another one and said “i love you”

depressed and down, starving and struggling,
is what one feel right now and been laying,
one is begging for another one to come
wished for a day of smiles and fun

before one will say his final goodbyes
reconciliation is needed to stop the cries
so please another one,
lets get back to ONE

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 😀

Basic UNIX commands

Unix Terminal Command

The most important 8 commands

ls dirname
Shows directory listing. If no directory dirname is specified, ls prints the names of the files in the current directory.

cd dirname
Change current directory. Without a dirname, it will return you to your home directory. Otherwise, it takes you to the directory named dirname.

mkdir dirname
Makes a sub-directory named dirname in the current directory.

rm filename(s)
Removes files. Careful with this one – it is irreversible. It is usually aliased ( in a user”s .cshrc file ) to rm -i which insures that rm asks you if you are sure that you want to remove the named file.

mv oldname newname
Changes the name of a file from oldname to newname. Can also be used to move a file from one directory to another, e.g. mv oldname a/oldname. After using mv, the file oldname no longer exists (at least not in its original location). Contrast with cp.

cat filename
Types a file to the screen without stopping. Usually more is better.

cp filename(s) path
Copies files from one directory/filename to another. cp f1 f2 makes a file f2 identical to f1. cp *.c src/ copies all files that end in “.c” into the “src” subdirectory. cp does not remove the original file. Contrast with mv.

more filename
Types a file to the screen, one page at a time

More Complete Alphabetical List

cd dirname
Change current directory. Without a dirname, it will return you to your home directory.
Otherwise, it takes you to the directory named dirname.

cmp file1 file2
Compares the contents of two files from eachother. Reports the first different character found, and the line nummber.

cp filename(s) path
Copies files from one directory/filename to another. cp f1 f2 makes a file f2 identical to f1. cp *.c src/ copies all files that end in “.c” into the “src” subdirectory. cp does not remove the original file. Contrast with mv.

diff file1 file2
Displays all the differences between two files or directories to the screen.

grep pattern filename
Finds all the occurrences of pattern in the file filename. grep has a sophisticated pattern matching capability – use man grep to see the details.

head –n filename
Type the first several lines of a file. If “-n´ is present, where n is an integer, the first n lines are shown. This is useful for getting the top of a large file for testing, especially if you redirect the output to a new file, e.g. head -50 big.txt > small.txt

ls dirname
Shows directory listing. If no directory dirname is specified, ls prints the names of the files in the current directory.

mkdir dirname
Makes a sub-directory named dirname in the current directory.

man name
Shows the full manual page entry for name. For example, man grep will give you the manual pages for the write command.

more filename
Types a file to the screen, one page at a time

mv filename oldname newname
Changes the name of a file from oldname to newname

pwd
Shows what directory you are currently in

rm filename(s)
Removes files. Careful with this one – it is irreversible. It is usually aliased ( in a user”s .cshrc file ) to “rm -i” which insures that “rm” asks you if you are sure that you want to remove the named file.

rmdir dirname
Removes the directory “dirname”.

tail –n filename
Type the last several lines of a file. If “-n´, where n is an integer, is present, the last n lines are shown. This is useful for checking if your program completed writing all of its expected output. See also cat, more, head.

wc filename
Counts the lines, words, and characters in a file. wc is particularly useful in conjunction with grep to count the number of occurrences of something, e.g., grep “>” seq.fa | wc can be used to count the number of sequences in a FASTA formatted sequence file.

Stringing Commands Together

One of the powerful features of the UNIX is its ability to string together commands using the vertical bar “|” or pipe operator. this is particularly useful to filter the output of one command through the more program in order to see it one page at a time, or through the grep program to extract only some of the output.

A few examples:

See a detailed directory listing, sorted by time, one page at a time
ls –alt | more

Run a program, a.pl, and see the result pagewise
a.pl | more

Get a directory listing that include only files with suffix ‘.dat’
ls –l | grep “.dat”

Count the files with suffix ‘.dat’ in the current directory
ls –l | grep “.dat” | wc