3

I have a config file for apache2 virtualhosts. For each virtuahost I have a conf file which looks like this:

NameVirtualHost mydomain.co.uk:80

<VirtualHost mydomain.co.uk:80>
        DocumentRoot /home/d/r/drupal/web/public_html
        ServerName mydomain.co.uk
        ServerAlias www.mydomain.co.uk
        ErrorLog /var/log/apache2/sites/mydomain.co.uk_error-log
        CustomLog /var/log/apache2/sites/mydomain.co.uk_access-log "combined"

        # Rewrite the www
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www\.mydomain.co.uk/?$ [NC]
        RewriteRule ^(.*)$ http://mydomain.co.uk$1 [L,R=301]

        # Read in the drupal configuration
        Include conf.d/drupal6.conf

        # Block access while developing
#       Include conf.d/beta.conf

</VirtualHost>

How can I specify myDomain in a single place and re-use this string. i.e.

mydomain = "www.SomeDomainIHave.com"

The idea being that I specify the domain one at the top of the file so to avoid any mistakes further down.

Hope this makes sense.

Thanks.

5 Answers 5

1

Alternatively, if most of your vhosts are following similar templates, you could use mod_macro. I've recently set this up for an apache installation with 200+ similar vhosts and it's working like a charm.

You're example would be re-written as such : ( /etc/apache2/macro.d/drupal.vhost.conf (

<Macro drupal0 $domain>
NameVirtualHost $domain:80

<VirtualHost $domain:80>
        DocumentRoot /home/d/r/drupal/web/public_html
        ServerName $domain
        ServerAlias www.$domain
        ErrorLog /var/log/apache2/sites/$domain_error-log
        CustomLog /var/log/apache2/sites/$domain_access-log "combined"

        # Rewrite the www
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www\.$domain/?$ [NC]
        RewriteRule ^(.*)$ http://$domain$1 [L,R=301]

        # Read in the drupal configuration
        Include conf.d/drupal6.conf

        # Block access while developing
#       Include conf.d/beta.conf
</VirtualHost>
</Macro>

Using the macro : ( /etc/apache2/sites-available/drupal.sites.conf )

Include /etc/apache2/macro.d/drupal.vhost.conf
Use drupal0 mydomain.co.uk
1
  • absolutely spot on... this will (and already has) saved on a lot of typos. Thank you.
    – belliez
    Feb 1, 2010 at 16:35
1

Directly you can not. However, if you have amny such parameters that you want to set, then a template-based approach can do. Use m4/sed/ruby/python to substitute variables in your files. Downside is that you must have a process to regenerate the files when you change something but that can be automated.

1
  • thats a shame.. was hoping for something simple like a variable declaration in c or something!
    – belliez
    Jan 26, 2010 at 10:47
0

have a look at configuration management tools like Puppet or Chef. These will allow you to define configuration file templates and rebuild them when you make any changes to the setting. You then can define generic settings such as domain name and reuse it in your templates. These tools would then deploy new configuration and restart your web services. Just don't reinvent the wheel again writing your own configuration management tool (who hasn't done this yet, btw? :) )

0

You can't do this directly in apache. You'll have to use some other process to generate the apache config files.

0

it is not possible to use variables as Keltia already said, but perhaps mod_vhost_alias can help you with your problem.

<VirtualHost 111.22.33.44>
  ServerName www.commercial.isp.com

  CustomLog logs/access_log.commercial vcommon

  VirtualDocumentRoot /www/commercial/%0/docs
  VirtualScriptAlias /www/commercial/%0/cgi-bin
</VirtualHost>

%0 in this example will substitute with www.commercial.isp.com.

here are some more examples.

4
  • I do not think mod_vhot_alias can do that.
    – proy
    Jan 26, 2010 at 13:15
  • i put an example in my answer to show how i think this can work for this problem.
    – Christian
    Jan 28, 2010 at 10:49
  • can I specify www.commercial.isp.com outside of VirtualHost? If you look at the top of my sample script in the question I also include "NameVirtualHost mydomain.co.uk:80" before.
    – belliez
    Jan 29, 2010 at 12:38
  • when you use name based vhosts, you only have to configure NameVirtualHost once for each ip. it is not needed for each virtual host. the doc says you even shouldn't use a fqdn for NameVirtualHost. perhaps it is enough for your config to use NameVirtualHost *. here is the link to the docs: httpd.apache.org/docs/2.0/mod/core.html#namevirtualhost
    – Christian
    Jan 29, 2010 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .