Can I use the document root resulting from a VirtualDocumentRoot as a variable in other places in the same VirtualHost?
Take the following VirtualDocumentRoot for example:
VirtualDocumentRoot "/var/workspaces/%1"
The resulting document root from the hostname test.localhost
is /var/workspaces/test
. Which is also visible as the document root in the $_SERVER['DOCUMENT_ROOT']
in php.
So can I use the actual document root in a PutEnv or Define directive in the apache config? Is there a variable in the apache config with the actual document root?
edit:
I want to use this for a few different use cases.
I want to set a logging directory for all my applications, so I want to do something like this:
SetEnv LOG_DIR ${DOCUMENT_ROOT}/logs/
Or I want to point the php-fpm applications in the right direction:
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9764${DOCUMENT_ROOT}/$1
Or set some different directives on a sub directory in the document root:
<Directory "${DOCUMENT_ROOT}/bin">
Best Answer
First, I'd like to point out that the desired state of your use cases might have certain security issues.
Why would you like to store logs inside
DocumentRoot
? This increases the risk of exposing logs to public. Of course you can prevent this with a new<Directory>
directive, but it is easier to keep the logs on different directory. It's also more manageable in the perspective of directory permissions, considering Log Files Security Warning:On the other hand, you cannot have separated log file for each virtual host if using mod_vhost_alias. While the motivation for using Dynamically Configured Mass Virtual Hosting is having only one "wildcard"
VirtualHost
container for allcustomer-*.example.com
,It seems that in this case the setup needs to meet these requirements:
VirtualHost
containers.Thus, considering all the limitations with mod_vhost_alias, I'd suggest abandoning it and using mod_macro instead, as it would meet the requirements and it's much more flexible.
An example macro with all the examples from your question (even the not recommended
LOG_DIR
):Now, you can add all the workspaces with:
Unlike with mod_vhost_alias you can still have a custom domain for every workspace. Just modify
<Macro Workspace $workspace $hostname>
andServerName $hostname
and you can use the macro withUse Workspace testwithdomain example.com
.