Sharing configuration between several VHosts

2019-05-31 00:28发布

问题:

I'm managing a httpd installation with several sites running on it, each defined within it's own <VirtualHost> directive. There are several configuration variables (most of them from core as well as some php_flag/php_value) that are exactly the same for several of the sites defined.

I would like to be able to define a configuration set that will apply to several <VirtualHost> directives at the same time (to remove redundancy and make the server maintenance a little bit easier).

I have considered using <Directory>, however many of the configuration values I need to set, cannot be modified from within <Directory>. I have also tried to define another <VirtualHost> section, containing all of the sites I want to share the config in

<VirtualHost xx.xx.xx.xx:80 yy.yy.yy.yy:80>
    ...config...
</VirtualHost>

but this just results in

VirtualHost xx.xx.xx.xx:80 overlaps with VirtualHost yy.yy.yy.yy:80, the first has precedence, perhaps you need a NameVirtualHost directive

Any idea how I can achieve this?

回答1:

In apache documentation this is called mass VirtualHost configuration.

Several solutions exists:

  • mod_vhost_alias, with the VirtualDocumentRoot instructions, but it is usually too simple and managing other variations than DocumentRoot and the domain name are quite hard.
  • advanced mod_rewrite tricks, as mod_rewrite is a swiss knife and can do everything, but everything seems quite complex when it is done with mod_rewrite.
  • mod_macro is a module that allow the usage of an infinite list of macros with parameters that can be reused between Virtualhosts (for example), so you can add more parameters than DocumentRoot and ServerName and share single configurations files between serval Vhosts.