4

I've setup my first vagrant machine, with some cookbooks downloaded through knife.

I am stuck with the setup of a virtual host.

Here's my Vagrantfile:

Vagrant.configure("2") do |config|

  config.vm.box = "precise32"

  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  config.vm.network :forwarded_port, guest: 80, host: 8080

  config.vm.network :private_network, ip: "192.168.33.10"


  config.vm.provision :chef_solo do |chef|
    chef.json = {
        "mysql" => {
        "server_root_password" => "admin",
        "server_repl_password" => "admin",
        "server_debian_password" => "admin"
        },
        "apache" => {
            "listen_address" => "0.0.0.0"
        }
    }
    chef.add_recipe "apt"
    chef.add_recipe "vim"
    chef.add_recipe "openssl"
    chef.add_recipe "apache2"
    chef.add_recipe "mysql"
    chef.add_recipe "mysql::server"
    chef.add_recipe "php"
    # chef.add_recipe "php::module_apc"
    chef.add_recipe "php::module_curl"
    chef.add_recipe "php::module_mysql"
    chef.add_recipe "apache2::mod_php5"
    chef.add_recipe "apache2::mod_rewrite"
  end

  web_app "blog_site" do
    server_name "blog"
    server_aliases [ "blog.#{node['domain']}", node['fqdn'] ]
    docroot "/var/www/blog_site"
  end

  #
end

I've seen here that if I want to set a virtual host through the apache cookbook I have to use the "web_app" definition.

I've added the web_app to the vagrant file but I am getting this error

in `block in <top (required)>': undefined method `web_app' for main:Object (NoMethodError)

that means (I think) that syntax is wrong :)

How can I fix this? Where the definition "web_app" goes?

Robert
  • 10,403
  • 14
  • 67
  • 117
apelliciari
  • 8,241
  • 9
  • 57
  • 92
  • Why did you add: chef.add_recipe "mysql" and chef.add_recipe "mysql::server"? Isn't chef.add_recipe "mysql::server" enough? – Radu Nov 03 '13 at 17:45

2 Answers2

15

Invocation of web_app must go into a recipe.

For example, you can create my-site cookbook in a directory which is also named my-site. There must be at least 3 files:

  1. my-site/metadata.rb with basic metadata:

    name "my-site"
    description "Adds Apache domain configuration for my site"
    
  2. my-site/recipes/default.rb:

    include_recipe "apache2"
    
    web_app "my_site" do
      server_name "my-site.localhost"
      server_aliases ["www.my-site.localhost"]
      docroot "/vagrant"
    end
    
  3. my-site/templates/default/web_app.conf.erb - copy it's contents from example template from apache2 cookbook (apache2/templates/default/web_app.conf.erb).

Note that I use "my-site.localhost" as ServerName. You should replace it with your domain name, because node['fqdn'] and node['domain'] are not defined in your code. DocRoot must be also correct path to your site - it probably will be your vagrant synced directory, which is "/vagrant" by default (you can change it).

You'll porbably also want to add 192.168.33.10 my-site.localhost to your hosts file on the host machine (your actual OS).

I've written an introductory post on Vagrant and Chef solo, it may be useful to you: http://scriptin.github.io/2013-05-09/vagrant-chef-intro.html

scriptin
  • 3,060
  • 1
  • 24
  • 33
  • please update the answer adding that a template is needed too. i copied mine (web_app.conf.erb) from the apache2 cookbooks – apelliciari May 16 '13 at 09:05
  • Is there a way to use default template provided by Apache2 cookbook ? – xyz Jun 02 '13 at 09:46
  • 1
    @KamilZieliński It seems like there is a way to share files/templates between cookbooks: http://serverfault.com/a/416037/176245 – scriptin Jun 02 '13 at 21:08
  • According to https://github.com/opscode-cookbooks/apache2#parameters-3, if you pass the :cookbook option naming the "apache2" cookbook in the call to web_app, it will search for the template in that cookbook. `web_app 'my_site', cookbook: 'apache2' do ... end` – a moose Nov 19 '13 at 23:26
  • @amoose I found I had to add the `cookbook: 'apache2'` within the `do ... end` block; adding as a parameter to `web_app` didn't work. There's [an example in the documentation](https://github.com/viverae-cookbooks/apache2#examples-4) that does this. – Matt Gibson Dec 27 '14 at 14:49
1

It has to go into a recipe of a cookbook that you have to write since it's (apparently? I don't know the Apache cookbook so good) not possible to configure Apache virtual hosts through plain node configuration (chef.json).

cmur2
  • 2,614
  • 1
  • 20
  • 23