Installing LAMP stack through Chef

31 / Mar / 2014 by ankush 4 comments

Chef as we all know is a Configuration Management Tool. It enables Infrastructure as Code.

Recently I had to give a technical presentation on Chef in my current organization which helped me to get some insights into how Chef works & how it eases the deployment process.

In this article I would touch upon very basic entities associated with Chef & how to deploy a complete LAMP stack on Chef-solo.

Chef-solo behaves as a standalone system, it is in itself a client & a server.

Knife is the most important part of Chef as this is the command line tool for chef all the deployment from Chef server to Chef client happens via knife. In case we want to launch instances in cloud through Chef server we will make use of knife ec2 for AWS instances.

Without wasting any more time let’s move further with the installation steps. For this example I would be Ubuntu 12.04 as the OS

Installing Chef

We can download chef-solo from github.

[shell]root@ankush# curl -L https://www.opscode.com/chef/install.sh | bash

root@ankush# chef-solo -v

root@ankush# wget http://github.com/opscode/chef-repo/tarball/master

root@ankush# tar -zxf master

root@ankush# mv opscode-chef-repo* chef-repo

root@ankush# rm master

root@ankush# cd chef-repo

root@ankush# ls[/shell]

Now we would specify the cookbook path under .chef directory in knife.rb file.

[shell]root@ankush:~/chef-repo# mkdir .chef

root@ankush:~/chef-repo# echo "cookbook_path [ ‘/root/chef-repo/cookbooks’ ]" > .chef/knife.rb[/shell]

Create any cookbook via knife e.g phpapp

[shell]root@ankush:~/chef-repo# knife cookbook create phpapp

root@ankush:~/chef-repo# cd cookbooks/phpapp

root@ankush:~/chef-repo/cookbooks/phpapp# ls

root@ankush:~/chef-repo/cookbooks/phpapp# cd ..[/shell]

Installing Apache

[shell]root@ankush:~/chef-repo/cookbooks# knife cookbook site download apache2

root@ankush:~/chef-repo/cookbooks# tar zxf apache2*

root@ankush:~/chef-repo/cookbooks# rm apache2*.tar.gz

root@ankush:~/chef-repo/cookbooks# knife cookbook site download apt

root@ankush:~/chef-repo/cookbooks# tar zxf apt*

root@ankush:~/chef-repo/cookbooks# rm apt*.tar.gzbelow line

root@ankush:~/chef-repo/cookbooks# knife cookbook site download iptables

root@ankush:~/chef-repo/cookbooks# tar zxf iptables*

root@ankush:~/chef-repo/cookbooks# rm iptables*.tar.gz

root@ankush:~/chef-repo/cookbooks# knife cookbook site download logrotate

root@ankush:~/chef-repo/cookbooks# tar zxf logrotate*

root@ankush:~/chef-repo/cookbooks# rm logrotate*.tar.gz

root@ankush:~/chef-repo/cookbooks# knife cookbook site download pacman

root@ankush:~/chef-repo/cookbooks# tar zxf pacman*

root@ankush:~/chef-repo/cookbooks# rm pacman*.tar.gz

root@ankush:~/chef-repo/cookbooks# cd phpapp[/shell]

Open metadata.rb , add below line

depends “apache2”

Open recipes/default.rb , add below lines

include_recipe “apache2”

 apache_site “default” do

  enable true

end

[shell]root@ankush:~/chef-repo/cookbooks/phpapp# cd ../..[/shell]

Create a new file called solo.rb in your text editor & make below entries

file_cache_path “/root/chef-solo”

cookbook_path “/root/chef-repo/cookbooks”

This file configures chef-solo, telling it where to keep its cache of files and where our cookbooks are.

Now create a file called web.json & add below lines

{

“run_list”: [ “recipe[apt]”, “recipe[phpapp]” ]

}

Why have we not included the apt cookbook inside our recipe as we did with the apache2 cookbook? It’s because our PHP application requires Apache to function but we don’t necessarily want to tie our cookbook to platforms that only support apt.

[shell]root@ankush#chef-solo -c solo.rb -j web.json[/shell]

Now open the page in browser it will show default page for apache.

Installing mysql

[shell]root@ankush:~/chef-repo# cd cookbooks

root@ankush:~/chef-repo/cookbooks# knife cookbook site download mysql

root@ankush:~/chef-repo/cookbooks# tar zxf mysql*

root@ankush:~/chef-repo/cookbooks# rm mysql-*.tar.gz

root@ankush:~/chef-repo/cookbooks# cd mysql/recipes/

root@ankush:~/chef-repo/cookbooks/mysql/recipes# ls

root@ankush:~/chef-repo/cookbooks/mysql/recipes# cd ../../phpapp[/shell]

Open metadata.rb, add below lines

depends “mysql”

Open recipes/default.rb , add below lines

include_recipe “apache2”

include_recipe “mysql::client” —- This to be added

include_recipe “mysql::server”   —- This to be added

apache_site “default” do

  enable true

end

Run chef-solo again

[shell]root@ankush:~/chef-repo/cookbooks/phpapp# cd ../..

root@ankush:~/chef-repo# chef-solo -c solo.rb -j web.json[/shell]

You will now get errors like

FATAL: Stacktrace dumped to /root/chef-solo/chef-stacktrace.out

FATAL: Chef::Exceptions::CookbookNotFound: Cookbook build-essential not found. If you’re loading build-essential from another cookbook, make sure you configure the dependency in your metadata

Open cookbooks/mysql/metadata.rb & specify the dependencies

depends “openssl”

depends “build-essential”

[shell]root@ankush:~/chef-repo# cd cookbooks

root@ankush:~/chef-repo/cookbooks# knife cookbook site download openssl

root@ankush:~/chef-repo/cookbooks# tar zxf openssl*.tar.gz

root@ankush:~/chef-repo/cookbooks# rm openssl*.tar.gz

root@ankush:~/chef-repo/cookbooks# knife cookbook site download build-essential

root@ankush:~/chef-repo/cookbooks# tar zxf build-essential-*.tar.gz>

root@ankush:~/chef-repo/cookbooks# rm build-essential-*.tar.gz[/shell]

Now we’ve fulfilled those dependencies let’s try and re-run chef-solo!

[shell]root@ankush:~/chef-repo/cookbooks# cd ..

root@ankush:~/chef-repo# chef-solo -c solo.rb -j web.json[/shell]

Again we have an ERROR

FATAL: You must set node[‘mysql’][‘server_debian_password’], node[‘mysql’][‘server_root_password’], node[‘mysql’][‘server_repl_password’] in chef-solo mode. For more information, see https://github.com/opscode-cookbooks/mysql#chef-solo-note

Open web.json to specify the mysql root password

{

“mysql”: {“server_root_password”: “ankush”, “server_debian_password”: “ankush”, “server_repl_password”: “ankush”},

“run_list”: [ “recipe[apt]”, “recipe[phpapp]” ]

}

[shell]root@ankush:~/chef-repo# chef-solo -c solo.rb -j web.json[/shell]

Now to install PHP

[shell]root@ankush:~/chef-repo# cd cookbooks/

root@ankush:~/chef-repo/cookbooks# knife cookbook site download php

root@ankush:~/chef-repo/cookbooks# tar zxf php*.tar.gz

root@ankush:~/chef-repo/cookbooks# rm php*.tar.gz[/shell]

The php cookbook depends on the xml, yum-epel, windows, and iis cookbooks, so we’ll need those even though we won’t be using all of them. We’ll also have to install sub-dependencies yum (a dependency of yum-epel), chef_handler, and powershell (dependencies of windows).

[shell]root@ankush:~/chef-repo/cookbooks# knife cookbook site download xml

root@ankush:~/chef-repo/cookbooks# tar zxf xml-*.tar.gz

root@ankush:~/chef-repo/cookbooks# knife cookbook site download yum

root@ankush:~/chef-repo/cookbooks# tar zxf yum-*.tar.gz

root@ankush:~/chef-repo/cookbooks# knife cookbook site download yum-epel

root@ankush:~/chef-repo/cookbooks# tar zxf yum-epel-*.tar.gz

root@ankush:~/chef-repo/cookbooks# knife cookbook site download powershell

root@ankush:~/chef-repo/cookbooks# tar zxf powershell-*.tar.gz

root@ankush:~/chef-repo/cookbooks# knife cookbook site download iis

root@ankush:~/chef-repo/cookbooks# tar zxf iis-*.tar.gz

root@ankush:~/chef-repo/cookbooks# rm *.tar.gz[/shell]

Let’s use the php cookbook in our cookbook.

[shell]root@ankush:~/chef-repo/cookbooks# cd phpapp[/shell]

Open metadata.rb add below line

depends “php”

Open recipes/default.rb , add below lines

include_recipe “php”

include_recipe “php::module_mysql”

include_recipe “apache2::mod_php5”

Save the file and we’re good to run chef-solo again to install all of those things

[shell]root@ankush:~/chef-repo/cookbooks/phpapp# cd ../..

root@ankush:~/chef-repo# chef-solo -c solo.rb -j web.json[/shell]

So that’s PHP installed. Let’s confirm that by creating a test page. Open /var/www/test.php in your editor.

<?php phpinfo(); ?>

Now goto http://yourserver/test.php , you will see the phpinfo page.

FOUND THIS USEFUL? SHARE IT

comments (4)

  1. sonali

    Hi
    I tried to download mysql cookbook but in that cookbook their is no recipes for mysql.Also I am facing Permission issue in running apache2. Can anybody help me solving this problem

    Reply
  2. niecmiacych

    My spouse and I stumbled over here by a different web address and thought I may as well check things out. I like what I see so i am just following you. Look forward to going over your web page yet again.

    Reply
  3. sniper elite

    Hi there, You’ve done an excellent job. I will definitely digg
    it and personally suggest to my friends. I’m confident they will be benefited from this web
    site.

    Reply

Leave a Reply to sonali Cancel reply

Your email address will not be published. Required fields are marked *