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 … this is where doing so complicates things)
  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
  1. NameVirtualHost *:80
  1. <VirtualHost *:80>
  2. DocumentRoot "C:/xampp/htdocs"
  3. ServerName localhost
  4. </VirtualHost>
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 .. below the first code
  1. <VirtualHost *:80>
  2. DocumentRoot "C:/xampp/htdocs/testsite"
  3. ServerName testsite.dev
  4. ServerAlias www.testsite.dev
  5. <Directory "c:/xampp/htdocs/testsite">
  6. Order allow,deny
  7. Allow from all
  8. </Directory>
  9. </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
  1. <VirtualHost *:80>
  2. DocumentRoot "C:/xampp/htdocs/testsite"
  3. ServerName testsite.dev
  4. ServerAlias www.testsite.dev
  5. <Directory "c:/xampp/htdocs/testsite">
  6. AllowOverride All
  7. Require all Granted
  8. </Directory>
  9. </VirtualHost>
#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 would work fine … but don’t quote me on it)
  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.
  1. 127.0.0.1 localhost
look for the line above, and enter your site mimicking the layout
  1. 127.0.0.1 localhost
  2. 127.0.0.1 www.somesite.dev
  3. 127.0.0.1 www.multisite.dev
  4. 127.0.0.1 demo.multisite.dev
  5. 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.
  1. Restart Apache and test to make sure it is working
If you have any problems comment if not … give it a like
Share on Google Plus

About Unknown

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment