How to configure Apache Load-balancer on CentOS7
Apache is used world wide to host website. It proved to be good performance software in many ways which also provide many other features and functionalities.
In this post we will configure Load-balancer which manage network between two Apache machine in way that clients connection balance between multiple Apache servers.
So in front-end we could have one Apache Load-balancer that would redirect to multiple back-end Apache machine, in this post we have only two Apache machine like below.
For same we are using CentOS7 Machine like below.
[root@srv40 ~]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) [root@srv40 ~]# uname -r 3.10.0-1160.11.1.el7.x86_64
We have three machine with below details
================================================| Machine name | Roles | IP details | ----------------------------------------- | 1. srv40 | load-balancer | 192.168.0.40 | 2. srv41 | Web-Server | 192.168.0.41 | 3. srv42 | Web-Server | 192.168.0.42 | ================================================|
For same we need to install Apache Software on all three machine and need to configure load-balancer on front-end machine and test web-pages on other two machines.
Installation of Apache
As i am doing this setup on RHEL7/CentOS7 machine, we will be using yum package manager on these machine, like below to install Apache on all three machines
# yum groupinstall "Web Server" -y
You should have super user root privileges to install these packages on machines.
configure web-Server first
Let’s configure back-end WebServer first to test things working to test Load-balancer.
So our WebServer are RHEL7/CentOS7, After installation of packages we can start httpd service with below command.
systemctl restart httpd
Once we start apache service, we can check it through status command, like below
[root@srv42 ~]# systemctl status httpd ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2021-01-02 13:03:01 IST; 10h ago Docs: man:httpd(8) man:apachectl(8) Main PID: 6909 (httpd) Status: "Total requests: 295; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─6909 /usr/sbin/httpd -DFOREGROUND ├─6910 /usr/sbin/httpd -DFOREGROUND ├─6911 /usr/sbin/httpd -DFOREGROUND ├─6912 /usr/sbin/httpd -DFOREGROUND ├─6913 /usr/sbin/httpd -DFOREGROUND ├─6914 /usr/sbin/httpd -DFOREGROUND ├─6915 /usr/sbin/httpd -DFOREGROUND ├─7151 /usr/sbin/httpd -DFOREGROUND ├─7174 /usr/sbin/httpd -DFOREGROUND ├─7175 /usr/sbin/httpd -DFOREGROUND └─7176 /usr/sbin/httpd -DFOREGROUND Jan 02 13:03:01 srv42.geekpills.com systemd: Stopped The Apache HTTP Server. Jan 02 13:03:01 srv42.geekpills.com systemd: Starting The Apache HTTP Server... Jan 02 13:03:01 srv42.geekpills.com systemd: Started The Apache HTTP Server.
It’s always good to enable service through below command, So we will have it auto start in case machine reboot anytime.
[root@srv42 ~]# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Now we can check if we see default CentOS Apache page on Machine-IP or Hostname through browser.
To remove default page we need to comment-out or delete welcome page file “/etc/httpd/conf.d/welcome.conf” and create another file in DocumentRoot of Apache like below.
[root@srv41 ~]# cat /var/www/html/index.html Server1 in proxy configuration:srv41 ============================================ [root@srv42 ~]# cat /var/www/html/index.html Server2 in proxy configuration:srv42
Once we have such pages, we can check them through Web-Server IP on browser.
Configure Apache Load-balancer
Now we need to configure Apache Load-balancer on front-end machine which will redirect Client communication to above two back-end Application hosted Apache Servers.
I have appended below lines in “/etc/httpd/conf/httpd.conf”.
<VirtualHost *:80> <Proxy balancer://mycluster> BalancerMember http://192.168.0.41:80 BalancerMember http://192.168.0.42:80 ProxySet lbmethod=byrequests </Proxy> <Location /balancer-manager> SetHandler balancer-manager Require ip 192.168.0.4 192.168.0.12 192.168.0.2 127.0.0.1 192.168.0.40 </Location> ProxyPass /balancer-manager ! ProxyPreserveHost On ProxyPass /app balancer://mycluster/ ProxyPassReverse /app balancer://mycluster/ </Virtualhost>
So in above We are creating one proxy balancer Set named mycluster which in tern have two members.
Connecting Load-balancer-IP/app it will automatically connect to back-end Servers like below.
So now if try to connect to Load-balancer-IP/app, i get webpage from back-end WebServer page like below and on refresh page keep on moving to another back-end machine.
On Page refresh, keep on change Server connection through Load-balancer.
This also has one Load balancer-manger Web Tool that also configured in above configuration and only allow to open from some specific IPs only.
This tools by-default comes with proxy.
[root@srv40 ~]# httpd -M| grep proxy_balancer proxy_balancer_module (shared)
With Above method, Now we know how we can manage to configure Load-balancer for back-end machine and manage Client connection between multiple back-end Apache Machines.