Carrried on from CHEF install PHP on AWS EC2 Ubuntu

Now if you were setting up a server by hand, this is where you’d manually setup a database, copy the web application code over, create a MySQL user for the website and configure virtual hosts. Instead, we’ll use Chef to automate the setup of our application. This allows us to set up multiple servers and know we will always get the same results.

Before we setup our database we need to fetch a final few cookbooks. The database cookbook provides resources that allow us to easily manage databases and database users. The database cookbook depends on the postgresql, mariadb, xfs and aws cookbooks so we’ll need those as well even though we won’t be using them. We’ll also fetch the mysql2_chef_gem cookbook which the database cookbook requires when used with MySQL.

cd /root/chef-repo/cookbooks
knife cookbook site download database
tar zxf database-*.tar.gz
knife cookbook site download postgresql
tar zxf postgresql-*.tar.gz
knife cookbook site download xfs
tar zxf xfs-*.tar.gz
knife cookbook site download mariadb
tar mariadb xfs-*.tar.gz
knife cookbook site download aws
tar zxf aws-*.tar.gz
knife cookbook site download mysql2_chef_gem 0.1.0
tar zxf mysql2_chef_gem-*.tar.gz
root@intro:~/chef-repo/cookbooks# rm *.tar.gz

cd phpapp
vi metadata.rb # and add the following 2 lines
depends “database”
depends “mysql2_chef_gem”

To setup our web application we need to:

  1. Create a MySQL database for our application
  1. Create a MySQL database user for our application
  1. Fetch the code for our web application
  1. Create a configuration file with database details and other configuration options for our web application
  1. Create an Apache VirtualHost for our web application

A resource is an action that your recipe can perform. The template resource creates a file by expanding variables in a template. The user resource can be used to manage users. The database cookbook provides the resource mysql_database which we will now use to perform the first step.

vi recipes/default.rb
#
# Cookbook Name:: phpapp
# Recipe:: default
#
# Copyright 2013, YOUR_COMPANY_NAME
#
# All rights reserved – Do Not Redistribute
#
include_recipe “apache2”
include_recipe “mysql::client”
include_recipe “mysql::server”
include_recipe “php”
include_recipe “php::module_mysql”
include_recipe “apache2::mod_php5”
include_recipe “mysql::ruby”
include_recipe “mysql2_chef_gem”
apache_site “default” do
enable true
end
mysql_database node[‘phpapp’][‘database’] do
connection ({:host => ‘localhost’, :username => ‘root’, :password => node[‘mysql’][‘server_root_password’]})
action :create
end

The line “mysql_database node[‘phpapp’][‘database’] do ” allows us to define the database name later because we have also included the mysql2_chef_gem recipe which installs a Ruby library (called a “gem”) that allows Chef to interact with a MySQL server.

Attributes

The database attribute can be defined in a few places. We could define the attribute in web.json like we did with the MySQL ones but that makes our cookbook unnecessarily difficult to use. We want to provide the option to use a database called something other than phpapp but we should really provide a default.

vi cookbooks/phpapp/attributes/default.rb # and add the line

default[“phpapp”][“database”] = “phpapp”

Create the Database

cd ../..

chef-solo -c solo.rb -j web.json

Create the Database User

cd cookbooks/phpapp

vi recipes/default.rb # append the lines
mysql_database_user node[‘phpapp’][‘db_username’] do
connection ({:host => ‘localhost’, :username => ‘root’, :password => node[‘mysql’][‘server_root_password’]})
password node[‘phpapp’][‘db_password’]
database_name node[‘phpapp’][‘database’]
privileges [:select,:update,:insert,:create,:delete]
action :grant
end
vi attrributes/default.rb # Add the line
default[“phpapp”][“db_username”] = “phpapp”
cd ../..
vi web.json # add the following

{
“mysql”: {“server_root_password”: “808052769e2c6d909027a2905b224bad”, “server_debian_password”: “569d1ed2d46870cc020fa87be83af98d”, “server_repl_password”: “476911180ee92a2ee5a471f33340f6f4”},
“phpapp”: {“db_password”: “212b09752d173876a84d374333ae1ffe”},
“run_list”: [ “recipe[apt]”, “recipe[phpapp]” ]
}

chef-solo -c solo.rb -j web.json

MySQL is now up and running, try:

1) mysql -p

808052769e2c6d909027a2905b224bad

2) mysql -u phpapp

212b09752d173876a84d374333ae1ffe

Both get to the mysql database prompt.

Next Page

CHEF install WordPress on AWS EC2 Ubuntu