Hosting + Ai Website Builder + Free Domain (3 Month Free Credit)
Shop Today

How to Use Load Balancer on Linux – Easy Tutorial

Use load balancer on a Linux server to distribute incoming network or application traffic across multiple backend servers. This increases the availability, reliability, and scalability of your web services or applications by ensuring no single server becomes a bottleneck or point of failure. Load balancing improves performance and fault tolerance by balancing requests dynamically based on your chosen algorithms.

Load Balancer on Linux

Common open-source load balancers on Linux include HAProxyNginx, and the built-in reverse proxy capabilities of Apache HTTP Server.

This guide will walk you through how to use load balancer on a Linux server using popular tools, covering installation, configuration, basic management, and testing.

Prerequisites

  • A Linux server (Ubuntu, Debian, CentOS, Red Hat, etc.) designated as your load balancer server
  • Multiple backend servers (web or application servers) with static IPs or hostnames that the load balancer can proxy to
  • Root or sudo privileges on the Linux load balancer server
  • Basic command-line proficiency

Use Load Balancer on a Linux Server

Using load balancer on a Linux server ensures efficient distribution of incoming network traffic across multiple backend servers. This setup improves performance, maximizes uptime, and enhances fault tolerance, making it essential for scalable and high-availability environments like web apps, APIs, or cloud platforms.

Choose and Install Your Load Balancer

The first step is selecting a load balancer (like HAProxy or NGINX) and installing it using your Linux distribution’s package manager.

Option A: Install HAProxy (High-Performance TCP/HTTP Load Balancer)

  • On Ubuntu/Debian:
sudo apt update
sudo apt install haproxy
  • On CentOS/Red Hat:
sudo yum install haproxy
  • On Ubuntu/Debian:
sudo apt update
sudo apt install nginx
  • On CentOS/Red Hat:
sudo yum install nginx

Option C: Use Apache HTTP Server as a Load Balancer

Ensure required modules are installed and enabled:

  • On Ubuntu/Debian:
sudo apt update
sudo apt install apache2
sudo a2enmod proxy proxy_http proxy_balancer proxy_balancer_http lbmethod_byrequests
sudo systemctl restart apache2
  • On CentOS/Red Hat:
sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd

Make sure the modules for proxy and load balancing are loaded (mod_proxymod_proxy_balancer, etc.).

Configure Load Balancer with Backend Servers

Define backend servers in your load balancer config to distribute traffic across them efficiently.

HAProxy Basic Configuration Example

Edit the HAProxy config file /etc/haproxy/haproxy.cfg:

global
log /dev/log local0
maxconn 4096

defaults
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms

frontend http_front
bind *:80
default_backend http_back

backend http_back
balance roundrobin
server web1 192.168.1.101:80 check
server web2 192.168.1.102:80 check
  • balance roundrobin distributes requests evenly
  • check enables health checks on backend servers

Restart HAProxy to apply:

sudo systemctl restart haproxy

Nginx Load Balancer Configuration Example

Create or edit the Nginx load balancer config /etc/nginx/conf.d/loadbalancer.conf:

upstream backend_servers {
server 192.168.1.101;
server 192.168.1.102;
}

server {
listen 80;

location / {
proxy_pass http://backend_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

Test configuration and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Apache HTTP Server Load Balancer Configuration Example

Add load balancer config in /etc/apache2/sites-available/000-default.conf (Ubuntu/Debian) or corresponding Apache config file:

<Proxy balancer://mycluster>
BalancerMember http://192.168.1.101
BalancerMember http://192.168.1.102
</Proxy>

ProxyPreserveHost On
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/

Restart Apache to apply changes:

sudo systemctl restart apache2

Test Your Load Balancer

After configuration, it’s essential to verify that your load balancer distributes traffic correctly and consistently across all backend servers.

  • Open a browser and navigate to the load balancer server’s IP or domain name on the configured port (usually port 80)
  • Refresh the page multiple times to confirm that traffic is distributed among the backend servers
  • Use tools like curl to check responses and backend variations:
curl http://your-load-balancer-ip/
  • Monitor load balancer logs (/var/log/haproxy.log/var/log/nginx/access.log/var/log/apache2/access.log) for traffic distribution insights

Advanced Load Balancer Settings

Once your basic load balancer setup is working, you can optimize it with advanced features to improve performance, reliability, and security.

  • Configure health checks and failure handling
  • Use sticky sessions if session persistence is needed
  • Tune load balancing algorithms (roundrobin, leastconn, source IP hashing, etc.)
  • Enable SSL termination on the load balancer
  • Configure access control and rate limiting for security

Conclusion

To use load balancer on a Linux server, you install your preferred load balancing tool, such as HAProxy, Nginx, or Apache HTTP server. Then, configure backend servers to distribute incoming client requests using proxy rules and load balancing algorithms. This helps improve application scalability, availability, and fault tolerance by efficiently managing traffic across multiple servers.

For detailed official documentation for advanced deployment and features, see:

Himanshu Joshi

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top