Configure Apache Load Balancer for High Traffic Website – Outsourced Support | Web Hosting Support
Struggling with a high-traffic website, when there are no performance issues? Then no worries now with Apache load balancer you can manage high-traffic websites hassle-free. (Setup is based on Ubuntu/Debian systems, if you need assistance click here – Get Assistance).
You can follow the below steps to configure the Apache load balancer for
your website to avoid high traffic.
Step 1: Install Apache Modules
To configure the Apache load balancer, we require four Apache modules:
- mod_proxy: The primary proxy module that redirects traffic and
enables Apache to serve as a gateway to backend servers. - mod_proxy_http: Allows proxying HTTP
requests - mod_proxy_balancer and mod_lbmethod_byrequests: Add a load balancing
support to the Apache web server.
To install the required Apache modules, open a terminal and run the
following commands.
$ sudo a2enmod proxy $ sudo a2enmod proxy_http $ sudo a2enmod proxy_balancer $ sudo a2enmod lbmethod_byrequests
Step 2: Restart Apache Server
Apply the changes by restarting Apache Server.
$ sudo service apache2 restart
Step 3: Configure backend servers
Installing flask and setting up two servers to run on ports 8080 and 8081
as a backend server will allow us to configure Apache Load Balancer.
$ sudo apt-get update $ sudo apt-get -y install python3-pip $ sudo pip3 install flask
The default /backend.py file for Flask returns “Hello World” on
the home page of the request.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello world!'
Let’s duplicate it for our secondary server.
$ sudo cp ~/backend.py ~/backend1.py
In backend1.py, change the “Hello World” message in the last line
to “Hello World 2”.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello world 2!'
Initially, run the First Flask Server
$ FLASK_APP=~/backend.py flask run --port=8080 >/dev/null 2>&1&
and you can run the curl command to test this server.
$ curl http://127.0.0.1:8080/
Output: Hello World!
Now, run the Second Flask Server
$ FLASK_APP=~/backend1.py flask run --port=8081 >/dev/null 2>&1&
and you can run the curl command to test this server.
$ curl http://127.0.0.1:8081/
Output: Hello World 2!
We now have two backend servers that can handle the load. We will divide
the load between these two servers.
Step 4: Setup Apache Load Balancer
To configure the Apache load balancer, we must change the default configuration file. Open the Apache configuration in a text editor.
$ sudo vi /etc/apache2/sites-available/000-default.conf
In the Apache configuration file, add the following lines to the
VirtualHost tag.
<Proxy balancer://mycluster> BalancerMember http://127.0.0.1:8080 BalancerMember http://127.0.0.1:8081 </Proxy> ProxyPreserveHost On ProxyPass / balancer://mycluster/ ProxyPassReverse / balancer://mycluster/
The three directives are:
- ProxyPreserveHost– It informs Apache to
retain the original host header and pass it on to back-end servers. - ProxyPass – Primary proxy directive.
This specifies that everything under root (/) should be sent to a server
back-end cluster of servers. When Apache receives a request for /example, it
sends it to http://your backend server/example. - ProxyPassReverse – informs Apache to change
the response header in the response received from the back-end server. If the
back-end server returns a location redirect response, then the client will be
redirected to the Apache proxy server rather than the back-end server.
Note: The backend server in the Proxy tag is mentioned as balancer://myclustera and inside the Proxy tag each backend server is mentioned as BalancerMember.You can have one or more BalancerMember.
<VirtualHost *:80> ... <Proxybalancer://mycluster> BalancerMember http://127.0.0.1:8080 BalancerMember http://127.0.0.1:8081 </Proxy> ProxyPreserveHost On ProxyPass / balancer://mycluster/ ProxyPassReverse / balancer://mycluster/ ... </VirtualHost>
Step 5:Restart Apache
Server
Restart Apache Server
after making the changes.
$ sudo service apache2 restart
Read More: Quick Fix Apache: “module wsgi_module is already loaded, skipping”
Health Checks for Load Balancing
How to setup SSL for Elastic Load Balancer in AWS
To get more updates you can follow us on Facebook, Twitter, LinkedIn
Subscribe to get free blog content to your Inbox
actsupp-r0cks
Source link