Search code examples
apacheurivirtualhostdomain-nameapache2.2

Two apps on Apache server with URI


I need to install a server with Apache 2.2 on Linux and I need to do two VirtualHosts differentiated by URI.

But with only one domain name and one ip address. And I can't use Alias.

I tried something like that but that doesn't work :

<VirtualHost *:80>
    DocumentRoot /var/www/app1
    ServerName localhost/app1
    ServerAlias www.localhost/app1

    <Directory /var/www/app1>
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/app2
    ServerName localhost/app2
    ServerAlias www.localhost/app2

    <Directory /var/www/app2>
        Allow from all
    </Directory>
</VirtualHost>

Solution

  • Thank you to the first answer here, it's working : https://serverfault.com/questions/588841/two-apps-on-apache-server-with-uri

    I put the answer here if one day the link doesn't work :

    What you could do is set up a reverse proxy to different virtual hosts listening only on loopback.

    You would get in your www.localhost virtualhost:

    <VirtualHost *:80>
        DocumentRoot /var/www/
        ServerName localhost
        ServerAlias www.localhost
    
        ProxyPassReverse /app1/ http://webapp1.local/
        ProxyPassReverse /app2/ http://webapp2.local/
    </Virtualhost>
    

    And create two virtualhosts for the apps:

    <VirtualHost 127.0.0.1:80>
        DocumentRoot /var/www/app1
        ServerName webapp1.local
    
        <Directory /var/www/app1>
            Allow from all
        </Directory>
    </Virtualhost>
    
    <VirtualHost 127.0.0.1:80>
        DocumentRoot /var/www/app2
        ServerName webapp2.local
    
        <Directory /var/www/app2>
            Allow from all
        </Directory>
    </Virtualhost>
    

    Make sure to add webapp1.local and webapp2.local to your /etc/hosts file.