Configuration Management with Chef-Solo — Save 50%
A comprehensive guide to get you up and running with Chef-Solo book and ebook.
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:
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:
- Create a new folder in cookbooks:
$ mkdir demoapp
- 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
- 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
- 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:
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:
- Chef Infrastructure [Article]
- Getting started with using Chef [Article]
- Going Beyond the Basics [Article]
|A comprehensive guide to get you up and running with Chef-Solo book and ebook.|
eBook Price: $13.99
Book Price: $22.99
About the Author :
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.