Deploying an Apache Web Server in Chef
In this article,we will see Deploying an Apache Web Server in Chef.
If you want to make server as web server,we need to follow 3 things
- Install web package (httpd).
- Create file and put some web content.
- Start httpd service
we create recipe inside the cookbook and write ruby script belongs to above 3 things then run the recipe.
Once you run the recipe the server became a web server.
Implementation Steps
- Connect to Linux EC2 Instance “workstation” through putty.
- Switch to root user.
- Create a new cookbook “ktexperts-apache-cookbook”.
- Create a new recipe “ktexperts-apache-recipe”.
- Go to cookbooks directory.
- Open the recipe “ktexperts-apache-recipe.rb” and write apache web server script.
- Execute the Recipe/call chef-client.
- Copy public IP and search in browser.
- Modify Recipe “ktexperts-apache-recipe”
- Execute the Recipe/call chef-client.
- Refresh IPV4 Public IP in browser.
Connect to Linux EC2 Instance “workstation” through putty
1 2 3 4 5 6 7 8 9 |
Using username "ec2-user". Authenticating with public key "imported-openssh-key" Last login: Wed Dec 25 05:33:53 2019 from 203.187.233.15 __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ |
Switch to root user
1 2 3 4 |
[ec2-user@ip-172-31-42-243 ~]$ sudo su [root@ip-172-31-42-243 ec2-user]# [root@ip-172-31-42-243 ec2-user]# ls cookbooks nodes |
Create a new cookbook “ktexperts-apache-cookbook”
Go inside the cookbooks
1 2 |
[root@ip-172-31-42-243 ec2-user]# cd cookbooks/ [root@ip-172-31-42-243 cookbooks]# |
create cookbook by using below command
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-243 cookbooks]# chef generate cookbook ktexperts-apache-cookbook Hyphens are discouraged in cookbook names as they may cause problems with custom resources. See https://docs.chef.io/ctl_chef.html#chef-generate-cookbook for more information. Generating cookbook ktexperts-apache-cookbook - Ensuring correct cookbook content Your cookbook is ready. Type `cd ktexperts-apache-cookbook` 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 of local testing commands. Why not start by writing an InSpec test? Tests for the default recipe are stored at: test/integration/default/default_test.rb If you'd prefer to dive right in, the default recipe can be found at: recipes/default.rb |
To see the list of files and directories in a tree structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
[root@ip-172-31-42-243 cookbooks]# tree . ├── ktexperts-apache-cookbook │ ├── CHANGELOG.md │ ├── chefignore │ ├── kitchen.yml │ ├── LICENSE │ ├── metadata.rb │ ├── Policyfile.rb │ ├── README.md │ ├── recipes │ │ └── default.rb │ ├── spec │ │ ├── spec_helper.rb │ │ └── unit │ │ └── recipes │ │ └── default_spec.rb │ └── test │ └── integration │ └── default │ └── default_test.rb └── ktexperts-cookbook ├── CHANGELOG.md ├── chefignore ├── kitchen.yml ├── LICENSE ├── metadata.rb ├── Policyfile.rb ├── README.md ├── recipes │ ├── default.rb │ ├── ktexperts1-recipe.rb │ └── ktexperts-recipe.rb ├── spec │ ├── spec_helper.rb │ └── unit │ └── recipes │ ├── default_spec.rb │ ├── ktexperts1-recipe_spec.rb │ └── ktexperts-recipe_spec.rb └── test └── integration └── default ├── default_test.rb ├── ktexperts1-recipe_test.rb └── ktexperts-recipe_test.rb 16 directories, 28 files |
To see the list of cookbooks
1 2 |
[root@ip-172-31-42-243 cookbooks]# ls ktexperts-apache-cookbook ktexperts-cookbook |
Create a new recipe “ktexperts-apache-recipe”
Go inside the cookbook “ktexperts-apache-cookbook”
1 2 |
[root@ip-172-31-42-243 cookbooks]# cd ktexperts-apache-cookbook/ [root@ip-172-31-42-243 ktexperts-apache-cookbook]# |
using below command to create a new recipe “ktexperts-apache-recipe” inside the cookbook “ktexperts-apache-cookbook”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-243 ktexperts-apache-cookbook]# chef generate recipe ktexperts-apache-recipe Recipe: code_generator::recipe * directory[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/unit/recipes] action create (up to date) * cookbook_file[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/spec_helper.rb] action create_if_missing (up to date) * template[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/unit/recipes/ktexperts-apache-recipe_spec.rb] action create_if_missing - create new file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/unit/recipes/ktexperts-apache-recipe_spec.rb - update content in file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/spec/unit/recipes/ktexperts-apache-recipe_spec.rb from none to f34604 (diff output suppressed by config) * directory[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/test/integration/default] action create (up to date) * template[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/test/integration/default/ktexperts-apache-recipe_test.rb] action create_if_missing - create new file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/test/integration/default/ktexperts-apache-recipe_test.rb - update content in file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/test/integration/default/ktexperts-apache-recipe_test.rb from none to d0fbca (diff output suppressed by config) * template[/home/ec2-user/cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb] action create - create new file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb - update content in file /home/ec2-user/cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb from none to 70657e (diff output suppressed by config) |
To see the list of files and directories in a tree structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[root@ip-172-31-42-243 ktexperts-apache-cookbook]# tree . ├── CHANGELOG.md ├── chefignore ├── kitchen.yml ├── LICENSE ├── metadata.rb ├── Policyfile.rb ├── README.md ├── recipes │ ├── default.rb │ └── ktexperts-apache-recipe.rb ├── spec │ ├── spec_helper.rb │ └── unit │ └── recipes │ ├── default_spec.rb │ └── ktexperts-apache-recipe_spec.rb └── test └── integration └── default ├── default_test.rb └── ktexperts-apache-recipe_test.rb 7 directories, 14 files |
Go to cookbooks directory
1 2 |
[root@ip-172-31-42-243 ktexperts-apache-cookbook]# cd .. [root@ip-172-31-42-243 cookbooks]# |
Open the recipe “ktexperts-apache-recipe.rb” and write apache web server script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb package 'httpd' do action :install end file '/var/www/html/index.html' do content "Ktexperts is a knowledge sharing platform" action :create end service 'httpd' do action [ :enable, :start ] end |
Note
:wq! — to quit.
Verify syntax of script
1 2 |
[root@ip-172-31-42-243 cookbooks]# chef exec ruby -c ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb Syntax OK |
Execute the Recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-apache-cookbook::ktexperts-apache-recipe]" [2019-12-25T05:50:13+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-apache-cookbook::ktexperts-apache-recipe"] Synchronizing Cookbooks: - ktexperts-apache-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 3 resources Recipe: ktexperts-apache-cookbook::ktexperts-apache-recipe * yum_package[httpd] action install - install version 0:2.4.41-1.amzn2.0.1.x86_64 of package httpd * 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 67bb1d --- /var/www/html/index.html 2019-12-25 05:50:21.830170389 +0000 +++ /var/www/html/.chef-index20191225-3629-125kgut.html 2019-12-25 05:50:21.830170389 +0000 @@ -1 +1,2 @@ +Ktexperts is a knowledge sharing platform * service[httpd] action enable - enable service service[httpd] * service[httpd] action start - start service service[httpd] Running handlers: Running handlers complete Chef Infra Client finished, 4/4 resources updated in 08 seconds |
Verify content of Apache Web Server
Copy public IP and search in browser
Copy IPV4 Public IP from Chef-Workstation.
Search PV4 Public IP in browser
we can able to see the content of apache web server.
Modify Recipe “ktexperts-apache-recipe.rb”
Add some content to the index.html file
1 2 3 4 5 |
[root@ip-172-31-42-243 cookbooks]# vi ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb file '/var/www/html/index.html' do content "Ktexperts is a knowledge sharing platform &&& It will help you to share knowledge" action :create end |
Note
:wq! — to quit
Execute the Recipe/call chef-client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[root@ip-172-31-42-243 cookbooks]# chef-client -zr "recipe[ktexperts-apache-cookbook::ktexperts-apache-recipe]" [2019-12-27T20:50:40+00:00] WARN: No config file found or specified on command line. Using command line options instead. Starting Chef Infra Client, version 15.6.10 resolving cookbooks for run list: ["ktexperts-apache-cookbook::ktexperts-apache-recipe"] Synchronizing Cookbooks: - ktexperts-apache-cookbook (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 3 resources Recipe: ktexperts-apache-cookbook::ktexperts-apache-recipe * yum_package[httpd] action install (up to date) * file[/var/www/html/index.html] action create - update content in file /var/www/html/index.html from 5734e6 to 97e2a3 --- /var/www/html/index.html 2019-12-27 20:49:50.069471715 +0000 +++ /var/www/html/.chef-index20191227-7118-198h0ah.html 2019-12-27 20:50:43.648374524 +0000 @@ -1,2 +1,2 @@ -Ktexperts is a knowledge sharing platform &&& i +Ktexperts is a knowledge sharing platform &&& It will help you to share knowledge * service[httpd] action enable (up to date) * service[httpd] action start (up to date) Running handlers: Running handlers complete Chef Infra Client finished, 1/4 resources updated in 03 seconds |
Refresh IPV4 Public IP in browser
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
priya
The article is well designed and crystal clear. Reader friendly and easy to understand.
santhi
Clear explanation on depolying apache tomcat server in chef