3

So people complained about how PHP Scripts using DOCUMENT_ROOT break when Apache is being run with VirtualDocumentRoot and mod_vhost_alias since DOCUMENT_ROOT doesn't get set properly. Apache evidentally fixed this sometime ago and the brief notes are here: http://svn.apache.org/viewvc?view=revision&revision=1132494

Sadly, there isn't enough detail explaining how to actually override the DocumentRoot on a per-request basis. I'm guessing it must be done with mod_rewrite somehow but I'm not certain. Does anyone know how to do this?

JamesHoux
  • 2,999
  • 3
  • 32
  • 50
  • I will add that an in-depth discussion of the issue occurred here: https://issues.apache.org/bugzilla/show_bug.cgi?id=26052 – JamesHoux Dec 18 '12 at 12:31

2 Answers2

3

Well, evidentally, I wasn't quite asking the right question. Here's some information I got directly from Stephan on the Apache2 Bugzilla list:

Stephan said:

The bug report was about VirtualDocumentRoot and this now sets DOCUMENT_ROOT correctly. Therefore the issue is resolved.

And I pushed back to clarify:

Maybe I missed something but I'm pretty certain I tested $_SERVER['DOCUMENT_ROOT'] in php and it wasn't matching the value set by VirtualDocumentRoot. You're saying it should match now?

And he replied:

Yes, in 2.4.x. It is very unlikely that the fix will ever be ported to 2.2.x because it requires infrastructure that is only present in 2.4.

So I ran apache2 -v and discovered I'm not running 2.4.x. Apache2 -v shows this:

Server version: Apache/2.2.16 (Debian)
Server built:   Nov 30 2012 08:33:45

And if you notice the Server was built at the end of November this year when I used aptitude -install apache2. I don't know if apt-get would have pulled apache 2.4 instead. This is very lame. 2.4.3 stable was released in August. I don't know why aptitude pulled 2.2 but this really created a nightmare for me.

So in reality, the question I asked was moot. Everything should have worked fine, except that aptitude installed an outdated apache2 and I didn't realize what was going on. There is no bug anymore.

JamesHoux
  • 2,999
  • 3
  • 32
  • 50
1

mod_vhost_alias is the only module in the official distribution that sets the per-request document root.

Pre-existing code, including the code that sets the DOCUMENT_ROOT CGI environment variable, that looks up the document root via ap_document_root(r) sees these changes.

This change also introduce a new concept for modules like mod_userdir, where "document root" is not really appropriate. This new concept has new API's and new CGI environment variables (CONTEXT_PREFIX, CONTEXT_DOCUMENT_ROOT) that are not tied into the traditional document root.

The only real doc I see as being pertinent is:

  • doc for developers (basically done)
  • doc for folks migrating who see DOCUMENT_ROOT set by mod_vhost_alias
  • CGI/environment variable doc about what the new CONTEXT_* variables are

But, I don't see these as todos as leaving things "broke" since mod_vhost_alias and the existing DocumentRoot API's and environment variables are working together.

If the question really is "how" outside of mod_vhost_alias, look at the difference between mod_userdir and mod_vhost_alias with respect to ap_set_document_root() and ap_set_context_info() and then look at how these result in CGI environment variables in server/util_script.c.

covener
  • 17,402
  • 2
  • 31
  • 45
  • Thanks covener. Well I got some answers about this on the actual Apache bug list. One of the respondants mentioned the same thing you said about ap_document_root() being changed. He also claimed that DOCUMENT_ROOT is now set correctly by VirtualDocumentRoot. In other words, he's claiming that the problem is completely fixed. Its possible that I did something wrong, but when I tested DOCUMENT_ROOT in PHP, I'm pretty it was not matching the value assigned to VirtualDocumentRoot. If I used DocumentRoot, then the value was correct. I'll have to revisit and test again I guess... – JamesHoux Dec 27 '12 at 07:22
  • I'm marking your answer as the accepted answer even though it didn't fix my problem. (See my own answer for what ended up being the fix to the problem) You answered the question as completely as you could and it provides helpful relevant information. Thanks, covener. – JamesHoux Dec 27 '12 at 22:13