How to set environment variables in dispatcher config | Community
Skip to main content
Level 2
October 16, 2015
Solved

How to set environment variables in dispatcher config

  • October 16, 2015
  • 8 replies
  • 12964 views

I have followed the directions to set environment variables in dispatcher config for Apache 2.4.  I can use the variables within httpd.conf and included files.   But the variables do not seem to be getting set in dispatcher.any.

 

i'm trying to set /docroot "${DOCROOT}"

 

Where DOCROOT is initialized at apache start

 

export DOCROOT="/opt/aem/httpd/cache"
 

The virtual host entry is using this variable fine.

 

I also noticed that the dispatcher documentation for environment variables has a typo in the /renders section,.  TIt says to set RENDER_IP variable, but the example uses ${PUBLISH_IP}. No big deal, but I figured I would note it while I was talking about it.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by

Environment variables in Apache httpd, despite having the same name, are not related to the environment variables set in the operating system shell.

You should use SetEnv or SetEnvIf (along with ensuring the module mod_env or mod_setenvif is enabled) to set this in the conf - not export.

I personally haven't tested this with Dispatcher, but note that SetEnvIf is processed earlier during request processing than SetEnv, so would probably be the better option to use for this.

See the httpd documentation for more info on this: http://httpd.apache.org/docs/2.4/mod/quickreference.html#S

If you're already setting DOCROOT in the shell, you could use PassEnv (part of mod_env) instead.

 

- Carey

8 replies

Accepted solution
October 16, 2015

Environment variables in Apache httpd, despite having the same name, are not related to the environment variables set in the operating system shell.

You should use SetEnv or SetEnvIf (along with ensuring the module mod_env or mod_setenvif is enabled) to set this in the conf - not export.

I personally haven't tested this with Dispatcher, but note that SetEnvIf is processed earlier during request processing than SetEnv, so would probably be the better option to use for this.

See the httpd documentation for more info on this: http://httpd.apache.org/docs/2.4/mod/quickreference.html#S

If you're already setting DOCROOT in the shell, you could use PassEnv (part of mod_env) instead.

 

- Carey

smacdonald2008
Level 10
October 16, 2015

Are you referring to this AEM doc topic:

USING ENVIRONMENT VARIABLES @ https://docs.adobe.com/docs/en/dispatcher/disp-config.html#Using Environment Variables

This would be the doc topic that explains how to set environment variables for AEM Dispatcher. 

MschilliAuthor
Level 2
October 16, 2015

Pardon the lack of detail in my original post. I think I was misunderstanding some of what I was seeing, and I ended up offering bad information based on incorrect assumptions.

 

Yes. that is the topic I am referring to.  I am running apache 2.4 on redhat, and using the /etc/sysconfig/httpd file to initialize/inject variables into apache.  This is working fine within httpd.conf.  In redhat, the init script sources /etc/sysconfig/httpd so that anything set in that file is initialized as part of the environment that httpd runs in.

 

in /etc/sysconfig/httpd:

export OPTIONS="-DDOMAIN1 -DDOMAIN2" # these options are passed to test for argument existence later.

export HOST=$(hostname)  # this sets the ${HOST} variable and httpd.conf can use that variable for dynamic substitution.

export DOMAIN_NAME1="www.something.com"  # used to set virtualhost ServerName

export DOCROOT1="/opt/aem/httpd/cache1" # used to set virtualhost DocumentRoot

export DOMAIN_NAME2="www.nothing.com"  # used to set virtualhost ServerName

export DOCROOT2="/opt/aem/httpd/cache2" # used to set virtualhost DocumentRoot

 

In httpd.conf, I have the following:


# Configure variables for virtualhosts and redefine them for generic domain.com.conf

#If -DDOMAIN1 was passed in ${OPTIONS}

<IfDefine DOMAIN1>

# Redefine ${DOMAIN_NAME1} to ${DOMAIN_NAME}

  Define DOMAIN_NAME ${DOMAIN_NAME1}
# Redefine ${DOCROOT1} to ${DOCROOT}
  Define DOCROOT ${DOCROOT1}

# Include generic domain.com.conf, which will use the variables.
  Include /opt/aem/httpd/conf/domain.com.conf
</IfDefine>

# the same concept with DOMAIN2 option passed.

<IfDefine DOMAIN2>
  Define DOMAIN_NAME  ${DOMAIN_NAME2}
 
  Define DOCROOT  ${DOCROOT2}
  Include /opt/aem/httpd/conf/domain.com.conf
</IfDefine>

 

CONTENTS of  domain.com.conf:

<VirtualHost *:80>

    ServerAdmin webadmin@${DOMAIN_NAME}
    ServerName ${HOST}
    DocumentRoot ${DOCROOT}
    ErrorLog /opt/aem/httpd/logs/$host-error_log
    CustomLog /opt/aem/httpd/logs/$host-access_log common
    <Directory "${DOCROOT}">
      Options FollowSymLinks ExecCgi Includes
      AllowOverride None
      Order allow,deny
      Allow from all
      SetHandler dispatcher-handler
    </Directory>

  </VirtualHost>

 

Effectively, this allows me to use a dynamic Virtualhost config file over and over again for multiple domains.

Dispatcher does substitute ${HOST} ( initialized in /etc/sysconfig/httpd - shown above), but i can't get it to use any of the redefined variables.

 

Just like with virtualhosts, I have multiple <IfDefine DOMAIN1,DOMAIN2, etc> sections that redefine the "1" or "2", etc variable to the UnNumbered variable, for use in calling dispatcher.any.

 <IfDefine DOMAIN1>
    SetEnv DOMAIN_NAME  ${DOMAIN_NAME1}
    PassEnv DOMAIN_NAME
    SetEnv DOCROOT  "/opt/aem/httpd/cache"
    PassEnv DOCROOT
    SetEnv DISP_DOMAIN ${DISP_DOMAIN1}
    PassEnv DISP_DOMAIN

    DispatcherConfig "/opt/aem/httpd/conf/dispatcher.any"

  </IfDefine>


  But I can't get dispatcher to pick up any of the "redefined" variables.   If I use ${DOCROOT1} (defined in /etc/sysconfig/httpd) , it will work fine.  But I can't get it to pick up the redefined ${DOCROOT} variable.

 

I have tried using "Define", "SetEnv" and "PassEnv": 

 

Define DOCROOT "/opt/aem/httpd/cache" # A Define with static value.

Include /opt/aem/httpd/conf/dispatcher.any

AND

SetEnv DOCROOT "/opt/aem/httpd/cache" # SetEnv with static value

Include /opt/aem/httpd/conf/dispatcher.any

AND

SetEnv DOCROOT ${DOCROOT1} # SetEnv with variable value.

PassEnv DOCROOT # Pass the newly set variable.

Include /opt/aem/httpd/conf/dispatcher.any

 

 

 

Relevant line in dispatcher.any:

      /docroot "${DOCROOT}"

 

dispatcher.log:

[Thu Feb 19 07:59:59 2015] [W] [18880(139884441118688)] /opt/aem/httpd/conf/dispatcher.any:123: environment variable not defined: DOCROOT
[Thu Feb 19 07:59:59 2015] [D] [18880(139884441118688)] farms[website].cache.docroot =
[Thu Feb 19 07:59:59 2015] [I] [18880(139884441118688)] Dispatcher initialized (build 4.1.8)

 

None of these are getting passed in a way that the dispatcher module can use them.

Any ideas?

 

THANKS!

August 26, 2021

Hi @mschilli 

You can make an entry inside /usr/lib/systemd/system/httpd.service

EnvironmentFile=/etc/sysconfig/httpd

 

then stop and start the httpd service it will work.

smacdonald2008
Level 10
October 16, 2015

We passed this to the support team to see if there is a known bug. 

MschilliAuthor
Level 2
October 16, 2015

Any word from support?

 

Thanks!

 

Matt

this-that-the-otter
Level 4
November 30, 2016

FYI - I had the same issue, I set the variable within /etc/sysconfig/httpd (running centos 7 and disp 4.2.1) - it turns out the documentation showing the variable in dispatcher.any had a syntax error -

${PUBLISH_IP} should be {$PUBLISH_IP}.

Regards,

Bill

this-that-the-otter
Level 4
November 30, 2016

Unfortunately fixing the syntax only makes configtest happy, Apache still fails to start :(

this-that-the-otter
Level 4
December 1, 2016

Looks like the doc syntax was correct,

'service httpd configtest'

still gives warning :

[Wed Nov 30 21:00:14.765338 2016] [core:warn] [pid 13583:tid 140210428229696] AH00111: Config variable ${PUBLISH_IP} is not defined

but 'service httpd start' works and the correct render back-end is called when ${PUBLISH_IP} is used in dispatcher.any.

Variable is set in /etc/sysconfig/httpd:

PUBLISH_IP=192.168.1.10

Regards,

Bill

this-that-the-otter
Level 4
November 15, 2024

I just found out today (after several years seeing this warning) in order to tell apache about these variables in a way it won't complain during a configtest is to supply them in a config file. I chose /etc/httpd/conf/httpd.conf, e.g.

 

Define PUBLISH_IP 192.168.10.12

 

or as a command line argument, I presume by editing some low level apache script and passing the variable using the -D flag.

 

https://stackoverflow.com/questions/6569080/how-to-define-a-variable-in-apaches-httpd-conf-file

https://httpd.apache.org/docs/2.4/mod/core.html#define

 

P.S. In our OS (RHEL) /etc/sysconfig/httpd was replaced with /etc/systemd/system/httpd.service.d/override.conf (via systemctl edit httpd.service).