Ubuntu – Is it possible to setup multiple Virtual Hosts with one ip/domain

Apache2dnsservervirtualhost

Is it possible to setup multiple Virtual Hosts with one ip/domain?

We have a single domain (example.com) pointing to our server at ip (123.123.555.555). At location 123.123.555.555 we require the following folder structure:

# production level
/htdocs/www/prod/proj1/
/htdocs/www/prod/proj2/

# dev level
/htdocs/www/dev/proj3/
/htdocs/www/dev/proj4

Where ideally we would like example.com to resolve to /htdocs/www/prod/ and example.com/dev to resolve to /htdocs/www/dev/.

Simple enough no?

Caveats:

  1. dev and prod need different setup directory/apache rules (allow deny from, etc)
  2. prod needs to resolve without prod in the url. example.com/proj1 would goto example.com/prod/proj1

Attempts so far..

I've tried setting up with the same virtual host, but giving a directory an alias of / sends it into a fit, claiming that it will be overwritten later. It makes sense because / matches / but also /dev

I've also tried setting up multiple virtual hosts with apache2' sites-available/sites-enabled but it seems I can't distinguish it based on the ip/domain. Normally I would attempt something like <Directory 'sub.domain.com'> and <Directory 'sub2.domain.com'>

Best Answer

You have at least two options:

  1. Setup a virtualhost for a subdomain of example.com and direct it to your dev folder. Here's an example of a .conf file to put in sites-available:

    <VirtualHost *:80>
    DocumentRoot "/htdocs/www/dev/proj3/"
    ServerName dev.example.com
    <Directory "/htdocs/www/dev/proj3/">
    allow from all
    Options +Indexes
    </Directory>
    </VirtualHost>
    
  2. Setup virtualhost for a different port, i.e. 8080 or whatever you choose, and point this to the dev folder.

    <VirtualHost *:8080>
    DocumentRoot "/htdocs/www/dev/proj3/"
    ServerName example.com
    <Directory "/htdocs/www/dev/proj3/">
    allow from all
    Options +Indexes
    </Directory>
    </VirtualHost>