Chef recipe to deploy Grails war on a new production server

09 / Sep / 2015 by Tarun Saxena 0 comments

Chef, a configuration management tool not only brings a lot of functionality to DevOps but can also ease the process of multiple deployments at the same time. A major use case of Chef is to deploy the latest code on your old as well as new production servers. Chef, with its enormously great functionalities, can even replace continuous integration tools like Jenkins for automated deployments.

To have a basic understanding of Chef, you can refer to this blog. Below is an explanation of a Chef recipe which do the following tasks step by step on a new production server:-

  1. Update the system libraries
  2. Installing Nginx web server
  3. Installing Tomcat7 application server
  4. Copy and replace the default Nginx virtual host file with self-made Nginx virtual host file (to proxy_pass to tomcat7)
  5. Copy and deploy the new war file in tomcat7 webapps folder from Chef-server to node
  6. Restart tomcat and Nginx service

 Note :- You can modify the chef recipe just to deploy the war file in tomcat followed by restarting tomcat server. I am assuming that the production server is new and so all the utilities have to be installed from the scratch.

 Step 1:- Creating cookbook sample_war_deployment

 knife cookbook create sample_war_deployment

 Step 2:- Copying required files in cookbook directory

 As we need to transfer and replace two files from chef-server to the node, so both of them have to be present in the files/default directory of the cookbook. So, I am creating a simple Nginx virtual host file to proxy_pass to tomcat7 whose path will be mentioned in my recipe as the source to Nginx default cookbook file which needs to be replaced.

listen 80;

server_name localhost;
             location / {
                          proxy_pass http://localhost:8080;


We also need to copy our sample.war file in this directory and rename it as ROOT.war.

Step 3: – Creating recipe by editing default.rb  


###updating libraries###
execute "apt-get-update" do
command "apt-get update"
ignore_failure true

###installing nginx###
package ‘nginx’ do
action :install

###installing tomcat7###
package ‘tomcat7’ do
action :install

###replacing nginx default vhost file by vhost file in our cookbook files/default directory###
cookbook_file "/etc/nginx/sites-enabled/default" do
source "default"
mode "0644"
notifies :restart, "service[nginx]"

###Clearing tomcat7 webapps ROOT folder###
bash ‘Clearing tomcat7 webapps ROOT folder’ do
user ‘root’
cwd ‘/home/ubuntu’
code <<-EOH
sudo rm -rf /var/lib/tomcat7/webapps/ROOT

###copying and replacing existing ROOT.war with new ROOT.war in our cookbook files/default directory###
cookbook_file "/var/lib/tomcat7/webapps/ROOT.war" do
source "ROOT.war"
mode "0644"
notifies :restart, "service[nginx]"
notifies :restart, "service[tomcat7]"

###restarting tomcat7 service###
service ‘tomcat7’ do
supports :restart => true

###restarting nginx web server###
service ‘nginx’ do
supports :restart => true
supports :reload =>true



Step 4: – Uploading cookbook on to the Chef Server and adding recipe into the node’s run list

 knife cookbook upload sample_war_deployment
knife node run_list add chef-node ‘recipe[sample_war_deployment]’

 Step 5: – Running Chef-client on the node

Execute the recipe on the node by typing the below command:-
sudo chef-client

After the successful execution of the recipe, you will find all the required services installed and running in an appropriate manner on the server. When you hit your IP address, you will find your war file deployed successfully on tomcat with Nginx doing the proxy pass work.

 Chef is a tool to carry out automated deployment tasks in an easy way. You can also deploy same war file simultaneously on multiple nodes to take deployments to a next level.


Leave a Reply

Your email address will not be published. Required fields are marked *