Installing Zend Server and MongoDB using Chef and Vagrant

I am in my last two weeks at my current job, in this time frame I will be helping my colleague (Ingram) make sure he is prepared for my departure.

When ever I have used Vagrant I have used Puppet for provisioning but Ingram has other ideas and prefers Chef (he hasn’t use either before but has done some researching), I haven’t tried Chef before so thought it’s worth a try.

I am now a convert, I have nothing against Puppet, it does a perfectly good job but its declarative nature isn’t something i’m used to. Chef on the other hand using Ruby and its own Ruby like syntax which I am more comfortable with.

While writing this post I acted out each of the steps and zipped it all up for your consumption.
You can download it from http://www.snatchfrigate.com/wp-content/uploads/2012/07/example-vagrant-chef-zend-mongo.zip.

So kids, for this lesson I will cover the same ground as I did with the Puppet, Zend Server and MongoDB post but using Chef in place of Puppet.

I will be using Ubuntu for this example.

About Chef

Chef is an open-source systems integration framework. It’s been designed specifically for use in the cloud, it’s also very useful for provisioning development environments which we will be doing shortly.

Preparation

Lets create the file structure of our environment now so we know where everything goes the structure is as follows

Vagrant creates “Vagrantfile” on “vagrant init” in your current directory. We want to keep anything Vagrant or Chef related away from your application, this is why we created “projects/myproject/vagrant” and “cd” into it before running “vagrant init”.

projects/myproject/vagrant/Vagrantfile defines and configures our virtual machine(s) from a hardware perspective. For a better idea of what you can do with this file check out Vagrants documentation.

projects/myproject/vagrant/chef/roles/server.rb tells Cheff which recipes to run

projects/myproject/vagrant/chef/cookbooks/my_cookbook/recipes/server.rb is where the magic happens! You normally break the environment down into small recipes for each part of the environment.

Consider this, in your production environment you have two servers, one being your web server and another for your database server, you could create two recipes one for each server or you could separate your recipes down to package level.

Because you’ve made the effort creating these recipes you can mix them up via a role file.

Let’s say you want to virtualise your production environment for development, rather than virtualising the entire stack you could simply include all the packages on one VM simply by creating a new role file that ran all the recipes.

projects/myproject/vagrant/chef/cookbooks/my_cookbook/files/default/app.conf contains an Apache VirtualHost which has document root pointing to our “app” folder so you can have a play around with PHP and MongoDB after we have the environment up and running.

We will be creating one recipe for our example

Configuring our environment

projects/myproject/vagrant/chef/cookbooks/my_cookbook/recipes/server.rb is responsible for installing packages, starting services, creating users and so on. We could use other pre-existing cookbooks to achieve this but that won’t help you grasp what is going on, so we’re doing it the hard way ^^.

Just a quick note, paths can be relative in Vagrantfile which I have used above.

The ruby above is telling Chef to execute the following command providing certain conditions are met. Let’s do the same for the Zend Server repository

I need to go through this process one more time for MongoDB

We have our repositories and their keys installed, we need to update apt so we can install some packages

As you can see our recipe is doing a lot, it’s best to split them up for each package you want to install. For this example using one recipe will do fine.

Before our VM is ready to be booted we must create a role file for it, in this instance our role file is projects/myproject/vagrant/chef/roles/server.rb. The role files job is to specify the recipes to be used and any additional configuration information that may be needed.

Lets start the VM using the following command:

Please be patient, it can take a while to install Zend Server.

Once the VM has booted up and Chef has done its thing, you should be able to open http://10.0.0.2 in your browser and get a response. If you left forwarding for port 80 enabled, you will also be able to get a response from http://127.0.0.1

If you had any problems with Chef you can SSH into the VM and check the log files in more detail. The details you need to SSH into the VM are [email protected] on port 2222 with the password vagrant and in most cases the log file will be located in /tmp/vagrant-chef-1/chef-stacktrace.out. Try to fix the issue then execute the following command to re-run Chef.

Is everything working? Awesome! let’s configure Apache.

I am going to keep this file brief as configuring Apache is outside of this posts scope.

This file is no use to us at its present location, lets update our recipe to put the file somewhere useful.

We are now left with one thing to do in our recipe and that is to ensure our services are running

now re-execute

and we are done

when your finished with the VM you can execute either

That’s you have Zend Server, MongoDB and a VirtualHost ready for you to play with.

Please let me know if you find any issues or have anything to add to this post, it would much appearciated.

One thought on “Installing Zend Server and MongoDB using Chef and Vagrant

Leave a Reply