Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable default nginx site when using Chef and Vagrant?

I am using Berkshelf, Chef and Vagrant and I am trying to configure a custom site running on nginx. I am using the opscode nginx recipe and then writing my own recipe for the custom site. When I run vagrant up I get the an error about not disabling the default nginx site. I've found several different suggestions but nothing seems to be working.

The error:

STDOUT:
STDERR: nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-    enabled/kyleboon.me:2
nginx: configuration file /etc/nginx/nginx.conf test failed

My Berksfile:

site :opscode

metadata
cookbook 'nginx'

The 'roles/web.json' role I defined:

{
  "name": "web",
  "chef_type": "role",
  "json_class": "Chef::Role",
  "description": "The base role for systems that serve HTTP traffic",
  "default_attributes": {
    "nginx": {
      "default_site_enabled": false
    },
    "app": {
      "name": "kyleboon.me",
      "web_dir": "/var/data/www/kyleboon.me"
    },
    "user":{
      "name": "vagrant"
    }
  },
  "run_list": [
    "recipe[nginx]",
    "recipe[kyleboon.me]"
  ]
}

Here is the recipes/default/default.rb for the nginx site I am adding:

nginx_site 'default' do
  action :disable
end

%w(public logs).each do |dir|
  directory "#{node.app.web_dir}/#{dir}" do
    owner node.user.name
    mode "0755"
    recursive true
  end
end

template "#{node.nginx.dir}/sites-available/kyleboon.me" do
  source "site.erb"
  mode 0777
  owner node.nginx.user
  group node.nginx.user
end

nginx_site "kyleboon.me"

cookbook_file "#{node.app.web_dir}/public/index.html" do
  source "index.html"
  mode 0755
  owner node.user.name
end

(P.S. I know the file permissions need to be changed, I've just been fiddling with lots of things, I'll update those once I get everything else working)

And here is attributes/default.rb:

override['nginx']['enable_default_site'] = false

You can see I have tried to disable the default site in the web.json, the attributes and the recipe itself but none of them stick.

I don't have a node or solo node defined and I'm not sure if that's an issue. My main problem with vagrant so far has been the endless possibilities for how to do things. No two examples are done in the same way and I'm not sure what is considered the 'best' or 'right' way to go.

like image 309
Kyle Boon Avatar asked Jun 16 '13 13:06

Kyle Boon


2 Answers

You can disable any nginx site by name using the nginx_site and its name. The problem you are having is because the nginx_site definition is actually looking for the enable parameter to be set to true or false not the action parameter set to :disabled.

To disable the default site add to your recipe:

nginx_site 'default' do
  enable false
end

This is working for me as of version 1.7.1 of the nginx opscode cookbook. I dont know if this will work with the version provided by community as it appears to be several months old.

To get the latest version add to your Berksfile:

cookbook 'nginx', git: 'https://github.com/opscode-cookbooks/nginx'

Hope that helps :)

like image 94
Cliff Erson Avatar answered Oct 04 '22 06:10

Cliff Erson


I'm getting the same error, but I don't have the default site enabled. Instead, it's coming from /etc/nginx/conf.d/default.conf! I had to resort to

file "/etc/nginx/conf.d/default.conf" do
  action :delete
end

This is coming from the RHEL package that the nginx cookbook is installing on my box.

like image 25
jasongarber Avatar answered Oct 04 '22 06:10

jasongarber