Skip to content

Using Ansible-playbook for Change in DocumentRoot of Apache Webserver and create custom DocumentRoot in APache webserver and deploy Webpage

Notifications You must be signed in to change notification settings

Pratikshinde55/Ansible-Apache-Change_default_DocumentRoot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 

Repository files navigation

Change default DocumentRoot of Apache WEb server by using Ansible Automation:

Screenshot 2024-03-08 154000

Apache webserver(httpd):

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

By Ansible Adhoc Command: [Manual way]

  1. Install httpd package :

    yum install httpd -y
    
  2. 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
    
  3. 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.

  1. After file created then need to restart httpd service (when new settings created ):

    systemctl reload httpd
    

By Ansible Automation: [Ansible-playbook]

I take three AWS Cloud instances Amazon linux EC2, One instance make Ansible-Master & remaining Instances to make hosts or managed nodes.

Screenshot 2024-03-07 193702

In ansible-master node install ansible-core and connect with target node by ssh key Authentication :

Ansible-SetUp-onAWS

Step-1: [Create file or web page which will weploy ,(index.html) on ansible master node]

Screenshot 2024-03-07 194123

Step-2: [Create playbook ( "myweb.yml" my playbook name)]

vim myweb.yml

Screenshot 2024-03-07 194019

  • 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

Step-4: [Run ansible-playbook]

  ansible-playbook myweb.yml

Screenshot 2024-03-07 193938

Here we can see that Automation takes place on Manged node1 & node2 :

At ansible target nodes check what changes happened:

Screenshot 2024-03-07 194252

Screenshot 2024-03-07 194309

From google or outside world we can able to connect target Node1 & node2:

Ansible Target node 1 : public IP of EC2 instance : node1 : 3.7.69.254

Screenshot 2024-03-07 194326

Ansible Target node 2 : public IP of EC2 instance : node2 : 13.201.93.84

Screenshot 2024-03-07 194334

About

Using Ansible-playbook for Change in DocumentRoot of Apache Webserver and create custom DocumentRoot in APache webserver and deploy Webpage

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published