Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

http://httpd.apache.org/docs/current/mod/mod_env.html#setenv

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.

share|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
 %ProgramFiles%\xampp\apache\bin\httpd

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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