78

I want to define a variable in Apache server's httpd.conf configuration file.

Ex: variable static_path = C:\codebase\snp_static

and I want to use this variable (static_path) in httpd.conf where ever required.

Please tell me how can define a variable in httpd.conf file ?

3

5 Answers 5

155

Within httpd.conf, declare your variable(s) with: Define (Preferably at the very first line)
Syntax: Define variable-name variable-value

In this manner:

#The line below creates the variable [static_path] Define static_path C:/codebase/snp_static 

You can later use this variable like so:

ServerRoot = ${static_path} ... DocumentRoot = ${static_path} ... <Directory ${static_path}> ...etc. 

You can even combine multiple variables:

#Below, I am going to combine variables [server_space] and [static_path] Define server_space c:/ Define static_path codebase/snp_static ... ServerRoot = ${server_space}${static_path} ... DocumentRoot = ${server_space}${static_path} ... <Directory ${server_space}${static_path}> ...etc. 

Documentation: http://httpd.apache.org/docs/2.4/mod/core.html#define

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

17 Comments

mod_define not included in Apache 2.2 by default
Old original (made compatible with 2.0 and 2.2) mod_define is at people.apache.org/~rjung/mod_define/mod_define.html
This is definitely the ideal way to define variables in httpd conf files. I do hope the apachectl people stumble across this question.
Where can i find mod_define for windows lib apache 2.2 ?
@Radon8472 I may be mistaken, but I believe you can use the [define] "command" (or whatever the proper name may be) without mod_define on 2.2. After all, the documentation provided also works on 2.4 (and upwards). But just in case I am really, really wrong: people.apache.org/~rjung/mod_define/mod_define.html Have you tried it without modifications on your server?
|
11

If all you want is simple variable substitution inside httpd.conf, then define an ordinary shell environment variable for the user that runs Apache, then use the ${ENVVAR} syntax to refer to it inside your httpd.conf file, see Apache docs

Comments

5

Apache2.4 I researched it out and here is what worked for me. and tested using httpd_z.exe -t -D DUMP_RUN_CFG

 RESULTS::: ServerRoot: "C:/path/core/apache2" Main DocumentRoot: "C:/path/apache/htdocs" Main ErrorLog: "C:/path/core/apache2/logs/error.log" Mutex rewrite-map: using_defaults Mutex default: dir="C:/path/core/apache2/logs/" mechanism=default PidFile: "C:/path/core/apache2/logs/httpd.pid" Define: DUMP_RUN_CFG Define: US_ROOTF=C:/path **THIS IS THE ROOT PATH VARIABLE I JUST MADE** Define: php54 #<IfDefine TEST> # Define servername test.example.com #</IfDefine> #<IfDefine !TEST> # Define servername www.example.com # Define SSL #</IfDefine> #DocumentRoot /var/www/${servername}/htdocs <IfDefine US_ROOTF> Define US_ROOTF C:/PATH **The path i want the variable for** </IfDefine> <IfDefine !US_ROOTF> Define US_ROOTF C:/PATH **The path i want the variable for** # Define SSL </IfDefine> #DocumentRoot /var/www/${servername}/htdocs OPTIONS ON HOW TO USE EXAMPLE of use ServerRoot = ${US_ROOTF} <IfDefine php54> LoadFile "${US_ROOTF}/core/php54/icudt53.dll" PHPIniDir "${US_ROOTF}/core/php54/php_production.ini" 

I was told never to use a direct HARD path to anything when serving something to the internet always use variables to help secure your system.

I found the hard way this is so true. Now I finally figured out how to set the variables for all services dealing with Apache i use them.

Hope it helps you too.

1 Comment

From the management perspective, it makes it all much easier to use variables to update and maintain a server when you need to removing old paths, rather than looking for these direct paths on each line of the http.conf (and any other places that apply) as well as security (when it applies)
4

Late to the question but recently had this issue and fixed it like so:

DEFINE path "C:\path/to the/directory"

Then later use like so:

DocumentRoot ${path} <Directory ${path}>

Note: In the path use \ after the drive letter

Comments

1

If your apache project is not taking system's environmental variables which we added to bashrc, We can directly EXPORT variables to /etc/apache2/envvars file

example: export ADMIN='Bibin'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.