LoginSignup
7
9

More than 5 years have passed since last update.

ChefでNginxをインストールするときにハマった

Posted at

ローカル開発環境をつくろうとしてタイトルどおりうまくいかなくてあれやこれやしたことをメモ。。。

Vagrant + Chef + CentOS6.6 で作成

NginxをインストールするためにCookbookを作成します。

console
$ bundle exec knife cookbook create nginx -o ./site-cookbooks

Chef実践入門を参考にレシピを書いてみます(templateもあわせて用意)

./site-cookbooks/nginx/recipes/default.rb
include_recipe "yum-epel"

package "nginx" do
  action :install
end

service "nginx" do
  action    [ :enable, :start ]
  supports  :status => true, :restart => true, :reload => true
end

template 'nginx.conf' do
  path      '/etc/nginx/nginx.conf'
  source    "nginx.conf.erb"
  owner     'root'
  group     'root'
  mode      0644
  notifies  :reload, "service[nginx]"
end

berksコマンドを実行します。
yum, yum-epelはコミュニティCookbookを使用。

Berksfile
source "https://supermarket.getchef.com"

cookbook "yum"
cookbook "yum-epel"
cookbook "nginx", path: "./site-cookbooks/nginx"
console
$ bundle exec berks vendor ./cookbooks

Vagrantfileの方はこんな感じ

Vagrantfile

  # vagrant-omnibusの有効化
  config.omnibus.chef_version = :latest

  config.vm.provision :chef_solo do |chef|
    chef.log_level = :debug
    chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]
    chef.json = {
      nginx: {
        port: 80
      }
    }
    chef.run_list = %w[
      recipe[yum]
      recipe[yum-epel]
      recipe[nginx]
    ]

  end

Provisionしてみる

console
$ vagrant up --provision

エラーが出てる。。。

console
==> default: [2015-06-16T14:55:35+00:00] INFO: Starting Chef Run for localhost
==> default: [2015-06-16T14:55:35+00:00] INFO: Running start handlers
==> default: [2015-06-16T14:55:35+00:00] INFO: Start handlers complete.
==> default: [2015-06-16T14:55:35+00:00] ERROR: Running exception handlers
==> default: [2015-06-16T14:55:35+00:00] ERROR: Exception handlers complete
==> default: [2015-06-16T14:55:35+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> default: [2015-06-16T14:55:35+00:00] ERROR: Cookbook yum-epel not found. If you're loading yum-epel from another cookbook, make sure you configure the dependency in your metadata
==> default: [2015-06-16T14:55:35+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

依存関係を定義しろと言われているようなのでmetadata.rbにyum-epelへの依存関係を追加します。

./site-cookbooks/nginx/metadata.rb
name             'nginx'
maintainer       'YOUR_COMPANY_NAME'
maintainer_email 'YOUR_EMAIL'
license          'All rights reserved'
description      'Installs/Configures nginx'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version          '0.1.0'
depends          'yum-epel' #追加

再度berksコマンドの実行してからprovisionします。

console
$ bundle exec berks vendor ./cookbooks
$ vagrant provision

まだエラーが出ているけど内容は変わったみたいです。

console
==> default: [2015-06-20T01:23:18+00:00] ERROR: yum_package[nginx] (nginx::default line 21) had an error: Chef::Exceptions::Exec: yum -d0 -e0 -y install nginx-0.8.55-6.el5 returned 1:
==> default: STDOUT:  You could try using --skip-broken to work around the problem
==> default:  You could try running: rpm -Va --nofiles --nodigest
==> default: 
==> default: STDERR: Error: Package: nginx-0.8.55-6.el5.x86_64 (epel)
==> default:            Requires: perl(:MODULE_COMPAT_5.8.8)
==> default: Error: Package: geoipupdate-2.2.1-2.el5.x86_64 (epel)
==> default:            Requires: libcurl.so.3()(64bit)
==> default: [2015-06-20T01:23:18+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

どうやらNginxのバージョンが0.8とか言ってて古い様子。。。
vagrant ssh ゲストOSに入って一応yum-epelがあるか見てみるとあるっぽいので試しにsudo yum install Nginx をしてみるとさっき出たエラーと同じようなものが出ました。いろいろ探してみるとyumにNginxのリポジトリを追加する必要があるみたいです。

Nginxのwikiにインストール方法について丁寧に書かれていたので、それにしたがってrepoファイルを作成して配置してから同じようにsudo yum install Nginxを実行してみるとうまくver.1.8がインストールできました。

repoファイルを用意すればうまくいくっぽいので、repoファイルのひな形をtemplatesに作成し、そのファイルを所定の位置に配置するようにレシピを修正します。

./site-cookbooks/nginx/templates/default/nginx.repo.erb
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
./site-cookbooks/nginx/recipes/default.rb
include_recipe "yum-epel"

# yumにNginxのリポジトリを追加
template 'nginx.repo' do
  path    '/etc/yum.repos.d/nginx.repo'
  source  'nginx.repo.erb'
  mode    0644
  user    'root'
  group   'root'
end

package "nginx" do
  action :install
end

service "nginx" do
  action    [ :enable, :start ]
  supports  :status => true, :restart => true, :reload => true
end

template 'nginx.conf' do
  path      '/etc/nginx/nginx.conf'
  source    "nginx.conf.erb"
  owner     'root'
  group     'root'
  mode      0644
  notifies  :reload, "service[nginx]"
end

一応一旦仮想環境を削除してからやり直してみます。こういうことが気軽にできるのがChefの良い所ですね。

console
$ vagrant destory
$ bundle exec berks vendor ./cookbooks
$ vagrant up --provision

今度はうまくいったみたいです。

7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9