It is a free and open-source Web server Software. It's one of the most widely used web servers in the world, known for its reliability and robustness. Apache is capable of serving static and dynamic content on the World Wide Web.
- Note:
" /etc/httpd/ " or "/etc/apache2/ directory" is common location httpd configuration file.
In this project, I can create custom Document Root in httpd by using ansible-playbook
-
Install httpd package :
yum install httpd -y
-
Go inside Httpd internal configuration file , In this file we can create custom documentRoot, "/etc/httpd/conf.d" this httpd Default configuration file location:
cd /etc/httpd/conf.d
-
Create own name or custom name folder inside Httpd configuration file("/etc/httpd/conf.d"), i am creating "my.conf" file & in this file i put my custom documentRoot:
-
my.conf file put :--> DocumentRoot /var/www/pratik
vi my.conf
Now apache web server read from this filder , It means from browser we connect this apache filder & in this folder we deploy own webpages.
-
After file created then need to restart httpd service (when new settings created ):
systemctl reload httpd
I take three AWS Cloud instances Amazon linux EC2, One instance make Ansible-Master & remaining Instances to make hosts or managed nodes.
In ansible-master node install ansible-core and connect with target node by ssh key Authentication :
vim myweb.yml
- playbook explanation:
A. Here set variables (vars) which for my tasks :-
"webpage" variable for my local code or webpage which want to copy and deploy " packageName" variable for package that i want to install " documentDir" variable for my own custom documentRoot
- hosts: web
vars:
webpage: "index.html"
packageName: "httpd"
documentDir: "/var/www/pratik"
B. Install Httpd package here use variable packageName
tasks:
- name: "Install httpd package"
package:
name: "{{ packageName }}"
state: present
C. create document root :
File module is used to create directory & use state is directory, This Task createte directory as /var/www/pratik in ansible target nodes :
- name: "Create Document root for httpd package"
file:
state: directory
path: "{{ documentDir }}"
D. This is very important step Httpd configuration file change: (DocumentRoot)
Here Copy module used to create "my.conf" file at destination "/etc/httpd/conf.d" & and in this "/etc/httpd/conf.d/my.conf" file 'content' attribute create DocumentRoot = documentDir "/var/www/pratik":
- name: "Create or copy new path in config file"
copy:
dest: /etc/httpd/conf.d/my.conf
content: |
DocumentRoot {{ documentDir }}
E. Deploy local webpage "index.html to target nodes destination documentDir "/var/www/pratik/"
- name: "Deploy webpage"
copy:
src: "{{ webpage }}"
dest: "{{ documentDir }}"
F. Reload httpd service, After any change make in httpd configuration file then need to reload or resatrt httpd service then new settings apply:
- name: "Reload service httpd"
service:
name: "httpd"
state: reloaded
enabled: true
ansible-playbook myweb.yml
At ansible target nodes check what changes happened:
Ansible Target node 1 : public IP of EC2 instance : node1 : 3.7.69.254
Ansible Target node 2 : public IP of EC2 instance : node2 : 13.201.93.84