: Configuration-time variables in apache configuration files I have several virtual host configurations which are stored in files on their own and differ mostly in properties like DocumentRoot, logfile
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 ${stem}.com #if ${more_aliases}
ServerAlias ${more_aliases} #endif
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 ^/(.*) localhost:${port}/VirtualHostBase/http/%{HTTP_HOST}:80/unitracc/VirtualHostRoot/ [L,P]
</VirtualHost>
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.
More posts by @Annie201
2 Comments
Sorted by latest first Latest Oldest Best
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.
Take a look at mod_macro, sounds like it may be exactly what you need for your use case.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.