Ok, so I'm building a docker image for apache. I want to allow users to specify custom apache config with the environment variable.
TL;DR
The question could be simplified to this: how to use environment variables to print part of the config (not just directives values). Something like this:
<IfDefine HasCustomVHostConfig>
${APACHE_CUSTOM_VHOST_CONFIG}
</IfDefine>
Right now, the value of environment variable seem to be printed, but apache ignores most of the directives.
Long story short.
We have an 1) docker entry point 2) apache config 3) environment variable that holds part of that config (which could be modified by user starting docker image).
1. Docker entry point# Apache gets grumpy about PID files pre-existing
rm -f /usr/local/apache2/logs/httpd.pid
# Read apache2 env vars
source /etc/apache2/envvars
# Build apachectl options array
APACHE_OPTS=(-DFOREGROUND)
# If custom config has been set, add an option flag we will check later in config
if [[ -v APACHE_CUSTOM_VHOST_CONFIG ]]; then
APACHE_OPTS+=(-DHasCustomVHostConfig)
fi
# Run apache2 in foreground
/usr/sbin/apachectl "${APACHE_OPTS[@]}"
2. Apache virtual host config
<VirtualHost _default_:80>
ServerSignature Off
ErrorLog ${APACHE_LOG_DIR}/000-default-error.log
CustomLog ${APACHE_LOG_DIR}/000-default-access.log combined
DocumentRoot ${APACHE_DOCUMENT_ROOT}
<IfDefine HasCustomVHostConfig>
${APACHE_CUSTOM_VHOST_CONFIG}
</IfDefine>
<Directory ${APACHE_DOCUMENT_ROOT}>
Options FollowSymLinks
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
Note this config has an IfDefine
statement, which will check option flag we set in entrypoint, and if it is set, will print environment variable.
Testing
- If I set environment variable to incorrect configuration, I will clearly see error during apache startup, meaning all flags and IfDefine statements are working.
- If I set environment variable to simple redirect with mod_redirect, it will work, meaning apache is able to apply configuration printed with env variable.
- If I set environment variable to more complex redirect with mod_rewrite, or to a proxy configuration with mod_proxy and mod_proxy_http - apache will ignore it. If same configuration is added directly to the config, apache will pickup and apply it.
What do I missing here?