How To Install Nginx On Ubuntu Using Chef
In this post we show how to install NGINX on Ubuntu using Chef on Windows 10 workstation.
All commands are executed in PowerShell on a Windows workstation.
Before You Begin
Install Chocolatey
If you do not have Chocolatey, you can install it by following the instructions on chocolatey.org. You may also see this post for instructions on how to set the Chocolatey cache location.
Install Chef Development Kit (ChefDK)
In PowerShell as Administrator:
Install VirtualBox and Vagrant
To install VirtualBox and Vagrant, follow the steps described in Ubuntu with Vagrant and VirtualBox on Windows.
Install Vagrant Plugins
vagrant plugin install vagrant-omnibus
vagrant plugin install vagrant-berkshelf
vagrant plugin install vagrant-hostmanager
Create Chef Cookbook
This directory will become the root of your source repository:
Ensure the apt cache is up to date
Reference the apt
Cookbook
Add this line to metadata.rb
:
To get the latest version string, run knife cookbook site show apt
:
chef exec knife cookbook site show apt | grep latest_version
latest_version: https://supermarket.chef.io/api/v1/cookbooks/apt/versions/2.9.2
Here is the complete file:
name 'my_ubuntu_nginx'
maintainer 'The Authors'
maintainer_email 'you@example.com'
license 'all_rights'
description 'Installs/Configures my_ubuntu_nginx'
long_description 'Installs/Configures my_ubuntu_nginx'
version '0.1.0'
depends 'apt', '~> 2.9.2'
Set the apt cookbook's default recipe to run
Add this line to recipes/default.rb
:
Here is the complete file:
#
# Cookbook Name:: my_ubuntu_nginx
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
include_recipe 'apt::default'
Test
This generates Berksfile.lock
file.
Configure NGINX
Reference the nginx cookbook
We need the nginx
cookbook for it's nginx::repo
recipe. Add this line to metadata.rb
:
To get the latest version string, run knife cookbook site show nginx
:
chef exec knife cookbook site show nginx | grep latest_version
latest_version: https://supermarket.chef.io/api/v1/cookbooks/nginx/versions/2.7.6
Here is the complete file:
name 'my_ubuntu_nginx'
maintainer 'The Authors'
maintainer_email 'you@example.com'
license 'all_rights'
description 'Installs/Configures my_ubuntu_nginx'
long_description 'Installs/Configures my_ubuntu_nginx'
version '0.1.0'
depends 'apt', '~> 2.9.2'
depends 'nginx', '~> 2.7.6'
Write the install_nginx recipe
The first step is to create the recipe file, install_nginx.rb
. Run the following command to generate it:
Write out recipes/install_nginx.rb
like this:
#
# Cookbook Name:: my_ubuntu_nginx
# Recipe:: install_nginx
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
# Only needed if you want to install latest stable package from nginx.org
include_recipe 'nginx::repo'
package 'nginx' do
action :install
end
service 'nginx' do
supports status: true, restart: true, reload: true
action :enable
end
Set the install_nginx recipe to run
Add this line to cookbooks/my_web_server/recipes/default.rb
:
Here is the complete file:
#
# Cookbook Name:: my_ubuntu_nginx
# Recipe:: default
#
# Copyright (c) 2016 The Authors, All Rights Reserved.
include_recipe 'apt::default'
include_recipe 'my_ubuntu_nginx::install_nginx'
Update the integration test
Replace the contents of test/integration/default/serverspec/default_spec.rb
with this code:
require 'spec_helper'
describe 'my_ubuntu_nginx::default' do
# Serverspec examples can be found at
# http://serverspec.org/resource_types.html
describe package('nginx') do
it { should be_installed }
end
describe service('nginx') do
it { should be_enabled }
it { should be_running }
end
end
Test
Ruby Lint
Chef Lint
Unit Tests
Install / Update cookbooks:
Create a .rspec
file:
Paste these lines in .rspec
Run:
Integration Tests
Update .kitchen.yml
to include the Ubuntu platform only. This is how it should look:
---
driver:
name: vagrant
network:
- ["private_network", {type: "dhcp"}]
provisioner:
name: chef_zero
platforms:
- name: ubuntu-14.04
suites:
- name: default
run_list:
- recipe[my_ubuntu_nginx::default]
Run the integration tests: