Organising httpd.conf for Virtual Hosts

apache-2.2virtualhost

I'm trying to set up a subdomain using name-based Virtual Hosts, according to the configuration format in this question.

As I understand it, this:

Finally, you can fine-tune the
configuration of the virtual hosts by
placing other directives inside the
containers. Most
directives can be placed in these
containers and will then change the
configuration only of the relevant
virtual host. To find out if a
particular directive is allowed, check
the Context of the directive.
Configuration directives set in the
main server context (outside any
container) will be used
only if they are not overridden by the
virtual host settings.

should mean that the task of moving from a single host to 1 host + 1 subdomain should be fairly straightforward. However, when I add the VirtualHost directives, I run into problems.

First off (and pretty catastrophic) is that the home page for the main domain returns a 403 (Forbidden). Could any of the following be causing that:

  • The existing main domain has a large configuration file, with many settings. I'm placing the VirtualHost directives near the end of httpd.conf, just after the commented out instructions for virtual hosts
  • There are lots of Redirects and Rewrites in a file that's included after the VirtualHosts – could they be clashing?
  • I've put the absolute minimum in the VirtualHost directives, because I want to keep it as simple as possible. Would it be better to move the contents of the old httpd.conf into a separate file (e.g. main-domain.conf), Include that in one VirtualHost and create a separate conf file for the sub-domain, including that in the second VirtualHost directive?
  • Finally, is there any simple way of debugging this, bearing in mind I'm working with a live site, so any changes have to be tested very quickly!

Update

OK, the error is, more specifically:

Directory index forbidden by rule

Other pages appear to work OK, so it must be some kind of problem with a redirect from the homepage. I guess this was partly why I asked if there are known clashes between VirtualHost setups and Redirects / Rewrites.

Should a <Directory "/path/to/docroot"> block that matches the DocumentRoot in a VirtualHost block be before or after the latter (or doesn't it matter)?

Update 2

OK, so it looks like what's not happening is the following redirect:

RewriteRule ^/$ /path/to/something/else [PT]

which I'm guessing is happening for one of two reasons:

  1. The current config isn't even attempting to process this rewriterule. The rule itself is in an Included file, after the VirtualHost blocks.

  2. The rule is no longer matching because the VirtualHost directive is causing the pattern match to fail. This seems, to me, by far the most likely …

Update 3

Anyone think this might be relevant?

Note that, by default, rewrite configurations are not inherited. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use it.

In other words, does that mean I need to:

<VirtualHost *:80>
    RewriteEngine on
</VirtualHost>

for each VH that needs to handle Rewrite* stuff, rather than just relying on RewriteEngine on in the globally-included file?

Update 4

Sadly, the change referred to in Update 3 hasn't fixed the problem at all

Best Answer

I always split my vhosts in to seperate directories files. In httpd.conf have a line:

Include /etc/httpd/sites-enabled/*.conf

Then create two directories:

mkdir -p /etc/httpd/sites-{enabled,available}

Then you can create as many vhosts as you need, one per file in:

/etc/httpd/sites-available/site1.conf /etc/httpd/sites-available/site2.conf

Now you can turn sites on and off with a symlink in the sites-enabled directory:

ln -s /etc/httpd/sites-available/sites2.conf /etc/httpd/sites-enabled/

You can test your config with:

apachectl configtest

Which should at least tell you if you're syntactically correct.

Andrew