I have several virtual host configurations which are stored in files on their own and differ mostly in properties like DocumentRoot, logfile names, ports (for a RewriteRule) and the like.

Sometimes those configurations need to be changed; and I'd consider it extremely convenient if I could apply the changes simply in a template which resolves some variables at configuration time (i.e. while doing a service reload); for example:

<VirtualHost *:80>
ServerName ${stem}.com
ServerAlias www.${stem}.com
#if ${more_aliases}
ServerAlias ${more_aliases}
DocumentRoot /var/www/${stem}
CustomLog /var/log/httpd/${stem}.log combined
ErrorLog /var/log/httpd/${stem}.error.log
RewriteEngine On
RewriteCond ...  # some contents can be served by Apache directly
# the ${port} is a property of this virtual host:
RewriteRule ^/(.*) http://localhost:${port}/VirtualHostBase/http/%{HTTP_HOST}:80/unitracc/VirtualHostRoot/$1 [L,P]

Is this possible, and how would I define and reference those configuration-time variables?

I use Apache 2.2 (virtual development machine) and 2.4 (production servers) on Linux.

2 Answers 2


Take a look at mod_macro, sounds like it may be exactly what you need for your use case.

  • Excellent suggestion! I stole it. Sorry. One up-vote is the best I can do plus the mention. Cheers!!
    – closetnoc
    Commented Jun 10, 2016 at 16:52
  • This looks exactly like what I need. Unfortunately, it is availabe for Apache 2.4.5+ only ...
    – Tobias
    Commented Jun 13, 2016 at 10:25

Both; Yes. And No.

For the No part.

Apache is not expecting a variable within a configuration file. It is just not coded for that. As well, from a logic perspective, it would be impractical for Apache to fill in variables in various configuration files without yet another layer of configuration. Apache is intentionally lightweight for a reason. Any service residing in memory must be as small as possible especially when the process is spun-off into enough resident-ready processes to handle numerous requests immediately and potentially spin-off more processes to handle a spike in load.

Now for the Yes part.

You can always create a database and code that generates new configuration files based upon what the code sees in the data itself. In this respect, you can use a single configuration file with variables that can be filled in to recreate the configuration files. This is very possible and simple to do.

From @PlanetScaleNetworks, the suggestion to use mod_macro is an example of the above. The disadvantage of mod_macro is that it will have to be run with parameters for each site that you wish to create a configuration for. This may be trivial enough not to write your own code. You can simply create a batch file for this. However, I cannot imagine that it is a simpler and more convenient system for configuring many sites especially in light that the 000-default.conf file can simply be modified to your standards only once, copied when a new configuration is needed, then edited. This is also trivial enough to be very quick and simple.

Now for the bad news.

Apache will always required a restart. This is because site configurations are cached within memory to reduce the necessity of hitting the hard-dive over and over. Reading from memory has always been much faster and less resource intensive than reading from permanent storage. Be that as it may, Apache does restart very quickly because it is so light-weight so that any restart should be fairly transparent short of an extremely busy system. It is possible for most systems that few or no requests go unanswered. However, for very busy systems, it is very possible that a few requests go unanswered with each restart. This would happen anyway. This means that the cost is a natural result. It is possible that whatever code you write to rewrite the configuration files, can also do the restart.

  • Thanks for your verbose answer. Despite your comments, mod_macro seems to be exactly what I need. Thanks for the hyperlink. Since it has Base status, it seems to be considered useful enough to be available by default. If I understand right, macros are run at configuration time only, and thus won't cause a performance impact. The main advantage for me as a programmer would be: I'm able to have my tricky configuration details in scm-managed files, without any need to repeat some generation step; just systemctl reload httpd. Or do you have special knowledge a restart to be required?
    – Tobias
    Commented Jun 13, 2016 at 8:39
  • @Tobias I would recommend mod_macro. The drawbacks, small as they are, that I could see was that it requires a command per site and a restart or a reload as you suggest. A batch file could rebuild all of your configuration files for you at one time or you can just build one at a time using a command or a batch file. Either way, it should work just fine. I have never used mod_macro. I actually forgot about that one... being a coder, I would have just written a quick PERL script then kicked myself later. ;-) Cheers!!
    – closetnoc
    Commented Jun 13, 2016 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.