Have two (or more) Apache VirtualHosts separated by subdirectories

apache-2.4directoryvirtualhost

I think this is a simple question, but let me explain better. I have the following development environment:

  • Ubuntu Server 12.04 LTS;
  • Apache 2.4;
  • PHP 5.5;

Everything is working fine, except the VirtualHost configuration. I want to deploy two applications: Drupal and WordPress. Each application has your own directory, for example:

  • Drupal: /l/disk0/drupal/public_html;
  • WordPress: /l/disk0/wordpress/public_html;

In default site, VirtualHost configuration points to /var/www as document root. I want to disable this default-site configuration and I want to create two VirtualHosts, one for Drupal and another one for WordPress.

However, this is a development environment and there is no domain here. I've searched many sites and I found a lot of tutorials, all of them explains how to do this with domains, but not with subdirectories.

After doing the configuration, the URL to access my applications will be:

 - http://example.com/wordpress;
 - http://example.com/drupal;

I already try with this VirtualHost:

<VirtualHost *:80>
        ServerName example.com
        ServerAdmin web@myserver.com
        DocumentRoot /l/disk0/site/public_html

        <Directory />
                AllowOverride None
        </Directory>

        Alias /site /l/disk0/site/public_html
        <Directory /l/disk0/site/public_html>
                Options MultiViews Indexes Includes FollowSymLinks ExecCGI
                AllowOverride All
                Require all granted
                allow from all
        </Directory>

        RewriteEngine On
        LogLevel warn
        ErrorLog "/l/disk0/site/logs/apache/site/error.log"
        CustomLog "/l/disk0/site/logs/apache/site/access.log" combined
        SSLProxyEngine on
</VirtualHost>

Didn't work.

Best Answer

VirtualHost is used when you want to have a separate domain name for each site. When apache chooses which virtualhost to use, it looks at the domain name part of the URL. So when that part is the same, it means you cannot split it up using VirtualHosts.

You have two choices:

  • Use separate hostnames, e.g. drupal.example.com and wordpress.example.com instead of example.com/drupal and example.com/wordpress

  • Use another way to separate the two sites.

In the former case, you would keep your VirtualHost configs but you would change the ServerName in each.

In the latter case, you would remove the VirtualHost configuration and instead do something like

    Alias /drupal /l/disk0/drupal/public_html
    <Directory /l/disk0/drupal/public_html>
            Options MultiViews Indexes Includes FollowSymLinks ExecCGI
            AllowOverride All
            Require all granted
            allow from all
    </Directory>

    Alias /wordpress /l/disk0/wordpress/public_html
    <Directory /l/disk0/wordpress/public_html>
            Options MultiViews Indexes Includes FollowSymLinks ExecCGI
            AllowOverride All
            Require all granted
            allow from all
    </Directory>
Related Topic