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
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</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
<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>

#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.
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.

  1. Restart Apache and test to make sure it is working

If you have any problems comment if not … give it a like

Advertisements

22 Replies to “How To Setup Virtual Hosts Using XAMPP”

      1. I give:
        “Error: Apache shutdown unexpectedly.
        06:09:41 p.m. [Apache] This may be due to a blocked port, missing dependencies,
        06:09:41 p.m. [Apache] improper privileges, a crash, or a shutdown by another method.
        06:09:41 p.m. [Apache] Press the Logs button to view error logs and check
        06:09:41 p.m. [Apache] the Windows Event Viewer for more clues
        06:09:41 p.m. [Apache] If you need more help, copy and post this
        06:09:41 p.m. [Apache] entire log window on the forums

      2. what I would do is close Skype if it is open … and try again
        then I would try running xampp with admin privileges
        if I still have the problem I would reinstall xampp
        but if you got the problem after doing this tutorial

        the problem might be that you forgot to change the closing virtual host tag in the first section so that it looks like the closing virtual host tag in the second part

        *the code is screwed because wp.com keeps filtering code for HTML and JavaScript tags

  1. If you receive an access denied when you navigate to your set up dev domain you should change this line of code:

    AllowOverride none
    Require all denied

    from the httpd.conf file found in “c:xamppapacheconf” folder; change that code to :

    AllowOverride All
    Require all granted

    Save and restart Apache, should load fine now 🙂

  2. I’ve got some probelm…
    ——————————————-
    Not Found

    HTTP Error 404. The requested resource is not found.
    ————————————————

    That’s my code

    NameVirtualHost *:8080

    ServerName localhost
    DocumentRoot “C:/xampp/htdocs”

    ServerName magento.locl
    DocumentRoot “C:/www/magento”

    DirectoryIndex index.php
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted

    ServerName emporio88.locl
    DocumentRoot “C:/www/emporio88”

    DirectoryIndex index.php
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted

    1. NameVirtualHost *:80
      
      <VirtualHost *:80>
      DocumentRoot "C:/xampp/htdocs"
      ServerName localhost
      </VirtualHost>
      

      that part for you is correct, but it is only done so that you have access to the Xampp htdocs folder
      If you’re defining additional sites then you need to do this

      
      <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>
      

      below the first definition for Xampp and then just repeat the second code above only for each site you are defining and update your hosts file to match

      check the article to get the exact definitions

    1. If you are only doing development on your computer …. It is not recommended to use domains .com domains but if you aren’t developing its pretty much the same except you change .dev to .com

  3. Hi. I have tried different variations of this, and they either all open the root folder, or open only the first site, regardless how many VirtualHosts I add.

    1. Hey Abraham, I think you might be forgetting to change the Virtual Hosts directory to that of your website

      You need to repeat step 6 for each site you wish to add … Remembering to point the directories to the location of the sites

      1. I have tried that as well. There is just one site that I want to add. Att the moment both localhost and that site take me to the htdocs folder 🙁
        Let me try some more

  4. It’s working in my local PC where I configured it but I cannot access the site I made in the other PC/SmartPhones.

    DNS problem

Leave a Reply