Is it possible to use variables in httpd.conf
Asked Answered
E

2

38

Is there a way to use variables of some sort in an apache httpd.conf file? I'd like to define a value and use it throughout a block, as in

define myvar somename #or whatever the syntax would be
alias /my/path "/some/path/${myvar} #or whatever the syntax would be
Errantry answered 13/1, 2011 at 20:3 Comment(1)
See this question: #6569580Marrymars
N
48

Yes, kind of. You can get environment variables substituted in to the config file at start up with the ${ENVVAR} syntax. It's up to you to figure out how to set those variables before starting up the server.

http://httpd.apache.org/docs/2.2/configuring.html#syntax

Note that these variables will persist so any scripts in languages like php will be able to read them.

Also it's important to note that these values are interpreted once only when the server starts up so they're more like constants than variables.

Update

As of httpd version 2.4, see this answer instead: https://mcmap.net/q/269650/-how-to-define-a-variable-in-apache-39-s-httpd-conf-file

Nonagon answered 13/1, 2011 at 20:28 Comment(4)
in debian-likes installation they're defined in /etc/apache2/envvarsLabium
Right, which is why I say it's "up to you". Debian's Apache packaging is a shameful mess.Nonagon
thanks! I don't think I can do exactly what I want but this will actually help to simplify things alot.Errantry
As stated in a comment above, there is a better answer in this question stackoverflow.com/questions/6569080Wrenn
S
10

Try mod_macro. It actually allows you to use what are essentially variables. An example from the module's docs page gives the gist of it:

## Define a VHost Macro for repetitive configurations

<Macro VHost $host $port $dir>
  Listen $port
  <VirtualHost *:$port>

    ServerName $host
    DocumentRoot $dir

    <Directory $dir>
      # do something here...
    </Directory>

    # limit access to intranet subdir.
    <Directory $dir/intranet>
      Require ip 10.0.0.0/8
    </Directory>
  </VirtualHost>
</Macro>

## Use of VHost with different arguments.

Use VHost www.apache.org 80 /vhosts/apache/htdocs
Use VHost example.org 8080 /vhosts/example/htdocs
Use VHost www.example.fr 1234 /vhosts/example.fr/htdocs

I found a download for it at http://www.cri.ensmp.fr/~coelho/mod_macro/

Sulfathiazole answered 14/5, 2013 at 5:5 Comment(2)
mod_macro is conveniently found in Debian/Ubuntu, if you're using the Debian/Ubuntu mangling of Apache. We use mod_macro heavily and it's a positive delight. Package is: libapache2-mod-macroBalkanize
Alternatively you could have similar functionality with define, although less elegant. Example: define hostname mysite.com IncludeOptional vhost_template.conf undefine hostnameVizard

© 2022 - 2024 — McMap. All rights reserved.