Chef Solo Basics : Installing Nginx Webserver

Objective


To learn how to use Chef Solo to install nginx webserver on VM using Vagrant 1.7.2, Chef 12.3 and VirtualBox 4.3.28.

Steps


Step 1

$gem install chef

Step 2

$mkdir cengine
$cd cengine
$mkdir cookbooks

Step 3

$knife cookbook create nginx -o cookbooks

Step 4

Edit recipes/default.rb:

package 'nginx' do
  action :install
end

Step 5

$vagrant init

Step 6

Copy URL for Ubuntu 14:04 from www.vgrantbox.es and configure it in Vagrantfile.

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu-14.04"
  config.vm.box_url = 'https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box'

end

Step 7

Add chef solo configuration for installing nginx:

Vagrant.configure(2) do |config|

  config.vm.provision :chef_solo do |chef|
    chef.run_list = [
      "recipe[nginx]"
    ]
  end

end

Step 8

The Vagrantfile now looks like this:

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu-14.04"
  config.vm.box_url = 'https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box'

  config.vm.provision :chef_solo do |chef|
    chef.run_list = [
      "recipe[nginx]"
    ]
  end

end

SSH into VM and check the nginx installation.

$vagrant ssh
vagrant@vagrant-ubuntu-trusty-64:~$ nginx -v

You will see:

nginx version: nginx/1.4.6 (Ubuntu)

Exit the VM.

vagrant@vagrant-ubuntu-trusty-64:~$exit

Step 9

Add:

execute 'apt-get update' do
  action :run
end

to nginx/recipes/default.rb. The file now looks like this:

execute 'apt-get update' do
  action :run
end

package 'nginx' do
  action :install
end

Step 10

Let's provision the VM to apply the changes to the recipe.

$vagrant provision

Step 11

Verify the installation.

$vagrant ssh
$nginx -v
$exit

Summary


In this article, we installed nginx webserver on VM using Chef Solo using Vagrant and VirtualBox.


Related Articles


Create your own user feedback survey