Search blog

Saturday, February 18, 2012

Creating Virtual Hosts on Apache Web Server

For setting up Virtual Host in XAMPP 1.8.1 please visit the following page: http://soluzione08.blogspot.in/2012/12/creating-virtual-host-on-xampp-181.html

Note: The settings below are for general websites. For Drupal specific settings visit the following page: http://drupal.org/node/161975

When it’s first installed, Apache is capable of hosting only one website, which is identified in a local testing environment by the URL http://localhost/. To get around this restriction, it’s common practice to develop websites in subfolders of the Apache server root. For example, if you have two sites called site1 and site2 and create separate subfolders for them in the server root, you access them in your testing environment as http:// localhost/site1/ and http://localhost/site2/. This works perfectly well as long as you use document-relative links all the time. However, if you want to use links relative to the site root, you need to create virtual hosts for each site.

Virtual hosting is a technique that web servers use to host more than one website on the same machine. If you have bought a web-hosting package from a hosting company, it’s almost certainly on a shared server that uses virtual hosts. Continuing with the previous example, once you create virtual hosts for site1 and site2 in Apache, you can test them locally using http://site1/ and http://site2/. This is essential for testing sites that use links relative to the site root. If you’re serious about web development, you should learn sooner or later how to set up virtual hosts in your testing environment. Once you have mastered the technique, it takes only a few minutes to set up each one.

You can call your virtual hosts whatever you like, as long as you don’t use any spaces or characters that would be illegal in a domain name. I always use the same name as the actual website, without the top-level domain. For example, for my own site, http://foundationphp.com/, I have created a virtual host called foundationphp in my local testing setup. This means that I access it as http://foundationphp/. It’s then a simple matter of clicking in the browser address bar and adding the .com to see the live site. Whatever you do, don’t use the top-level domain as the name of a virtual host in your testing setup. If you do, your computer will always point to the local version of the site and never access the real one on the Internet.

Apache allows you to create as many virtual hosts as you want. It’s a two-stage process. First, you tell the operating system the names of the virtual hosts, and then you tell Apache where the files will be located. There are separate instructions for Windows and Mac OS X.

Registering virtual hosts on Windows


Although you can locate your virtual hosts anywhere on your hard drive system, it’s a good idea to keep them in a single top-level folder, as this makes it easier to set the correct permissions in Apache. The following instructions assume that all your virtual hosts are kept in a folder called C:\vhosts and show you how to create a virtual host called dwcs4 within that folder.

To edit the necessary files in Vista, you need to select Run as administrator even if you are logged in to an administrator account. For Notepad, go to Start ➤ All Programs ➤ Accessories, right-click Notepad, and select Run as administrator from the context menu.
Enter your administrator password when prompted. Inside Notepad, select File ➤ Open and navigate to the relevant file. The Open dialog box in Notepad shows only .txt files, so you need to select All Files (*.*) from the drop-down menu at the bottom right of the dialog box.

STEPS For Configuration:


1. Create a folder called C:\vhosts and a subfolder inside it called dwcs4.

2. Open C:\WINDOWS\system32\drivers\etc\hosts in Notepad or a script editor and look for the following line at the bottom of the file:
127.0.0.1 localhost
127.0.0.1 is the IP address that every computer uses to refer to itself.
3. On a separate line, enter 127.0.0.1, followed by some space and the name of the virtual host. For instance, to set up a virtual host for this book, enter the following:
127.0.0.1 dwcs4 or 127.0.0.1 sackid.local and also set ServerName (see bottom page to know more) also to sackid.local if you want something like http://sackid.local
4. If you want to register any further virtual hosts, add each one on a separate line and point to the same IP address. Save the hosts file and close it.

5. Open the Apache configuration file, httpd.conf. The default location is C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf. If you installed XAMPP, it should be at C:\xampp\apache\conf\httpd.conf.

6. Scroll down to the Supplemental configuration section at the end of httpd.conf, and locate the following section:
7. Apache uses the hash sign (#) to indicate comments in its configuration files.  Uncomment the command shown on line 463 in the preceding screenshot by removing the #, like this:
Include conf/extra/httpd-vhosts.conf
This tells Apache to include the virtual host configuration file, which you must now edit.

8. Save httpd.conf, and close it.

9. Open httpd-vhosts.conf. The default location is C:\Program Files\Apache Software Foundation\Apache2.2\conf\extra\httpd-vhosts.conf. If you installed XAMPP, it should be at C:\xampp\apache\conf\extra\httpd-vhosts.conf. The main part of the file looks like this:

10. Position your cursor in the blank space shown on line 15 in the preceding screenshot, and insert the following four lines of code:

<Directory C:/vhosts>
Order Deny,Allow
Allow from all
</Directory>


This sets the correct permissions for the folder that contains the sites you want to treat as virtual hosts. If you chose a location other than C:\vhosts as the top-level folder, replace the pathname in the first line. Remember to use forward slashes in place of backward slashes. Also surround the pathname in quotes if it contains any spaces.

11. Lines 27–42 in the preceding screenshot are examples of virtual host definitions. They show all the commands that can be used, but only DocumentRoot and ServerName are required. When you enable virtual hosting, Apache disables the main server root, so the first definition needs to reproduce the original server root. You then add each new virtual host within a pair of <VirtualHost> tags, using the location of the site’s web files as the value for DocumentRoot and the name of the virtual host for ServerName. If the path contains any spaces, enclose the whole path in quotes. If your server root is located, like mine, at C:\htdocs, and you are adding dwcs4 as a virtual host in C:\vhosts, change the code shown on lines 27–42 so they look like this:

<VirtualHost *:80>
DocumentRoot c:/htdocs
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/dwcs4
ServerName sackid.local
</VirtualHost>


For XAMPP, use C:/xampp/htdocs instead of C:/htdocs.

12. Save httpd-vhosts.conf, and restart Apache. All sites in the server root will continue to be accessible through http://localhost/sitename/. Anything in a virtual host will be accessible through a direct address, such as http://dwcs4/.

No comments:

Post a Comment

Thank you for your Feedback!
www.evagabond.me

Top 5 Posts (Weekly)