How To Create Xampp Virtual Host Windows 7 & 8

Discussion in 'Windows Know Base' started by securityhope, Aug 16, 2016.

  1. securityhope

    securityhope Administrator Staff Member

    Joined:
    Aug 3, 2016
    Messages:
    1,241
    Likes Received:
    0
    Trophy Points:
    36
    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://localhostas 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.

    People search:
    1. how to create virtual host in xampp
    2. xampp virtual host
    3. virtual host xampp
    4. virtual host in xampp
    5. create virtual host in xampp
    6. xampp add virtual host
    7. xampp create virtual host
    8. xampp multiple virtual hosts

    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/extrafolder
    5. At the very bottom of the file paste the following
    Code:
    NameVirtualHost *:80
    Code:
    <VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
    </VirtualHost>
    With out that line of code you will lose access to your default htdocsdirectory. IE. http://localhost/ will be inaccessible.

    1. Now copy and paste the code below .. below the first code
    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

    Code:
    <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.
    Code:
    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
    Creating virtual hosts in XAMPP



    Setting up a Virtual Host with XAMPP

    What XAMPP is a cross-platform, open-source, web-server package that contains Apache, MySQL, PHP and Perl . Anyone interested can download a copy from the developers, Apache Friends. I use XAMPP every day that I work to create a live copy of a website for my own development. If I break anything locally, the only code broken is mine. This allows developers to work simultaneously on multiple tasks without the interference of a site going down due to a bug introduced by someone else.

    Configuring XAMPP for the First Time

    To begin, download your copy of XAMPP from Apache Friends. A copy can be found at http://www.apachefriends.org/en/xampp.html. Once you have your copy, install it. I installed mine under its default directory of “C:xampp”. Once installed, we may move on to configuring the Apache server to run your site locally. To do this, locate your “hosts” file. In Windows 7, the file is located by following this route: “C:WindowsSystem32driversetc”. Open that file with your text editor of choice and you will find something like the following:

    Code:
    # Copyright (c) 1993-2009 Microsoft Corp.
    #
    # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
    #
    # This file contains the mappings of IP addresses to host names. Each
    # entry should be kept on an individual line. The IP address should
    # be placed in the first column followed by the corresponding host name.
    # The IP address and the host name should be separated by at least one
    # space.
    #
    # Additionally, comments (such as these) may be inserted on individual
    # lines or following the machine name denoted by a '#' symbol.
    #
    # For example:
    #
    #      102.54.94.97     rhino.acme.com          # source server
    #       38.25.63.10     x.acme.com              # x client host
    
    # localhost name resolution is handled within DNS itself.
       127.0.0.1       localhost
    #    ::1             localhost
    
    This file outlines how to set everything up, but to create a localhost, simple re-type your own IP (in my case, 127.0.0.1) followed by the name of your DNS. After adding a site for TheIntern, the file would look as follows:

    Code:
    # Copyright (c) 1993-2009 Microsoft Corp.
    #
    # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
    #
    # This file contains the mappings of IP addresses to host names. Each
    # entry should be kept on an individual line. The IP address should
    # be placed in the first column followed by the corresponding host name.
    # The IP address and the host name should be separated by at least one
    # space.
    #
    # Additionally, comments (such as these) may be inserted on individual
    # lines or following the machine name denoted by a '#' symbol.
    #
    # For example:
    #
    #      102.54.94.97     rhino.acme.com          # source server
    #       38.25.63.10     x.acme.com              # x client host
    
    # localhost name resolution is handled within DNS itself.
       127.0.0.1       localhost
    #    ::1             localhost
       127.0.0.1    TheIntern.localhost
    Note that this file is tab sensitive and you will need to write your code to match the example.

    After we have saved our changes to the hosts file, we can move on the editing Apache’s virtual hosts. This can be a little less streamline and where I ran into my own difficulties. To locate your vhosts file, navigate to your XAMPP file and from there, Apache > conf > extra. Within this file, look for “httpd-vhosts.conf” and open with a text editor. You will find:

    Code:
    #
    # Virtual Hosts
    #
    # If you want to maintain multiple domains/hostnames on your
    # machine you can setup VirtualHost containers for them. Most configurations
    # use only name-based virtual hosts so the server doesn't need to worry about
    # IP addresses. This is indicated by the asterisks in the directives below.
    #
    # Please see the documentation at
    #
    # for further details before you try to setup virtual hosts.
    #
    # You may use the command line option '-S' to verify your virtual host
    # configuration.
    
    #
    # Use name-based virtual hosting.
    #
    NameVirtualHost *:80
    
    #
    # VirtualHost example:
    # Almost any Apache directive may go into a VirtualHost container.
    # The first VirtualHost section is used for all requests that do not
    # match a ServerName or ServerAlias in any  block.
    #
    ##
        ##ServerAdmin [email protected]
        ##DocumentRoot "C:/xampp/htdocs/dummy-host.localhost"
        ##ServerName dummy-host.localhost
        ##ServerAlias www.dummy-host.localhost
        ##ErrorLog "logs/dummy-host.localhost-error.log"
        ##CustomLog "logs/dummy-host.localhost-access.log" combined
    ##
    
    ##
        ##ServerAdmin [email protected]
        ##DocumentRoot "C:/xampp/htdocs/dummy-host2.localhost"
        ##ServerName dummy-host2.localhost
        ##ServerAlias www.dummy-host2.localhost
        ##ErrorLog "logs/dummy-host2.localhost-error.log"
        ##CustomLog "logs/dummy-host2.localhost-access.log" combined
    ##
    
    This will need to be edited to follow to match when we enter our own site. For our pursoses, we will need to add a directory, as well as a local host. In the short of it, our end file will have this added:

    Order Deny,Allow
    Allow from all
    AllowOverride all
    Options all

    ServerAdmin [email protected]
    DocumentRoot "C:NuRelmTheIntern"
    ServerName TheIntern.localhost
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common

    Notice the Directory and Document Root are the same address. They need to be, and their specific address would be the file containing the main index of your site. Next, the ServerName will need to be the name listed as the DNS in the hosts file, in our case “TheIntern.localhost”. Once this is completed, you should be able to start Apache and run your site. Once you start Apache, open your browser of choice and type as the address what you have in your “hosts” file above,“TheIntern.localhost”, and everything should work as planned.
     
    Last edited: Aug 16, 2016

Share This Page

Share