how to create virtual host on XAMPP

270,591

Solution 1

I see two errors:

<VirtualHost *:80> -> Fix to :8081, your POrt the server runs on
    ServerName comm-app.local
    DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
    SetEnv APPLICATION_ENV "development"
    <Directory "C:/xampp/htdocs/CommunicationApp/public" -> This is probably why it crashes, missing >
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
 -> MIssing close container: </VirtualHost> 

Fixed version:

<VirtualHost *:8081>
    ServerName comm-app.local
    DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
    SetEnv APPLICATION_ENV "development"
    <Directory "C:/xampp/htdocs/CommunicationApp/public">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

One thing to mention:

You can always try and run command:

service apache2 configtest

This will tell you when you got a malformed configuration and maybe even can tell you where the problem is.

Furthermore it helps avoid unavailability in a LIVE system:

service apache2 restart

will shutdown and then fail to start, this configtest you know beforehand "oops I did something wrong, I should fix this first" but the apache itself is still running with old configuration. :)

Solution 2

Step 1) C:\WINDOWS\system32\drivers\etc\ Open the "hosts" file :

127.0.0.1       localhost
127.0.0.1       test.com
127.0.0.1       example.com

Step 2) xampp\apache\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot C:/xampp/htdocs/test/
    ServerName www.test.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot C:/xampp/htdocs/example/
    ServerName www.example.com
</VirtualHost>

Step 3) C:\xampp\apache\conf\httpd.conf. Scroll down to the Supplemental configuration section at the end, and locate the following section (around line 500), Remove the # from the beginning of the second line so the section now looks like this:

#Virtual hosts
Include conf/extra/httpd-vhosts.conf

Step 4) Restart XAMPP and now run in your browser :

www.example.com or www.test.com

Solution 3

Add this Code in C:\xampp\apache\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName qa-staging.com
ServerAlias www.qa-staging.com
<Directory "c:/xampp/htdocs">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Now Add your virtual host name in bellow file.

C:\Windows\System32\drivers\etc\hosts

127.0.0.1 qa-staging.com

If you are not able to save this code in host file then right click on notpad select Run as administrator and then you can able to save your custom code now restart your XAMP

Solution 4

Write these codes end of the C:\xampp\apache\conf\extra\httpd-vhosts.conf file,

DocumentRoot "D:/xampp/htdocs/foldername"
ServerName www.siteurl.com
ServerAlias www.siteurl.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common

between the virtual host tag.

and edit the file System32/Drivers/etc/hosts use notepad as administrator

add bottom of the file

127.0.0.1    www.siteurl.com

Solution 5

<VirtualHost *:80>
    DocumentRoot "D:/projects/yourdirectry name"
    ServerName local.yourdomain.com
    <Directory "D:/projects/yourdirectry name">
        Require all granted 
    </Directory>
</VirtualHost>

Save the Apache configuration file.

for detailed info refer to this

Share:
270,591
Paras Arora
Author by

Paras Arora

I'm here as a lifelong learner, I thank all the members the StackExchange's community for the great help that comes to me and I try in my turn to give my little help as I can. -about me at Magento Developer

Updated on June 18, 2021

Comments

  • Paras Arora
    Paras Arora almost 3 years

    I am sure this question is being asked many times but I am not encounter with a problem. I am using XAMPP where I configure Zend framework.

    XAMPP is running on port 8081 as 80 is being occupied by some Windows process I need to use virtual host for that I configure with following code in C:/xampp/apache/config/extra/httpd-vhosts.config (or C:/xampp/apache/conf/extra/httpd-vhosts.conf in newer releases).

    <VirtualHost *:80>
    ServerName comm-app.local
    DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
    SetEnv APPLICATION_ENV "development"
        <Directory "C:/xampp/htdocs/CommunicationApp/public"
            DirectoryIndex index.php
            AllowOverride All
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>    
    

    and also update the hosts file with 127.0.0.1 comm-app.local and try to re-start apache but it is showing error.

    15:03:01  [Apache]  Error: Apache shutdown unexpectedly.
    15:03:01  [Apache]  This may be due to a blocked port, missing dependencies, 
    15:03:01  [Apache]  improper privileges, a crash, or a shutdown by another method.
    15:03:01  [Apache]  Press the Logs button to view error logs and check
    15:03:01  [Apache]  the Windows Event Viewer for more clues
    15:03:01  [Apache]  If you need more help, copy and post this
    15:03:01  [Apache]  entire log window on the forums
    
  • Steini
    Steini over 9 years
    That is a misstake yes, but this would not prevent the apache service from starting, this would merely throw a warning and the vhost would not work
  • Paras Arora
    Paras Arora over 9 years
    Apache start running after these correction but comm-app.local is still nor found on server showing Not Found HTTP Error 404. The requested resource is not found.
  • Paras Arora
    Paras Arora over 9 years
    Apache start running after these correction but comm-app.local is still nor found on server showing Not Found HTTP Error 404. The requested resource is not found.
  • Steini
    Steini over 9 years
    Have you set-up comm-app.local to redirect to 127.0.0.1 in your hosts file? And are you sure that your path is correct? Oh and ofcours you have to close the VirtualHost container if you didnt... (Updated my post)
  • Paras Arora
    Paras Arora over 9 years
    yes I just places 127.0.0.1 comm-app.local in hosts file and even restart server
  • Steini
    Steini over 9 years
    And you entered comm-app.local:8081 in browser? (Forgot port maybe)? Rmember to restart your apache after changing config. However if this still wont work something else is wrong in your config-file...
  • karim_fci
    karim_fci almost 9 years
    Hi! Steini. I am experiencing same problem. Is there any way to avoid typing port no after virtual host name. Thanks in advance
  • Jason Krs
    Jason Krs about 6 years
    Hey amit... Mine is surprisingly not working.... The only differences in my settings are : 127.0.0.4 mycustomdomain and it runs on port 90 so I used <VirtualHost 127.0.0.4:90> . It fails when I try in the browser http://mycustomdomain
  • Jason Krs
    Jason Krs about 6 years
    Hey amit... Mine is surprisingly not working.... The only differences in my settings are : 127.0.0.4 mycustomdomain and it runs on port 90 so I used <VirtualHost 127.0.0.4:90> . It fails when I try in the browser http://mycustomdomain
  • Steini
    Steini almost 6 years
    up "mycustomdomain" must also be redirected to 127.0.0.4. this only happens when its set-up in your hosts file.
  • Moeez
    Moeez about 2 years
    Getting 404 error
  • Moeez
    Moeez about 2 years
    getting 404 error on browser
  • Moeez
    Moeez about 2 years
    @ParasArora did you find the solution?
  • Moeez
    Moeez about 2 years
    tried with you solution but getting 404 error
  • Moeez
    Moeez about 2 years
    I am getting 404 error
  • Moeez
    Moeez about 2 years
    @JasonKrs Did you find any solution for it ?