How to setup a simple Apache HTTP server using chef-client

How to setup a simple Apache HTTP server using chef-client

Purpose – Here we are going to setup a simple webserver using chef recipe and test the functionality

Pre-requisites
Chef DK already installed

Step1: Install apache httpd package

[user@host-1 chefspace]$ cat host-1_webserver.rb
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
[user@host-1 chefspace]$ sudo chef-client –local-mode host-1_webserver.rb
[2017-06-02T03:42:24-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T03:42:24-07:00] WARN: No cookbooks directory found at or above current directory.  Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks…
[2017-06-02T03:42:32-07:00] WARN: Node host-1 has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_webserver.rb
  * yum_package[Install Apache] action install
– install version 2.4.6-45.el7.centos.4 of package httpd
Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 01 minutes 25 seconds
[user@host-1 chefspace]$
[user@host-1 chefspace]$ sudo chef-client –local-mode host-1_webserver.rb
[2017-06-02T03:47:37-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T03:47:37-07:00] WARN: No cookbooks directory found at or above current directory.  Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks…
[2017-06-02T03:47:43-07:00] WARN: Node host-1 has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_webserver.rb
* yum_package[Install Apache] action install (up to date)
Running handlers:
Running handlers complete
Chef Client finished, 0/1 resources updated in 16 seconds
Step2: Enable and Start the httpd service
[user@host-1 chefspace]$ cat host-1_webserver.rb
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
[user@host-1 chefspace]$ sudo chef-client –local-mode host-1_webserver.rb
[2017-06-02T03:55:06-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T03:55:06-07:00] WARN: No cookbooks directory found at or above current directory.  Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks…
[2017-06-02T03:55:10-07:00] WARN: Node host-1 has an empty run list.
Converging 2 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_webserver.rb
  * yum_package[Install Apache] action install (up to date)
* service[httpd] action enable
    – enable service service[httpd]
  * service[httpd] action start
    – start service service[httpd]
Running handlers:
Running handlers complete
Chef Client finished, 2/3 resources updated in 09 seconds
[user@host-1 chefspace]$ sudo chef-client –local-mode host-1_webserver.rb
[2017-06-02T19:59:05-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T19:59:05-07:00] WARN: No cookbooks directory found at or above current directory.  Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks…
[2017-06-02T19:59:09-07:00] WARN: Node host-1.user.example has an empty run list.
Converging 2 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_webserver.rb
  * yum_package[Install Apache] action install (up to date)
  * service[httpd] action enable (up to date)
  * service[httpd] action start (up to date)
Running handlers:
Running handlers complete
Chef Client finished, 0/3 resources updated in 08 seconds
Step3: Create a webpage and test the functionality
[user@host-1 chefspace]$ cat host-1_webserver.rb
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
file ‘/var/www/html/index.html’ do
                content ‘<html>
                <head>
                <title> This is simple webpage </title>
                </head>
                <body>
                <h1> This is a grooming website </h1>
                </body>
</html>’
end
[user@host-1 chefspace]$ sudo chef-client –local-mode host-1_webserver.rb
[2017-06-02T20:04:49-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T20:04:49-07:00] WARN: No cookbooks directory found at or above current directory.  Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks…
[2017-06-02T20:04:57-07:00] WARN: Node host-1.user.example has an empty run list.
Converging 3 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_webserver.rb
  * yum_package[Install Apache] action install (up to date)
  * service[httpd] action enable (up to date)
  * service[httpd] action start (up to date)
  * file[/var/www/html/index.html] action create
    – create new file /var/www/html/index.html
    – update content in file /var/www/html/index.html from none to eea344
    — /var/www/html/index.html             2017-06-02 20:05:07.796943325 -0700
    +++ /var/www/html/.chef-index.html20170602-7309-1ijmzto                2017-06-02 20:05:07.795943412 -0700
    @@ -1 +1,9 @@
    +<html>
    +         <head>
    +         <title> This is simple webpage </title>
    +         </head>
    +         <body>
    +         <h1> This is a grooming website </h1>
    +         </body>
    +</html>
    – restore selinux security context
Running handlers:
Running handlers complete
Chef Client finished, 1/4 resources updated in 17 seconds
[user@host-1 chefspace]$
[user@host-1 chefspace]$ sudo chef-client –local-mode host-1_webserver.rb
[2017-06-02T20:05:35-07:00] WARN: No config file found or specified on command line, using command line options.
[2017-06-02T20:05:35-07:00] WARN: No cookbooks directory found at or above current directory.  Assuming /home/user/chefspace.
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks…
[2017-06-02T20:05:41-07:00] WARN: Node host-1.user.example has an empty run list.
Converging 3 resources
Recipe: @recipe_files::/home/user/chefspace/host-1_webserver.rb
  * yum_package[Install Apache] action install (up to date)
  * service[httpd] action enable (up to date)
  * service[httpd] action start (up to date)
  * file[/var/www/html/index.html] action create (up to date)
Running handlers:
Running handlers complete
Chef Client finished, 0/4 resources updated in 16 seconds
Step4: Test the webpage
[user@host-1 chefspace]$ curl http://localhost/index.html
<html>
                <head>
                <title> This is simple webpage </title>
                </head>
                <body>
                <h1> This is a grooming website </h1>
                </body>

</html>

Hope you enjoyed reading this article. Thank you.

1 COMMENT

comments user
DedicatedHosting4u

The article has genuinely peaks my interest. I’m going to bookmark your web page and maintain checking for new details. I really loved reading your blog, valuable information.

Offshore dedicated