Using constant variables in apache configuration

12,669

This has been asked before. The link is here:

Using variables in Apache config files to reduce duplication?

Share:
12,669

Related videos on Youtube

SpliFF
Author by

SpliFF

Updated on September 18, 2022

Comments

  • SpliFF
    SpliFF almost 2 years
    <VirtualHost *:80>
    SetEnv SITENAME testing
    ServerName www.test.com
    
    # Location of files
    DocumentRoot "/www/${SITENAME}/site"
    Alias /data "/www/data/${SITENAME}"
    Alias /base "/www/${SITENAME}/base/site"
    
    # Logging
    ErrorLog "/var/log/httpd/${SITENAME}-error.log"
    CustomLog "/var/log/httpd/${SITENAME}-access.log" combined
    </VirtualHost>
    
    • Starting apache2 ... Warning: DocumentRoot [/www/${SITENAME}/site] does not exist

    A further reading of the documentation provides a hint that SetEnv may not bind early enough to be used in paths and the suggested alternative SetEnvIf is based on the request properties as well.

    I've also seen examples using mod_vhost_alias however that does not seem to handle anything but the DocumentRoot and even then it's required to use components of the server name which is not what I want.

    What I want is pure variable substitution available when the config is read and usable in any string (like a #DEFINE in C). My Apache version is 2.2. Can it be done?

    UPDATE: I found a Define directive in the documentation but it doesn't work (looks like it's new to 2.3). I guess the question now is wether there's a module that backports this or equivalent functionality to 2.2?

    • silviud
      silviud about 13 years
      SetEnv will set the web server environment and it can be uses further from any script/program that can access the environment.
    • BMDan
      BMDan about 13 years
      An important question to ask: why do you want this type of setup? What's the end goal of your configuration?
    • SpliFF
      SpliFF about 13 years
      It simply makes setting up vhosts easy. I can edit the value in one place rather than 10. It isn't life or death it's just 9 less things to do when I create a new site. I typically use a sed script to replace all instances but sometimes it matches things it shouldn't.
    • silviud
      silviud about 13 years
      I would do some tokens into the config, have a script parsing the file and replace it later into the script then generate a config file and start apache.
  • SpliFF
    SpliFF about 13 years
    Close enough. Since the syntax varies radically from Define I'll just wait till I'm using Apache 2.4.