How to setup a simple Apache HTTP server using Chef Recipe
Here in this article we will try to setup a basic Apache HTTP server using chef recipe and deploy a html file and validate it.
Test Environment
Fedora Server
Chef DK
Procedure
Step1: Install apache httpd package
As a first step we will create chef recipe file which will be used to install httpd package or apache2 package based on the operating system wherein this recipe is executed.
[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
Now let’s execute the chef recipe to install the package locally on the server.
[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
Here we will update our recipe to enable and start the httpd service when the chef recipe is executed.
[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
Now let’s execute the chef recipe again to ensure the package is installed locally and the httpd service is started.
[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
Now, we will update our recipe to include additional step to create a sample html file on the server as shown below.
[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
Now let’s rerun the chef recipe and validate the results.
[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
Let’s try to access the html page that we just created using the chef recipe as shown below.
[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