Setting Up a Development Environment

Exclusive offer: get 50% off this eBook here
Configuration Management with Chef-Solo

Configuration Management with Chef-Solo — Save 50%

A comprehensive guide to get you up and running with Chef-Solo book and ebook.

$13.99    $7.00
by Naveed ur Rahman | June 2014 | Networking & Telephony Open Source

This article by Naveed ur Rahman, author of the book Configuration Management with Chef-Solo, will teach you how to create a Hello World recipe.

(For more resources related to this topic, see here.)

Creating a Hello World recipe

Now, our next task is to create a simple PHP project, and when we access http://localhost:8080, it should display our Hello World page.

To do so, we need PHP5 and Apache2.

Now, we can destroy our old box and start a new one with vagrant up. Alternatively, we can create a new box with Apache2 and PHP.

Use the same Vagrantfile and replace Nginx with Apache2.

Note that Apache2 has the following dependencies. Before we start, we need to clone individual cookbooks into our cookbooks folder.

It's a tedious task to clone the repo one by one.

The Apache2 dependencies are as follows:

  • apt
  • iptables
  • logrotate
  • pacman

Change the Nginx connection to Apache2, remove the old box, and create a new box using the following command:

$ vagrant destroy $ vagrant up

The recipes will execute, and we have our new Apache2 box installed. Go to http://127.0.0.1:8080, and we will see Apache2' s page.

Our next step is to create a PHP project, which can be done as follows:

  1. Create a new folder in cookbooks:

    $ mkdir demoapp

  2. The folder structure of the cookbook will look as follows:

    demoapp attributes default.rb files default test.php recipes default.rb metadata.rb README.md

  3. Create the folder structure and create files. The important bit of the cookbook is default.rb:

    $ mkdir attributes $ mkdir templates $ mkdir files $ mkdir recipes $ touch metadata.rb $ touch README.md $ touch attributes/default.rb $ touch recipes/default.rb $ mkdir files/default $ touch files/default/test.php

  4. Now, our folder structure is ready. The next step is to create a recipe. First, we will disable the default Apache site. Add the following code block in the recipes/default.rb file:

    apache_site "default" do enable true end

    In the next step, we will copy the test.php file in the /var/www directory. The following code block will move the test.php file in the /var/www directory:

    cookbook_file "/var/www/test.php" do source "test.php" owner "root" group "root" end

The cookbook_file resource will copy the file into the specified location.

After the completion of the recipe, we need to instruct Vagrant to install our demoapp cookbook:

config.vm.provision "chef_solo" do |chef| chef.cookbooks_path = "../cookbooks" chef.add_recipe "apt" chef.add_recipe "chef-dotdeb" chef.add_recipe "apache2" chef.add_recipe "apache2::mod_php5" chef.add_recipe "demoapp::default" end

Before executing our recipe, we need to set up our environment using apt, dotdeb, and apache2 cookbooks. The mod_php5 recipe is part of the apache2 cookbook, and it will install every dependency needed for PHP. Then, we can execute our recipe to set up our project in a new virtual machine.

Our first recipe is pretty simple and just responsible for copying one file to a specific location. However, for the complete project, this method is hectic and tedious.

After running the recipe, browse to 127.0.0.1:8080/test.php, and you should be able to see the PHP file content, as shown in the following screenshot:

Summary

In this article, we learned how to create a Hello World recipe using Apache2 and PHP5 and how to set up our environment using apt, dotdeb, and apache2 cookbooks.

Resources for Article:


Further resources on this subject:


Configuration Management with Chef-Solo A comprehensive guide to get you up and running with Chef-Solo book and ebook.
Published: June 2014
eBook Price: $13.99
Book Price: $22.99
See more
Select your format and quantity:

About the Author :


Naveed ur Rahman

Naveed ur Rahman is a self-taught programmer and an avid traveler. When he is not experimenting with the latest in programming and deployment, he is out camping and watching cricket.

His adventures in programming began at a very young age when he got introduced to GW-BASIC. Now, he has experience working for one of the biggest tech names in the Middle East.

Having worked at the largest technology company in the Middle East, Naveed has helped teams create and deploy applications written in various languages using configuration management tools.

Books From Packt


Visual SourceSafe 2005 Software Configuration Management in Practice
Visual SourceSafe 2005 Software Configuration Management in Practice

Ansible Configuration Management
Ansible Configuration Management

Learning Chef
Learning Chef

Managing Windows Servers with Chef
Managing Windows Servers with Chef

Chef Infrastructure Automation Cookbook
Chef Infrastructure Automation Cookbook

Instant Chef Starter [Instant]
Instant Chef Starter [Instant]

Creating Development Environments with Vagrant
Creating Development Environments with Vagrant

Fast Data Processing with Spark
Fast Data Processing with Spark


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software