Apache Virtual Host: Load Balancer

xxzeroxx

Antonio Silva

Posted on October 10, 2024

Apache Virtual Host: Load Balancer

Load balancing is an excellent strategy for distributing requests across multiple backend servers, improving system scalability and availability. Apache can be configured as a load balancer using the mod_proxy_balancer module.

Here is a complete guide to implementing load balancing in Apache:

Enable the Required Modules

First, enable the required modules in Apache:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
Enter fullscreen mode Exit fullscreen mode

Restart Apache to load the modules:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Configure Virtual Host with Load Balancing

Now, edit your Virtual Host's configuration file to add the load balancing directives.

Open the configuration file:

sudo your_editor /etc/apache2/sites-available/php.conf
Enter fullscreen mode Exit fullscreen mode

Add the following block of code to configure load balancing across multiple backend servers:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName php.info

    # Load balancer configuration
    <Proxy "balancer://meucluster">
        BalancerMember http://localhost:8080
        BalancerMember http://localhost:8081
        BalancerMember http://localhost:8082
        ProxySet lbmethod=byrequests
    </Proxy>

    ProxyPreserveHost On
    ProxyPass / balancer://meucluster/
    ProxyPassReverse / balancer://meucluster/

    <Directory /var/www/html/php/>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/php_error_http.log
    CustomLog ${APACHE_LOG_DIR}/php_access_http.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Explanation of the above elements:

  • BalancerMember: Defines the backend servers. In this case, we are configuring three servers listening on ports 8080, 8081, and 8082. You can replace these values ​​with your actual servers.
  • lbmethod=byrequests: Defines the balancing method. byrequests distributes requests equally among the servers. Other methods include:
    • bytraffic: distributes based on the amount of traffic.
    • bybusyness: distributes based on the number of active connections.
    • heartbeat: uses an advanced health monitoring approach.

Add Backend Servers

In the above example, I'm assuming you have three backend services running on localhost on ports 8080, 8081, and 8082. Make sure you have these services running.

Otherwise, you can configure backend servers with the correct ports or use Docker containers to simulate multiple services.

Enable SSL Module for HTTPS

If you want to load balance over HTTPS, also add the SSL Virtual Host (/etc/apache2/sites-available/php-le-ssl.conf) to load balance across HTTPS backend servers:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        ServerName php.info
        DocumentRoot /var/www/meu_projeto

        # Configuração do Balanceador de Carga
        <Proxy "balancer://meucluster">
            BalancerMember http://localhost:8080
            BalancerMember http://localhost:8081
            BalancerMember http://localhost:8082
            ProxySet lbmethod=byrequests
        </Proxy>

        ProxyPreserveHost On
        ProxyPass / balancer://meucluster/
        ProxyPassReverse / balancer://meucluster/

        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/php.info/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/php.info/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf

        ErrorLog ${APACHE_LOG_DIR}/php_error_https.log
        CustomLog ${APACHE_LOG_DIR}/php_access_https.log combined
    </VirtualHost>
</IfModule>
Enter fullscreen mode Exit fullscreen mode

Advanced Configuration Options

Set Server Weights

You can set different weights for servers, meaning that some servers receive more traffic than others. Example:

BalancerMember http://localhost:8080 loadfactor=1
BalancerMember http://localhost:8081 loadfactor=2
BalancerMember http://localhost:8082 loadfactor=1
Enter fullscreen mode Exit fullscreen mode

In this case, the server at localhost:8081 will receive twice as many requests as the others.

Set Failure Timeouts and Retries

You can set timeouts and retries to detect failures on backend servers:

<Proxy "balancer://mycluster">
BalancerMember http://localhost:8080 retry=5 timeout=10
BalancerMember http://localhost:8081 retry=5 timeout=10
BalancerMember http://localhost:8082 retry=5 timeout=10
ProxySet lbmethod=byrequests
</Proxy>
Enter fullscreen mode Exit fullscreen mode

Monitor and Manage the Load Balancer

To monitor the load balancer health and manage active/inactive members dynamically, launch the Balancer Manager interface:

<Location "/balancer-manager">
SetHandler balancer-manager
Require host localhost
</Location>
Enter fullscreen mode Exit fullscreen mode

You can now access http://php.info/balancer-manager to view the load balancer health and adjust settings in real time.

Restart Apache

After making the configuration changes, restart Apache for the changes to take effect:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Test Load Balancing

Now, when you access http://php.info, Apache will distribute the requests among the defined backend servers.

Implement Health Checks (Optional)

You can configure Apache to check the health of backend servers and automatically remove them from the pool if they are down. To do this, you can use the mod_proxy_hcheck module.

First, enable the module:

sudo a2enmod proxy_hcheck
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Then, add the following configuration to your <Proxy> block:

<Proxy "balancer://meucluster">
    BalancerMember http://localhost:8080 hcheck=on hcmethod=HEAD
    BalancerMember http://localhost:8081 hcheck=on hcmethod=HEAD
    BalancerMember http://localhost:8082 hcheck=on hcmethod=HEAD
    ProxySet lbmethod=byrequests
</Proxy>
Enter fullscreen mode Exit fullscreen mode

Apache will now automatically check the backend servers and remove them from the pool if they fail.

Conclusion

With Apache configured as a load balancer, you can distribute traffic across multiple backend servers, ensuring scalability and redundancy. Using SSL and additional health checks helps keep the environment secure and robust.

💖 💪 🙅 🚩
xxzeroxx
Antonio Silva

Posted on October 10, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related