Question

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>
Was it helpful?

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.

OTHER TIPS

Since you have only a single domain name and only a single ip address available there is no means for the apache server to distinguish which host is meant. Therefore there is noo sense in defining VirtualHosts here.

However you certainly can place two apps in separate folders inside your DocumentRoot:

ServerName whatever-your-domain.is

DocumentRoot /var/www

<Directory /var/www/app1>
    Order allow,deny
    Allow from all
</Directory>

<Directory /var/www/app2>
    Order allow,deny
    Allow from all
</Directory>

Then you'd call those apps by their paths:

Don't forget to take care of requests to the "main folder" of that single host: /var/www which can be reached by http://whatever-your-domain.is/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top