0

My question is how can I use setEnv with an already defined environment variable.

For example:

"/etc/apache2/envvars"

...
export SSL_ROOT_DIR=/etc/letsencrypt/live
export DEFAULT_HOME_DIR=/var/www/html
...

"/etc/apache2/sites-available/domain.tld.conf"

...
SetEnv SERVERNAME domain.tld
SetEnv HOME_DIR ${DEFAULT_HOME_DIR}/${SERVERNAME}
SetEnv LOG_DIR ${APACHE_LOG_DIR}/${SERVERNAME}
SetEnv SSL_DIR ${SSL_ROOT_DIR}/${SERVERNAME}
...
DocumentRoot ${HOME_DIR}
<Directory "${HOME_DIR}">
  ...
</Directory>
...
SSLCertificateFile ${SSL_DIR}/cert.pem
SSLCertificateKeyFile ${SSL_DIR}/privkey.pem
SSLCertificateChainFile ${SSL_DIR}/chain.pem
...

Without env this config work!

Error output

[core:warn] [pid 13844] AH00111: Config variable ${SERVERNAME} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${SERVERNAME} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${SERVERNAME} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${SERVERNAME} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${HOME_DIR} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${HOME_DIR} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${LOG_DIR} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${LOG_DIR} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${SSL_DIR} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${SSL_DIR} is not defined
[core:warn] [pid 13844] AH00111: Config variable ${SSL_DIR} is not defined
Law29
  • 3,557
  • 1
  • 16
  • 28
UnitedPE
  • 3
  • 4

1 Answers1

0

You have a conceptual misunderstanding about how these work in Apache, but it's not your fault. It's very misleading in Apache and a very common misunderstanding.

Background:

There are two types of environment variables in Apache, native process-wide environment variables and per-request environment variables (that will become the former for a CGI if a CGI happens to be run)

The confusion:

  • Variables of the form ${FOO} are interpolated at startup, they can only be native environment variables (commonly sourced from bin/envvars before apachectl/httpd are ever run)
  • Directives like SetEnv and SetEnvIf set per-request environment variables. Even when they appear unconditional, they aren't executed until request-processing and don't set native environment variables.
covener
  • 1,685
  • 9
  • 15