How to create a virtual host with vagrant and chef

2019-04-09 12:55发布

问题:

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?

回答1:

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



回答2:

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).