Stack trying to migrate a configuration from Linux to Windows Apache 2.2 (via WAMP)

We are trying to set an ENV Apache variable to be used as DocumentRoot and rest of directives thereafter so we can use the same set of confs on server and local working copies of developers.

In Debian Linux Apache we have an envvars file that is loaded with "EXPORT" directives. This does not work on Windows but

Setenv ROOT_TO_FILES "C:/wamp/www/test"

Seems to work as displayed by a phpinfo()

But when we use it inside a DocumentRoot directive of an vhost:

DocumentRoot ${ROOT_TO_FILES} 

Apache looks for that literal text under its own root.

This is the way we use it in Linux but we've also tried the syntax of env=ROOT_TO_FILES

The apache doc seems clear about how to "define" but not how to use. Also I see Apache 2.4 includes a new "Define" directive that seems to do exactly this which makes me think that it might not possible in previous versions.

Any samples of use under Windows Apache would be appreciated.

  • Realise this is quite old, and can't add an answer, but in case anyone else stumbles across this I ended up solving it using "Define" syntax. So I have a config file full of lines like: Define ROOT_TO_FILES "C:/wamp/www/test" and then load that config file at the beginning of my httpd.conf and reference it everywhere else using the same "DocumentRoot ${ROOT_TO_FILES}" syntax as you normally would. Works quite nicely. – Barry Pollard Jan 20 '17 at 18:59

Apache's SetEnv directive just defines a variable that PHP or Perl scripts can use, for example they show up in PHP's $_SERVER[] global array.

SetEnv, nor it's cousins, have have anything to do with use of variables in Apache directives.


DocumentRoot usually needs a literal string...

What you want to do is use mod_macro (included in Apache 2.4.5 / *before that, you can find it as an extra module to download)... http://httpd.apache.org/docs/current/mod/mod_macro.html

*For Apache 2.2, search Google for mod_macro Apache 2.2 VC9. TS/Thread-safe is for mod_php, NTS/Non-Thread-Safe is for PHP-FCGI.

|improve this answer|||||

You can use Env variables even in windows apache, e.g. I use this code to set my servername:

ServerName "${COMPUTERNAME}:80"

But in this way you can only use env variables this way, if they are defined before starting your server, and the can NOT be changed by SetEnv.

If you like to use ${ROOT_TO_FILES} to like did in Linux you could

  1. create ROOT_TO_FILES as system wide env variable
  2. start apache from commandline or batchfile

Here`s an example: (enter your path to httpd.exe if its different)

 set ROOT_TO_FILES=C:/wamp/www/test

This was tested in apache 2.2 and 2.4.

If you like you can even use variables declared in apache.con with SetEnv e.g. like this:

SetEnv MY_VHOST "example.com"
Header set X-Vhost %{MY_VHOST}e 

## this will add your own variable to this logformat
LogFormat "%h %l %{MY_VHOST}e %t \"%r\" %>s %b" common_myvhost
CustomLog log/access.log common_myvhost

But you need to know that variables set by SetEnv won`t be accessable for the "early" sections like access controll.

Take a look to the documentation http://httpd.apache.org/docs/2.4/mod/mod_env.html#setenv if you need more informations about SetEnv (or SetEnvIf)

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.