When configuring Apache for frameworks like Django with WSGI, we often face repetitive path declarations:
<Directory /path/to/foo/>
Order allow,deny
Allow from all
</Directory>
Alias /foo/static /path/to/foo/static
WSGIScriptAlias /foo /path/to/foo/run_wsgi
This becomes particularly problematic when:
- Managing multiple similar environments (dev/stage/prod)
- Sharing configurations across team members
- Migrating servers with different base paths
Apache provides the Define
directive which works similarly to environment variables:
Define PROJECT_ROOT "/path/to/foo"
<Directory ${PROJECT_ROOT}>
Order allow,deny
Allow from all
</Directory>
Alias /foo/static ${PROJECT_ROOT}/static
WSGIScriptAlias /foo ${PROJECT_ROOT}/run_wsgi
For more complex scenarios, consider using include files with variable definitions:
1. Create a vars.conf:
Define PROJECT_ROOT "/path/to/foo"
Define STATIC_URL "/foo/static"
Define APP_URL "/foo"
2. Include it in your main config:
Include /path/to/vars.conf
<Directory ${PROJECT_ROOT}>
# Configuration here
</Directory>
Alias ${STATIC_URL} ${PROJECT_ROOT}/static
WSGIScriptAlias ${APP_URL} ${PROJECT_ROOT}/run_wsgi
- Place all variable definitions at the top of your config file or in a dedicated include file
- Use uppercase names for variables to distinguish them from other directives
- Document each variable's purpose in comments
- Consider version controlling your variable definitions separately from main configs
For maximum flexibility, combine Apache variables with system environment variables:
Define PROJECT_ROOT "${ENV:PROJECT_PATH}"
<Directory ${PROJECT_ROOT}>
# Rest of the configuration
</Directory>
Then set the environment variable when starting Apache:
export PROJECT_PATH="/path/to/foo"
apachectl start
When configuring Apache web servers, you often encounter repetitive paths or values that need to be referenced multiple times. This becomes particularly noticeable when setting up Django+WSGI applications or similar web frameworks. The traditional approach leads to duplicated values, making maintenance harder and increasing the risk of inconsistencies.
Apache provides the Define
directive which allows you to create variables at server startup. These can be referenced throughout your configuration files using the ${VAR_NAME}
syntax.
Define PROJECT_ROOT "/path/to/foo"
<Directory ${PROJECT_ROOT}>
Order allow,deny
Allow from all
</Directory>
Alias /foo/static ${PROJECT_ROOT}/static
WSGIScriptAlias /foo ${PROJECT_ROOT}/run_wsgi
For more dynamic configurations, you can leverage environment variables:
SetEnv PROJECT_ROOT "/path/to/foo"
<Directory ${PROJECT_ROOT}>
Require all granted
</Directory>
Another approach is to use include files where you define common variables:
# In vars.conf
SetEnv PROJECT_ROOT "/path/to/foo"
# In your main config
Include vars.conf
<Directory ${PROJECT_ROOT}>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
For complex scenarios, consider using mod_macro
which provides template-like functionality:
<Macro Project $name $path>
<Directory $path>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Alias /$name/static $path/static
WSGIScriptAlias /$name $path/run_wsgi
</Macro>
Use Project foo "/path/to/foo"
- Document all variables at the top of your configuration
- Use clear, descriptive names for variables
- Consider using relative paths where appropriate
- Test configurations after changes