How to setup a Cookbook to configure Apache Webserver

How to setup a Cookbook to configure Apache Webserver

Purpose – To create a Cookbook for Configuring Apache Webserver using Chef

Step1: Generate a Cookbook
[user@host-1 chefspace]$ mkdir cookbooks
[user@host-1 chefspace]$ cd cookbooks/
[user@host-1 cookbooks]$ chef generate cookbook apache_httpd
Generating cookbook apache_httpd
– Ensuring correct cookbook file content
– Ensuring delivery configuration
– Ensuring correct delivery build cookbook content
Your cookbook is ready. Type `cd apache_httpd` to enter it.
There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local –help` to see a full list.
Why not start by writing a test? Tests for the default recipe are stored at:
test/recipes/default_test.rb
If you’d prefer to dive right in, the default recipe can be found at:
recipes/default.rb
[user@host-1 cookbooks]$ tree apache_httpd/
apache_httpd/
|– Berksfile
|– chefignore
|– metadata.rb
|– README.md
|– recipes
|   `– default.rb
|– spec
|   |– spec_helper.rb
|   `– unit
|       `– recipes
|           `– default_spec.rb
`– test
    `– recipes
        `– default_test.rb
6 directories, 8 files
Step2: Generate template for HTML file
[user@host-1 cookbooks]$ chef generate template apache_httpd index.html
Recipe: code_generator::template
  * directory[./apache_httpd/templates/default] action create
    – create new directory ./apache_httpd/templates/default
    – restore selinux security context
  * template[./apache_httpd/templates/index.html.erb] action create
    – create new file ./apache_httpd/templates/index.html.erb
    – update content in file ./apache_httpd/templates/index.html.erb from none to e3b0c4
    (diff output suppressed by config)
    – restore selinux security context
[user@host-1 cookbooks]$ chef generate template apache_httpd index.html
Recipe: code_generator::template
  * directory[./apache_httpd/templates/default] action create
    – create new directory ./apache_httpd/templates/default
    – restore selinux security context
  * template[./apache_httpd/templates/index.html.erb] action create
    – create new file ./apache_httpd/templates/index.html.erb
    – update content in file ./apache_httpd/templates/index.html.erb from none to e3b0c4
    (diff output suppressed by config)
    – restore selinux security context
Step3: Update the ‘ index.html.erb ‘ with the webpage content
[user@host-1 templates]$ pwd
/home/user/chefspace/cookbooks/apache_httpd/templates
[user@host-1 templates]$ vi index.html.erb
[user@host-1 templates]$ cat index.html.erb
<html>
                <head>
                <title> This is simple webpage </title>
                </head>
                <body>
                <h1> This is a grooming website from Cookbook </h1>
                </body>
</html>
Step4: Update the default.rb recipe with the apache webserver configuration setup
[user@host-1 recipes]$ pwd
/home/user/chefspace/cookbooks/apache_httpd/recipes
[user@host-1 recipes]$ cat default.rb
#
# Cookbook Name:: apache_httpd
# Recipe:: default
#
# Copyright (c) 2017 The Authors, All Rights Reserved.
package ‘Install Apache’ do
                case node[:platform]
                when ‘redhat’,’centos’
                                package_name ‘httpd’
                                action    :install
                when ‘ubuntu’,’debian’
                                package_name ‘apache2’
                                action :install
                end
end
service ‘httpd’ do
                action [:enable, :start]
end
template ‘/var/www/html/index.html’ do
                source ‘index.html.erb’
end
Step5: Run the Cookbook using chef-client
[user@host-1 recipes]$ sudo chef-client –local-mode –runlist ‘recipe[apache_httpd]’
[2017-06-03T03:04:30-07:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: [“apache_httpd”]
Synchronizing Cookbooks:
  – apache_httpd (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks…
Converging 3 resources
Recipe: apache_httpd::default
  * yum_package[Install Apache] action install (up to date)
  * service[httpd] action enable (up to date)
  * service[httpd] action start (up to date)
  * template[/var/www/html/index.html] action create
    – update content in file /var/www/html/index.html from eea344 to 5ff101
    — /var/www/html/index.html             2017-06-02 20:05:07.795943412 -0700
    +++ /var/www/html/.chef-index.html20170603-3853-wyjz1z 2017-06-03 03:05:11.707685428 -0700
    @@ -3,7 +3,7 @@
                <title> This is simple webpage </title>
                </head>
                <body>
    –          <h1> This is a grooming website </h1>
    +         <h1> This is a grooming website from Cookbook </h1>
                </body>
     </html>
    – restore selinux security context
Running handlers:
Running handlers complete
Chef Client finished, 1/4 resources updated in 41 seconds
Step6: Test the webpage
[user@host-1 recipes]$ curl http://localhost/index.html
<html>
                <head>
                <title> This is simple webpage </title>
                </head>
                <body>
                <h1> This is a grooming website from Cookbook </h1>
                </body>
</html>

Hope you enjoyed reading this article. Thank you.