popularity
question
0

virtual host creation

Hi,

I have a lot of virtual hosts in apache2 and the only thing differentiating them is ServerName, I was thinking about creating a attribute with a array of all the ServerNames and generate them with a ERB template. I’m not really sure what would be the correct approach?

Any suggestions?

Thanks

 
Answers: 1
0
Best

The apache2 cookbook comes with a definition called web_app for doing just this thing. Example usage might look something like this:

array_of_apps.each do | app |
  web_app app do
    template "web_app.conf.erb"
    docroot "#{node['apps']['dir']}/#{app}"
    server_name "#{app}.#{node['fqdn']}"
    server_aliases "#{app}.#{node['fqdn']}"
    notifies :restart, resources("service[apache2]")
  end
end

This example assumes each app has a subdirectory at node[‘apps’][‘dir’] that will become the docroot for the vhost and you would like a subdomain created for each app. The actual array of apps/vhost names to iterate over should probably come from a data bag so adding new vhosts wouldn’t require a code/cookbook update! Yeah data-driven infrastructure.

Answered
Mar 17 '11 at 18:52