Setting Up a Development Environment

(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, 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

  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 $ 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, 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:

Further resources on this subject:

You've been reading an excerpt of:

Configuration Management with Chef-Solo

Explore Title