74

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 ?

Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
Satya
  • 8,146
  • 9
  • 38
  • 43
  • 1
    check that response for example: http://stackoverflow.com/questions/6343621/conditions-in-apache/6346407#6346407 – regilero Jul 04 '11 at 18:59
  • should / could this question be moved to serverfault? https://serverfault.com/help/on-topic – Sybille Peters Feb 22 '19 at 09:26
  • Also see http://httpd.apache.org/docs/trunk/mod/mod_macro.html - this can often be a better solution to the problem and is included in the default distro since 2.4.5 – dualed Mar 05 '19 at 11:12

5 Answers5

146

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

Omar
  • 11,783
  • 21
  • 84
  • 114
  • 11
    mod_define not included in Apache 2.2 by default – craigrs84 Jun 04 '14 at 16:36
  • 1
    Old original (made compatible with 2.0 and 2.2) mod_define is at http://people.apache.org/~rjung/mod_define/mod_define.html – reallynice Sep 19 '14 at 12:24
  • 2
    This is definitely the ideal way to define variables in httpd conf files. I do hope the apachectl people stumble across this question. – Spencer Williams Sep 07 '15 at 03:18
  • Where can i find mod_define for windows lib apache 2.2 ? – Radon8472 May 24 '17 at 15:10
  • @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: https://people.apache.org/~rjung/mod_define/mod_define.html Have you tried it without modifications on your server? – Omar May 25 '17 at 09:48
  • I tried it on Apache 2.2 and like craigrs84 said, this module and the 'define' command is not part of Apache 2.2. And I know the page where I can get the source of this module, what I liked to know, if I can get a precompiled version of this module for windows apache. – Radon8472 May 26 '17 at 13:49
  • @radon8472 Have you tried creating a new question and linking this post to it? Because for what you are telling me, it sounds that your question worthy of having its own post – Omar Jun 07 '17 at 03:45
  • @Omar I think creating a question like this would only result in lots of "Compile the sourcecode yourself"-answers. I hoped, that someone has a downloadlink to precompiled files. – Radon8472 Jun 10 '17 at 10:38
  • What if the `static_path` should contain a space? I keep getting syntax error, I tried sorrounding the string in quotes, double quotes and square brackets but none of them seems to be ok. I've searched the docs for this but there's no mention apparently... – Mark Apr 22 '19 at 17:17
  • The documentation that you linked to currently only mentions the "Define variable-name" syntax to declare the existence of a variable, but not the "Define variable-name variable-value" syntax for setting variables with arbitrary values. So it feels like using an undocumented feature to me. – Cito Sep 05 '20 at 14:37
  • @Cito My short answer: It WORKS – Omar Sep 17 '20 at 10:29
  • @Cito If you read the documentation carefully, it DOES explains BOTH. How to define a `variable-name` AND ALSO how to define a `variable-value`. It is right-smacked in the **main section** of the documentation, enclosed in a box to make it even more obvious to find: `Syntax: Define parameter-name [parameter-value]` JUST under the title: **Define Directive** – Omar Sep 17 '20 at 10:35
  • @Mark What OS? Windows? There's a way to get a non-spaced paths in windows. That technique will save your neck and is a quick workaround – Omar Sep 17 '20 at 10:44
  • @mark Read this too: https://medium.com/@leedowthwaite/dealing-with-spaces-in-paths-f26856aef06f Let me know what OS and if this article helps – Omar Sep 17 '20 at 10:47
  • @Omar yes, Windows – Mark Sep 17 '20 at 14:17
  • @Mark try this https://superuser.com/questions/179449/windows-path-without-spaces-with-1 and the other stuff I suggested. Let me know what works (if it works) – Omar Sep 17 '20 at 17:54
  • @Omar You made me feel stupid, so I looked again. You're right, it DOES explain both, but only in the English version of the documentation. My browser shows the German version which currently does NOT explain both (what you see seems to be based on the preferred browser language). Maybe it was forgotten in the German version, or added later and the translation was not updated. As a side note, I think "it works" is in general not a very good argument when something works now, but when it is not documented and not part of the test suite, since it may then stop working in a future version. – Cito Sep 17 '20 at 22:07
8

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

maxpolk
  • 2,167
  • 1
  • 18
  • 24
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
    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) – Omar May 25 '17 at 09:59
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

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'

Bibin Joseph
  • 499
  • 4
  • 3