Home > front end >  XAMPP with virtualhost is serving the wrong documentroot
XAMPP with virtualhost is serving the wrong documentroot

Time:11-24

I'm running xampp on Linux and want to set up virtual hosts, so I can quickly jump between projects.

I have two projects set up like this:

/home/(user)/webdev/app1 which contains an index.html

/home/(user)/webdev/app2 which contains an index.html

My httpd.conf includes these snippets:

<Directory "/home/(user)/webdev/app1">
    Options Indexes FollowSymLinks ExecCGI Includes
    AllowOverride All
    Require all granted
</Directory>

<Directory "/home/(user)/webdev/app2">
    Options Indexes FollowSymLinks ExecCGI Includes
    AllowOverride All
    Require all granted
</Directory>

and Include etc/extra/httpd-vhosts.conf is uncommented.

My httpd-vhosts.conf includes this:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/home/(user)/webdev/app1"
    ServerName webdev.app1
    ErrorLog "logs/app1.example.com-error_log"
    CustomLog "logs/app1.example.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/home/(user)/webdev/app2"
    ServerName webdev.app2
    ErrorLog "logs/app2.example.com-error_log"
    CustomLog "logs/app2.example.com-access_log" common
</VirtualHost>

And my /etc/hosts file looks like this:

127.0.0.1   localhost
::1     localhost

127.0.0.1   webdev.app1
127.0.0.1   webdev.app2

To my understanding, I did everything right. Unfortunately, when I go to webev.app1 or webdev.app2, it serves the regular htdocs folder instead of the modified DocumentRoot. When I go to localhost, it serves whatever is named first in httpd-vhost.conf, in this case /home/(user)/webdev/app1.

The behavior I expect would be that app1 is served when I visit webdev.app1 and app2 is served when I visit webdev.app2. What am I missing?

CodePudding user response:

Your virtual hosts are set up incorrectly, you are telling Apache that if any request comes in on port 80 serve app1 and at the same time if any request comes in on port 80 serve app2. Fix it using one of the examples below:

----------START DIFFERENTIATE VIA DIFFERENT DOMAINS----------------
 
    Listen 80
    
    <VirtualHost *:80>
        DocumentRoot "/www/app1"
        ServerName www.app1.com
        ServerAlias *.app1.com
    
        # Other directives here
    </VirtualHost>
    
    <VirtualHost *:80>
        DocumentRoot "/www/app2"
        ServerName www.app2.com
        ServerAlias *.app2.com
    
        # Other directives here
    </VirtualHost>

----------END DIFFERENTIATE VIA DIFFERENT DOMAINS----------------


----------START DIFFERENTIATE VIA SUB FOLDERS----------------

    Listen 80

    <VirtualHost *:80>
        DocumentRoot "/www/apps"
        
    
        # in the 'apps' folder place subfolders 'app1' and 'app2'
        # navigate in the browser to each app i.e. 127.0.0.1/apps/app1 and 127.0.0.1/apps/app2
        # Other directives here
    </VirtualHost>

----------END DIFFERENTIATE VIA SUB FOLDERS----------------



----------START DIFFERENTIATE VIA DIFFERENT PORTS----------------

    Listen 81
    Listen 82
    
    <VirtualHost *:81>
        DocumentRoot "/www/app1"
    
        # Other directives here
    </VirtualHost>
    
    <VirtualHost *:82>
        DocumentRoot "/www/app2"
    
        # Other directives here
    </VirtualHost>
    

----------END DIFFERENTIATE VIA DIFFERENT PORTS----------------
  • Related