我的问题是如何使用 setEnv 与已定义的环境变量。
例如:
“/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
...
无需环境,此配置即可工作!
错误输出
[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
答案1
您对 Apache 中这些功能的工作原理存在概念上的误解,但这不是您的错。这在 Apache 中非常具有误导性,并且是一种非常常见的误解。
背景:
Apache 中有两种类型的环境变量:本机进程范围环境变量和每个请求的环境变量(如果恰好运行了 CGI,则将变为前者)
令人困惑的是:
- 形式为 ${FOO} 的变量插入于启动,它们只能是本机环境变量(通常在 apachectl/httpd 运行之前从 bin/envvars 中获取)
- SetEnv 和 SetEnvIf 等指令会设置每个请求的环境变量。即使它们看起来是无条件的,它们也不会在请求处理之前执行,也不会设置本机环境变量。