Multiple virtual hosts with XAMPP on Windows

Published:

Add / read comments

Few years ago, when I started with programming of web applications, I installed XAMPP package and was putting all stuff into the htdocs directory. Later, when I learned how to manipulate with Apache configuration file (httpd.conf), I created my another document root by editing DocumentRoot in config file. From that time, all my documents (applications) were served from that directory.

I was also playing with other Apache HTTP server configuration directives like: LoadModule, Directory, AllowOverride, DirectoryIndex etc. But after some time, I was accessing my projects from path like for example:


 http://localhost/web_page_projects_current/AJAX/guestbook/shoutbox/
  

I had been working on many projects simultaneously and had to struggle with different paths and subdirectories which were sometimes confusing. I had to pay attention to setting base URLs etc. instead of comfortable developing.

So I was searching for the way, how to setup my own local server domains (ServerName) which are having also separate document root directories (DocumentRoot), so I could sort projects files. I wished to have for example domains like:


 http://guestbook.dev/
 http://blog.dev/
  
with their own directories:

 D:\web\guestbook
 D:\web\blog
  

I have found few articles about this problem, but they weren't quite clear and understandable. I had to finally figure it out by myself. So this is tutorial how to set up things to make them work!

I am running XAMPP version 1.7.7 and let's say it is installed at: C:\xampp directory. So you have to find the Appache configuration file http-vhosts.conf, which should be located at the address like:


 C:\xampp\apache\conf\extra\
  

If you have found the file, first of all, uncomment the following line with Apache configuration directive NameVirtualHost to enable name based virtual host on your server’s port 80:


 NameVirtualHost *:80
  

Right after that you can start with adding your virtual hosts. I will create two examples mentioned above. First one accessible by entering http://guestbook.dev in your browser’s address bar.


 <VirtualHost *:80>
    DocumentRoot "D:\web\guestbook"
    ServerName guestbook.dev
    ServerAlias guestbook.dev
       
    <Directory "D:\web\guestbook">    
        DirectoryIndex index.php
    
        Options Indexes FollowSymLinks Includes ExecCGI
            
        # make possible to use .htaccess file     
        AllowOverride All   
        #None !!! will make impossible to use .htaccess file 

        # prevent other people from outside to access this server
        Order allow,deny
        Allow from 127.0.0.1
      
    </Directory>
 </VirtualHost>

and second one accessible by entering http://blog.dev in your browser’s address bar.

 <VirtualHost *:80>
    DocumentRoot "D:\web\blog"
    ServerName blog.dev
    ServerAlias blog.dev
       
    <Directory "D:\web\blog">    
        DirectoryIndex index.php
    
        Options Indexes FollowSymLinks Includes ExecCGI
            
        # make possible to use .htaccess file     
        AllowOverride All   
        #None !!! will make impossible to use .htaccess file 

        # prevent other people from outside to access this server
        Order allow,deny
        Allow from 127.0.0.1
      
    </Directory>
 </VirtualHost>

When creating your own local servers just make sure that the DocumentRoot exists and matches the Directory.

We are almost done, there is one last thing to do. It's necessary to edit windows hosts file. It's because you have to tell your system what to do if you enter http://blog.dev in your browser's bar. The way without having to deal with DNS or anything else is to edit your hosts file which should be located at:


 C:\WINDOWS\system32\drivers\etc

The windows hosts file is a simple text file which contains IP to hostname pair mappings. You can edit the file with just a text editor. You need to append a new line which maps the hostname you specified in Apache’s directive ServerName to 127.0.0.1. In my example case, file will look like this:


 127.0.0.1		localhost
 127.0.0.1		guestbook.dev
 127.0.0.1		blog.dev

And it is done! Now just restart the Apache and try to type created local server names into the browser's bar. Good practice is also firstly create and save some index file into the DocumentRoot directory which will "Say Hallo" to us :-)

For detail information you can visit official Apache Virtual Host documentation. I also should mention that if you have problems with restarting Apache server, then check the Apache configuration file for keying mistake or take a look at the error log.

Published:

Add / read comments

FIND ME

Share, follow or connect with me.