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

Apache

Apache Problem Overview


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 ?

Apache Solutions


Solution 1 - Apache

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

Solution 2 - Apache

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

Solution 3 - Apache

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.

Solution 4 - Apache

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

Solution 5 - Apache

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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSatyaView Question on Stackoverflow
Solution 1 - ApacheOmarView Answer on Stackoverflow
Solution 2 - ApachemaxpolkView Answer on Stackoverflow
Solution 3 - ApacheChristine ReardonView Answer on Stackoverflow
Solution 4 - ApacheNikolaiJamezView Answer on Stackoverflow
Solution 5 - ApacheBibin JosephView Answer on Stackoverflow