How to define a variable in apache's httpd.conf file?
Asked Answered
M

5

75

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 ?

Mcguigan answered 4/7, 2011 at 8:33 Comment(3)
check that response for example: https://mcmap.net/q/270932/-conditions-in-apache/…Mongolia
should / could this question be moved to serverfault? serverfault.com/help/on-topicLisandra
Also see 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.5Redfin
J
152

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

Jeanniejeannine answered 31/3, 2013 at 16:51 Comment(17)
mod_define not included in Apache 2.2 by defaultPainless
Old original (made compatible with 2.0 and 2.2) mod_define is at people.apache.org/~rjung/mod_define/mod_define.htmlCenis
This is definitely the ideal way to define variables in httpd conf files. I do hope the apachectl people stumble across this question.Endear
Where can i find mod_define for windows lib apache 2.2 ?Carmel
@Carmel 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?Jeanniejeannine
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.Carmel
@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 postJeanniejeannine
@Jeanniejeannine 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.Carmel
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...Trencherman
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.Triune
@Triune My short answer: It WORKSJeanniejeannine
@Triune 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 DirectiveJeanniejeannine
@Trencherman 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 workaroundJeanniejeannine
@mark Read this too: medium.com/@leedowthwaite/… Let me know what OS and if this article helpsJeanniejeannine
@Jeanniejeannine yes, WindowsTrencherman
@Trencherman try this superuser.com/questions/179449/… and the other stuff I suggested. Let me know what works (if it works)Jeanniejeannine
@Jeanniejeannine 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.Triune
M
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

Merritt answered 13/6, 2012 at 23:40 Comment(0)
I
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.

Imperium answered 4/7, 2011 at 8:33 Comment(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)Jeanniejeannine
N
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

Numbersnumbfish answered 8/8, 2019 at 21:25 Comment(0)
H
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'

Heartworm answered 11/10, 2019 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.