LOAD BALANCING LARAVEL APPLICATION WITH APACHE LOAD BALANCER

PLEASE SHARE

This article explains how to implement load balancing(high availability) for the Laravel application using Apache load balancer installed on the Ubuntu operating system. This article also has instructions to enable SSL with a self-signed certificate for load balancing server.

Here are the advantages of implementing load balancing,

  • The application will be scalable. We can easily add or remove application nodes for facilitating increased or decreased traffic.
  • We can achieve high availability. Even if some application nodes are down, the other nodes will continue to serve the users.
  • Multiple application servers nodes will share the load; thus, we can maintain optimum system performance.

Here’s the load balancing architecture for our reference,

PREREQUISITES

  • Ubuntu 18.04 OS
  • Apache2 Web Server
  • A Laravel application running in 2 or more servers (Nodes)

INSTALLING LOAD BALANCER

The following are the instructions to install and configure the load balancer server.

INSTALLING APACHE2

  1. Login into Ubuntu Server.
  2. Switch to the root user by executing the command,
sudo su -

3. Execute the following commands to install the Apache2 server,

apt-get update
apt-get install apache2

4. Open the /etc/apache2/apache2.conf file to edit. Add the following line at the end of the file,(Replace <IP_ADDRESS> with IP address of the load balancer server)

ServerName <IP_ADDRESS>

5. Save & exit the file.

6. Execute the following command,

systemctl restart apache2

7. Type “http://<LOAD_BALANCER_IP_ADDRESS>” into the browser and make sure the Apache Server default page is getting displayed.

GENERATING THE SELF SIGNED CERTIFICATE

  1. Execute the following command to generate key and certificate,
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/server.pem -out /etc/ssl/certs/server.crt

2. Enter the appropriate values when prompted as displayed in the below screenshot, (Enter server hostname or IP address for Common Name)

Note: The same certificate files (server.pem, server.crt) generated above should be used in all the Laravel application nodes to enable SSL.

CONFIGURING VIRTUAL HOSTS

  1. Open “/etc/apache2/sites-available/default-ssl.conf” file to edit.
  2. Delete all the text and paste the following.
ProxyRequests On
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://myloadbalancer>
        	BalancerMember https://<SERVER_1_IP_ADDRESS> route=node1 retry=1 acquire=3000 timeout=600 Keepalive=On
        	BalancerMember https://<SERVER_2_IP_ADDRESS> route=node2 retry=1 acquire=3000 timeout=600 Keepalive=On
        	ProxySet lbmethod=byrequests
        	ProxySet stickysession=ROUTEID
</Proxy>
 
<IfModule mod_ssl.c>
        	<VirtualHost _default_:443>
                    	ServerAdmin webmaster@localhost
                    	ServerName  <LOAD_BALANCER_IP>
                    	ServerAlias  <LOAD_BALANCER_IP>
 
                    	ErrorLog ${APACHE_LOG_DIR}/error.log
                    	CustomLog ${APACHE_LOG_DIR}/access.log combined
                    	SSLProxyEngine on
                    	SSLProxyVerify none
                    	SSLProxyCheckPeerCN off
                    	SSLProxyCheckPeerName off
                    	SSLProxyCheckPeerExpire off
                    	SSLCipherSuite SSLv2:-LOW:-EXPORT:RC4+RSA
                    	SSLCertificateFile /etc/ssl/certs/server.crt
                    	SSLCertificateKeyFile /etc/ssl/private/server.pem
                    	SSLVerifyClient optional
                    	RequestHeader set X-Client-DN %{SSL_CLIENT_S_DN}e
                    	RequestHeader set X-Client-Verify %{SSL_CLIENT_VERIFY}e
 
                    	<Location />
                                	SetHandler balancer-manager
                                	Order allow,deny
                    	        	Allow from all
                    	</Location>
 
                    	ProxyPass /balancer-manager !
                    	ProxyPass / balancer://myloadbalancer/ stickysession=ROUTEID nofailover=On
                    	ProxyPassReverse / balancer://myloadbalancer/
                    	ProxyPreserveHost on
 
        	</VirtualHost>
</IfModule>
 
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

3. Replace <SERVER_1_IP_ADDRESS> with the IP address of node 1 of the Laravel application server.

4. Replace <SERVER_2_IP_ADDRESS> with the IP address of node 2 of the Laravel application server.

5. Replace <LOAD_BALANCER_IP> with the IP address of the load balancer server.

6. Define the load balancing method as below,

ProxySet lbmethod=byrequests

The load balancing method can be any of the following,

byrequests This method will distribute the requests among nodes in a round-robin fashion. Also, this is the default load-balancing method.
bybusyness This method keeps track of how many “requests” are currently assigned to each node at present. A node with the lowest number of active “requests”  will automatically get a new request.
bytrafficThis method is similar to the weighted round-robin algorithm.  

7. Define the sticky session as below,

ProxySet stickysession=ROUTEID

This is the name of the cookie or request parameter to maintain the session with the clients. Incase of Laravel applications it’s ROUTEID.

8. Save and close the file.

9. Open “/etc/apache2/sites-available/000-default.conf”  file to edit.

10. Delete all the text and paste the following.

<VirtualHost *:80>
        	ServerAdmin webmaster@localhost
        	Redirect / https://<LOAD_BALANCER_IP>/
        	ProxyPreserveHost on
 
        	ErrorLog ${APACHE_LOG_DIR}/error.log
        	CustomLog ${APACHE_LOG_DIR}/access.log combined
 
</VirtualHost>
 
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

11. Replace <LOAD_BALANCER_IP> with the IP address of the load balancer.

12. Save and close the file.

ENABLING PROXY

  1. Execute the following commands,
a2enmod proxy

a2enmod proxy_balancer

a2enmod proxy_http

a2enmod ssl

a2ensite default-ssl

a2enmod headers

a2enmod lbmethod_byrequests

2. Restart the Apache server,

systemctl restart apache2

CONFIGURING LOAD BALANCER IP ADDRESS IN LARAVEL APPLICATION NODES

Here are the instructions to configure the load balancer IP address in Laravel application nodes.

Note: These changes need to be applied in all the servers(Nodes) hosting the Laravel application.

  1. Login into the Laravel application node
  2. Open the “<LARAVEL_APP_HOME>/.env” file for editing. (Note: <LARAVEL_APP_HOME> is the root directory of Laravel application)
  3. Specify the APP_URL as below,
APP_URL=https://<LOAD_BALANCER_IP_ADDRESS>

4. Save and close the “.env” file.

5. Open the “<LARAVEL_APP_HOME>/app.php” file for editing.

6. Specify the APP_URL environment variable as below,

'url' => env('APP_URL', 'https://<LOAD_BALANCER_IP_ADDRESS>'),

7. Save and close the “app.php” file.

8. Open the “<LARAVEL_APP_HOME>/session.php” file for editing.

9. Specify the SESSION_DOMAIN environment variable as below,

'domain' => env('SESSION_DOMAIN', '<LOAD_BALANCER_IP_ADDRESS>'),

10. Save and close the “session.php” file.

11. Execute the following commands to clear the cache,

php artisan config:clear

php artisan cache:clear

php artisan config:cache

TESTING THE LARAVEL LOAD BALANCING

  1. Try accessing the load balancer URL. You should have access to the Laravel application hosted in any of the nodes.
  2. Stop all the application nodes except one and make sure the application is accessible via load balancer URL.

Tip: We can monitor the Apache load balancer status with the following URL,

https://<LOAD_BALANCER_IP_ADDRESS>/balancer-manager

Example : 

Follow Me

Leave a Reply

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