Web-server – Hosting two web applications on same server

apache-2.2httpdhttpd.confweb-applicationsweb-server

Without giving all the details, I'm trying to set up (via apache) two web applications that will be served from the same (internal development) server. Currently I have two VirtualHost directives and the two applications running on different ports. Instead, I'd like to have the URL determine which application is used, so, for example, myapp.domain.com will forward to one application and any other .domain.com will go to the other. Setting up the internal DNS to take care of that piece isn't a problem. But I'm not very familiar with apache, what's the best way to accomplish this?

Best Answer

Within the <VirtualHost> you can use a ServerName; this is the directive to tell Apache to handle requests for that particular name. You can also use ServerAlias to specify other domains (and more besides!) that you'd like the VirtualHost to handle requests for.

For example, you can set up your directives like this

<VirtualHost ...>
    ServerName foo.example.com
    ...
    #Handle webapp1 in here
    ...
</VirtualHost>

<VirtualHost ....>
    ServerName example.com
    ServerAlias *.example.com
    ...
    #Handle webapp2 in here
    ...
</VirtualHost>

The Apache documentation contains several very good examples for most use cases in their documentation (http://httpd.apache.org/docs/2.0/vhosts/examples.html). You may also be interested in their "default" catchall halfway down the page, which is basically a catchall for any virtual host requests that aren't matched by previous directives.

Related Topic