home

Search A2Z 24

How To Setup Virtual Hosts Using XAMPP

X

How To Setup Virtual Hosts Using XAMPP

I am currently using Windows 7 and I have XAMPP installed in the default folder so things might be different on your machine just Google them for your OS.
Now lets get underway.

By default XAMPP sets up all the sites you create to have http://localhost as its top-level domain, and giving your site http://localhost/site as the url unless you install your site in the top-level folder.
But what happens when you have multiple sites or you want to test out environments which would require you to have different domains, well I am going to teach you to do just that.

The Steps

  1. You need to have XAMPP installed
  2. Open up the Xampp control panel and stop Apache (Ensure that you don't have it running as a service.)
  3. Navigate to C:/xampp/apache/conf/extra or wherever you installed xampp
  4. Fire up your text editor with administrative privileges and open up httpd-vhosts.conf found in the C:/xampp/apache/conf/extra folder
  5. At the very bottom of the file paste the following
NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost

With out that line of code you will lose access to your default htdocs directory. IE. http://localhost/ will be inaccessible.

  1. Now copy and paste the code below.
<VirtualHost *:80>
     DocumentRoot "C:/xampp/htdocs/testsite"
     ServerName testsite.dev
     ServerAlias www.testsite.dev
     <Directory "c:/xampp/htdocs/testsite">
           Order allow,deny
           Allow from all
     </Directory>
</VirtualHost>

For Persons using the latest version of Apache (at time of this update 2.4 +) use the code below as the above code is for Apache versions below 2.4

<VirtualHost *:80>
     DocumentRoot "C:/xampp/htdocs/testsite"
     ServerName testsite.dev
     ServerAlias www.testsite.dev
     <Directory "c:/xampp/htdocs/testsite">
          AllowOverride All
          Require all Granted 
     </Directory>
</VirtualHost>

Note : Change everywhere you see test site to the location of your site and the domain name you would like to use

The most common ones are .dev, .loc and .local (I believe anything except the traditional .com / .net domains)

  1. Now we head over to our Windows Hosts File, to edit the HOSTS. the file will be located at C:/Windows/System32/drivers/etc/hosts, where hosts is the file.
127.0.0.1    localhost

look for the line above, and enter your site mimicking the layout

127.0.0.1    localhost
127.0.0.1    www.somesite.dev
127.0.0.1    www.multisite.dev
127.0.0.1    demo.multisite.dev
127.0.0.1    www.testsite.dev #change this to the domain name you chose earlier

change it to reflect the lines above (if you have problems saving it meant you didn't have your text editor running in admin mode.so open editor with administrator permission.

  1. Restart Apache and Enjoy.

About Author

by Admin

Share your thoughts!

Login as a member to access comment posting block !! click-here

Thoughts From Other Users (0)

No Comments

×