3

Currently I have an Ubuntu 12.04 webserver running apache2. I have it setup to dynamically create subdomains by creating new folders under /sites/example.com/*/public /sites/example.com/www/public is reserved for my main root site.

This is working out, however I am unable to configure PHP's $_SERVER['DOCUMENT_ROOT'] to be dynamic to the newly created folder.

When I echo $_SERVER['DOCUMENT_ROOT'] I get /etc/apache2/htdocs which I assume is some sort of default path. I would like this to be: /sites/example.com/*/public instead

# Wildcards
<VirtualHost *:80>
    VirtualDocumentRoot /sites/example.com/%1/public
    ServerAdmin [email protected]
    ServerAlias *.example.com
</VirtualHost>

Curious why PHP doesn't pick up the the virtual document root setting above, I'm likely doing something wrong.

2
  • Are you confident that files are being served out of the proper DocumentRoot at /sites/example.com/*/public? What is the path of the PHP file you're testing with, and what Host are you using to obtain it? Commented Jul 29, 2014 at 14:09
  • 1
    Yea, the rest of the php stuff is executing as I expected. It seems that this variable can vary per host. This seems to do the trick, but I don't know if its the best solution (joshbenner.me/blog/…) Commented Jul 29, 2014 at 14:12

2 Answers 2

7

Solution found at : http://joshbenner.me/blog/quick-tip-get-proper-document-root-when-using-mod-vhost-alias/

The Apache module mod_vhost_alias and its VirtualDocumentRoot directive can really be a great time saver for local development (some googling will explain why in more deapth). Basically, my local dev is set up so that I just have to create a directory in my aliases directory, and I just then navigate my browser to a URL matching the name of that new directory, and apache knows exactly what to serve automagically.

However, there are a few evil gotchas when using mod_vhost_alias, one of which is that the PHP global $_SERVER['DOCUMENT_ROOT'] remains set to the apache default DOCUMENT_ROOT environment variable rather than being re-assigned to the document root activated by the VirtualDocumentRoot directive for the current URL. This can cause some PHP applications (that are too trusting) to die for one reason or another.

I found a great solution to this in the related apache bug report: Simply add the following line to your apache configuration inside the VirtualDocumentRoot vhost definition:

php_admin_value auto_prepend_file /path/setdocroot.php

Then, create the referenced PHP file, and put set this as its contents:

<?php $_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']); ?>

Now, every page load has this file executed, which properly sets DOCUMENT_ROOT.

Sign up to request clarification or add additional context in comments.

1 Comment

I did this and set php_admin_value auto_prepend_file /sites/example.com/_setdocroot.php It does what I need it to.
1

This has apparently (finally) been fixed in Apache 2.4. Unfortunately, Ubuntu 12.04 still runs Apache 2.2.

Having said that, Ubuntu 14.04 (the next LTS release) is out now and do-release-upgrade should now be enabled for 12.04 to 14.04 upgrades since 14.04 had its first point release last week.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.