How to Use Variables for ServerName in Apache httpd.conf Configuration


2 views

html

Managing multiple virtual hosts in Apache often leads to repetitive configurations, especially when each block contains identical patterns like ServerName and file paths. Consider this common scenario:

<VirtualHost *:443>
    DocumentRoot /var/www/html/www.example1.com
    ServerName www.example1.com
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/www.example1.com/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/www.example1.com/private.key
    SSLCACertificateFile  /var/www/ssl/www.example1.com/bundle.crt
</VirtualHost>

Apache's Define directive combined with Include provides an elegant solution:

# Define variables in a separate file (e.g., vars.conf)
Define SERVER_DOMAIN "www.example1.com"

# Main httpd.conf
Include conf/vars.conf

<VirtualHost *:443>
    DocumentRoot /var/www/html/${SERVER_DOMAIN}
    ServerName ${SERVER_DOMAIN}
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/${SERVER_DOMAIN}/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/${SERVER_DOMAIN}/private.key
    SSLCACertificateFile  /var/www/ssl/${SERVER_DOMAIN}/bundle.crt
</VirtualHost>

For large-scale deployments, consider using Apache's macro processor (mod_macro):

<Macro SharedSSLConfig $domain>
    DocumentRoot /var/www/html/$domain
    ServerName $domain
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/$domain/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/$domain/private.key
    SSLCACertificateFile  /var/www/ssl/$domain/bundle.crt
</Macro>

Use SharedSSLConfig www.example1.com
Use SharedSSLConfig www.example2.com
  • Always test configuration with apachectl configtest
  • Variable substitution works in Apache 2.4+
  • For complex scenarios, consider using configuration management tools

Managing multiple similar VirtualHost entries in Apache's httpd.conf often leads to configuration bloat. Each new domain requires duplicating nearly identical blocks while only changing the domain name and paths.

# Traditional repetitive approach
<VirtualHost *:443>
    DocumentRoot /var/www/html/www.example1.com
    ServerName www.example1.com
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/www.example1.com/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/www.example1.com/private.key
    SSLCACertificateFile  /var/www/ssl/www.example1.com/bundle.crt
</VirtualHost>

Apache's Define directive combined with Include allows creating template-like configurations. Here's how to implement it:

# Define a template file (vhost-template.conf)
<VirtualHost *:443>
    DocumentRoot /var/www/html/${server_name}
    ServerName ${server_name}
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/${server_name}/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/${server_name}/private.key
    SSLCACertificateFile  /var/www/ssl/${server_name}/bundle.crt
</VirtualHost>

# In main httpd.conf
Define server_name www.example1.com
Include vhost-template.conf

Define server_name www.example2.com
Include vhost-template.conf

For more complex scenarios, Apache's mod_macro (available in 2.4+) provides better flexibility:

# Load the module (if not already loaded)
LoadModule macro_module modules/mod_macro.so

# Define the macro
<Macro VHost $domain>
<VirtualHost *:443>
    DocumentRoot /var/www/html/$domain
    ServerName $domain
    SSLEngine on
    SSLCertificateFile    /var/www/ssl/$domain/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/$domain/private.key
    SSLCACertificateFile  /var/www/ssl/$domain/bundle.crt
</VirtualHost>
</Macro>

# Use the macro
Use VHost www.example1.com
Use VHost www.example2.com

For large-scale deployments, combine this with configuration management tools:

# Ansible example
- name: Configure Apache vhosts
  template:
    src: vhost-template.conf.j2
    dest: "/etc/apache2/sites-available/{{ item }}.conf"
  with_items: "{{ domains }}"
  notify: restart apache

Remember these key points:

  • Always test configurations with apachectl configtest
  • Variables won't work in all directives (check Apache docs)
  • For mass virtual hosting, consider VirtualDocumentRoot
  • Keep your certificate paths consistent with the domain naming pattern