Configure Web Server and Load Balancer using Ansible in Redhat Linux OS

Dhiraj Kumar
3 min readJan 12, 2021

A computer which gives service to host the website for real world is known as web server , i have configured apache software on OS for use as web server .

Apache web server is run on port number 80 , it can be changed . Root directory of apache webserver is “/var/www/html”

In apache client authentication is done by webserver , for configure authentication through webserver first of all need to edit file “/etc/httpd/conf/httpd.conf”

After edit file run below command , this command will update data base for username

htpasswd /etc/www.passwd username

After add user to database create file “.htaccess” in root directory of web server as :

After done all the process restart webserver using below command

systemctl restart httpd

Load balancer: Load balancer balance the traffic of webservers or applications . It transfer the traffic among servers so that single point of failure is avoided.

Haproxy is free open source software that provides a high availability load balancer and proxy server for tcp and http based application , that spreads requests across multiple servers.

For configure web server and load balancer created ansible playbook

Ansible Playbook:

- hosts: mywebtasks:
- command: "ls -l /dvd"
register: x
ignore_errors: yes
- file:
state: directory
path: /dvd
when: x.rc != 0
- mount:
path: "/dvd"
src: "/dev/cdrom"
state: mounted
fstype: "iso9660"
- yum_repository:
name: "mydvd1"
description: "This is dvd1 for package"
baseurl: "/dvd/AppStream"
gpgcheck: no
- yum_repository:
name: "mydvd2"
description: "This is dvd2 for package"
baseurl: "/dvd/BaseOS"
gpgcheck: no
- name: "install web server"
package:
name: "httpd"
state: present
- name: "install php"
package:
name: "php"
state: present
- copy:
dest: "/var/www/html/index.php"
src: "index.php"
- replace:
path: "/etc/httpd/conf/httpd.conf"
regexp: "AllowOverride None"
replace: "AllowOverride AuthConfig"
- copy:
src: ".htaccess"
dest: "/var/www/html/.htaccess"
- package:
name: "python36"
- pip:
name: "passlib"
- htpasswd:
path: "/etc/www.passwd"
name: "dhiru"
password: "qwerty"
- service:
name: "httpd"
state: restarted
enabled: yes
- firewalld:
port: "80/tcp"
permanent: yes
state: enabled
immediate: yes
- hosts: mylbtasks:
- name: "install load balancer"
package:
name: "haproxy"
- template:
src: "haproxy.cfg.j2"
dest: "/etc/haproxy/haproxy.cfg"
- service:
name: "haproxy"
state: restarted
- firewalld:
port: "5000/tcp"
permanent: yes
state: enabled
immediate: yes

use below github link for get full code

https://github.com/firsttalk26/task12

For configure load balancer and update backend server in /etc/haproxy/haproxy.cfg file use jinja technique as

After creating playbook run below cmd

ansible-playbook lb_web_config.yml

After running playbook load balancer and webserver are configured automatically

Use url as https://ip_of_load_balancer:5000

clap, if you like this . give any suggestion if required. your suggestion will be appreciated.

--

--

Dhiraj Kumar

Expertise on Cloud Computing who has helped many startups to reduce cloud cost upto 40% based on business need. Focused to optimize development process.