Load balancing PHP web application with three servers

13,012

Solution 1

I've used HAProxy in a similar setup and I highly recommend it. It can run very well on a single server and you only need two servers for load balancing in case you want high availability.

There are many tutorials such as this that provides detailed explanations on how to set it up in the configuration you seek.

Solution 2

I just yesterday created a configuration where NGINX server as a load balancer, behind which there are 2 PHP-FPM servers, 1 Memcache server and 1 MySQL server. NGINX was configured using Upstreaming feature and the related configuration lines are something like this:

html {

    ...

    # loadbalancing        
    upstream myLoadBalancer {
        ip_hash; # makes sure same user uses the same server, not 100% effective - application
                 # should handle this; in my case 1 Memcached and 1 MySQL servers commonly used
                 # by all App-servers work just fine. I store sessions in Memcache, so Session 
                 # management isn't a problem at all. Its shared across all App-servers.
        server 192.168.1.10:9000; # location of my first php-fpm server
        server 192.168.1.11:9000; # second php-fpm server
        # server aaa.bbb.ccc.ddd:80; # let's say, an Apache server
    }

    #vhost
    server {
        listen 80;
        server_name mydomain.com;
        index index.php;

        location ~* \.php$ {
            gzip on;
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass myLoadBalancer;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /path/to/webdir$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_script_name;
        }
    }
}

HTTPS: They should be installed on NGINX load-balancer if I am not mistaken - never tried myself. Once client request is passed down to App-server, it will process it, fetch the request and send it back to NGINX. And NGINX, before relaying the response to the client, will encrypt the content. Its a theory of course. But I am 100% positive NGINX can handle SSL quite easily. It is the fastest and the most robust proxy/balancer and good webserver when combined with FASTCGI capabilities of various CLIs.

NOTE: This is not a configuration for production environment, but a test-case scenario. Production environment would require much secure settings. Following resources could be of use:

Share:
13,012
Ecdoram
Author by

Ecdoram

Updated on July 15, 2022

Comments

  • Ecdoram
    Ecdoram almost 2 years

    I have 2 web servers and 1 server that is intended to be used as reverse proxy or load balancer. 2 web servers have real/public IPs as well as the load balancer. Load balancer server is not configured yet because I did not decide which option will be best for my web applications. I am aware of one load balancer is risky because "single point of failure" but I want to go with it.

    2 web servers contain more than one PHP application/vhost (Apache + fastcgi) with same domain and different subdomains. They all use sessions, cookies and some of them require SSL. My main goal is purely divide incoming connections in half and forward them to 2 web nodes. And if one web node goes offline, the other should accept all connections. And I think if needed we can do session sharing with memcache.

    I read about Nginx, HaProxy and any other application I can find on the net. But I cannot decide because :

    1) I have 1 server that I can use as load balancer but all configuration I found on the net needs 2 load balancer nodes. 2) I am not sure where should I install SSL certificates (on load balancer or web nodes) and which solution is the best while using HTTPS connections.

    Any help/idea is needed, thanks a lot.

    • Brian
      Brian almost 13 years
      Also have a think about lighty - lighttpd.net