Click to See Complete Forum and Search --> : Using variables in httpd.conf file


The King Ant
08-16-2001, 09:44 PM
Is it possible to use variables in an httpd.conf file? Say I have this:

<VirtualHost 192.168.168.192>
ServerName www.domainname.com (http://www.domainname.com)
ServerAlias domainname.com
DocumentRoot /home/domainname.com/html
</VirtualHost>

Could I instead make a variable equal to "www.domainname.com", and then have something like

<VirtualHost 192.168.168.192>
SetEnv THIS_DOMAIN domainname.com
ServerName www.$THIS_DOMAIN (http://www.$THIS_DOMAIN)
ServerAlias $THIS_DOMAIN
DocumentRoot /home/$THIS_DOMAIN/html
</VirtualHost>

That would make it much easier to add a new virtual host by just copying over that section. I would only have to change 1 line instead of 5 or 6 (with logging, ServerAdmin, etc.)

Craig McPherson
08-29-2001, 02:07 AM
Not really, no. I've used "variables" in httpd.conf by using sed, but doing what you're trying to do with sed would be nontrivial ("nontrivial" = "I don't know how").

What I'd do would be to use a text editor's copy, paste, and search+replace functions to make the process a bit easier. If you don't know how to do copy/paste/search in vi or emacs, just use a GUI editor like the KDE text editor, copy the VirtualHost block x number of times, then use an interactive search+replace to stick the appropriate info in each block.

Yes, it would be more elegant to generate the httpd.conf using a shell script with sed and awk or maybe a one-line perl script, but that will be left as an exercise to the reader.