Forum Moderators: phranque
Lets say you have 2 servers all hosting the same website. This allows you to spread the load across 2 servers. What I was thinking was, to use a simple php script to detect a server going down and cut it out of the equasion.
Lets use a simple random script to decide who goes where. This will ensure that each server gets a prety even load. The random selection scrit will read from a text file..
server1
server2
this gives it 2 possible options.
Now we need to build the text file so we run a cron every so often, It runs a script, lets call it server.php that makes a request to each of the servers...
<?php
#server.php
include "ip.to.server.1./status.php" ;
include "ip.to.server.2./status.php" ;
if ($server1 == 'yes') {
} elseif ($server1 == 'no') {
if ($server2 == 'yes') {
} elseif ($server2 == 'no') {
?>
We could then use fopen to write the text file each time the script is run.
the file Status.php would simply be a php document placed in the root folder of each server
example...
<?php
#status.php
$server1="yes" ;
?>
The idea is to draw an include from all servers and this include will ensure that the server is placed within the random selection, and that any dead severs are removed.
Has anyone ever done anything like this and if so did it work, or do you have any better solutions.
Mack.
Thanks in advance.
Anyway, your solution is simple, but in terms of the amount of "work" (as in network and file I/O) your servers have to do to be load balanced, it is actually not simple at all. Implementing load balancing at the PHP level is just not a good idea.
As for alternatives, free and fairly easy to setup alternatives exist such as LVS [linuxvirtualserver.org...]
[edited by: DaveAtIFG at 2:57 pm (utc) on April 7, 2004]