# Load Balancing PHP

**URL:** <https://www.ruby-forum.com/t/load-balancing-php/189423>\
**Category:** NGINX\
**Created:** [May 31, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423 "2010-05-31T19:09:15Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/1 "2010-05-31T19:09:15Z")

</div>

Hi there I am trying to load balance my website between 2 servers.

I have set them both up to mirror each other

But they are a forum and using PHP ???

This is the code I have but it will only use one site.

It only uses the 127.0.0.1 local address and will not contact my other  
PHP server ?

I opened the port 9000 on both machines via iptables ???

```auto
upstream newsite {
  server 127.0.0.1:9000;
  server 15.82.114.196:9000;
  }

     server
     {
             listen 80;
             server_name thissite.com;
             index index.html index.htm index.php;
             root /data0/htdocs/www/forum;

             location ~ .*\.php?$
             {
                  include fcgi.conf;
                  fastcgi_pass newsite;
                fastcgi_index index.php;

             }
```

Thank you very much

Posted at Nginx Forum:

> **[Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,92738#msg-92738)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 8:54pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/2 "2010-05-31T20:54:38Z")

</div>

```auto
upstream newsite {
  server 127.0.0.1:9000;
  server 15.82.114.196:9001;
  }

server
     {
             listen 80;
             server_name thissite.com;
             index index.html index.htm index.php;
             root /data0/htdocs/www/forum;

             location ~ .*\.php?$
             {
                  include fcgi.conf;
                  fastcgi_pass newsite;
                fastcgi_index index.php;

             }
```

I think this might be a permission thing ? but I have no idea how to fix  
it

Still getting the:

no input file specified

Not sure how to make the main server work with my other server ?

Thank you very much

Posted at Nginx Forum:

> **[Re: Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,92775#msg-92775)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 9:09pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/3 "2010-05-31T21:09:17Z")

</div>

Check the script\_filename in fcgi.conf (post it) it should be in caps  
too but I’m on my iPhone 🙂

If that looks okay then make sure the file is accessable in the right  
location on the remote hosts. Cause that’s how PHP throws a 404 (that  
is so annoying)

Jerome maybe php-fpm sapi could be patched to throw a 404 header and  
something resembling a proper error? 😉

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 7:44pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/4 "2010-05-31T19:44:22Z")

</div>

I managed to get it working KIND OFF lol.

I edited the php file

nano /etc/rc.d/init.d/php\_cgi

There you can set the IP address and the port for php and it will start  
with your ip and port of choice.

I now have another problem ☹

This is my code and it is load balancing 50% to each server now 🙂

but I get 50% of pages white with the words:

no input file specified

Thank you for any help

Posted at Nginx Forum:

> **[Re: Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,92753#msg-92753)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 9:30pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/5 "2010-05-31T21:30:56Z")

</div>

Hi Mike

I do not have a fcgi.conf ?

I have a spawn-fcgi

```auto
/usr/bin/spawn-fcgi
```

Thanks Mike

Mick

Posted at Nginx Forum:

> **[Re: Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,92787#msg-92787)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 9:43pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/6 "2010-05-31T21:43:49Z")

</div>

Sorry Mike lol

Could it be that one server is running fpm and the other is cgi ?

```auto
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;

```

Posted at Nginx Forum:

> **[Re: Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,92794#msg-92794)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 9:49pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/7 "2010-05-31T21:49:05Z")

</div>

i know that typically the error is due to this not being defined right:  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;

are you sure that on the remote server that  
/data0/htdocs/www/forum/path/to/file.php exists?

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 9:33pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/8 "2010-05-31T21:33:17Z")

</div>

your nginx’s conf:

include fcgi.conf;

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/9 "2010-05-31T22:17:46Z")

</div>

Must they be identical on both machines ?

Because I have it running, if I go to the IP address.  
IE  
[http://newserverip](http://newserverip)

Then I can view the forums.

So should I make the home directories exactly the same on both machines  
?

Regards  
Mick

Posted at Nginx Forum:

> **[Re: Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,92809#msg-92809)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 10:20pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/10 "2010-05-31T22:20:41Z")

</div>

the SCRIPT\_FILENAME being passed is from the nginx machine to the PHP  
fastcgi engines

PHP is taking that and saying “okay, i need to execute this pathname as  
PHP”

so yes, if it’s not the same, you’ll have issues. 😛

there’s probably some other creative ways to mangle it per machine but  
for sanity you should keep things in sync anyway. a simple stack is a  
happy stack.

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/11 "2010-05-31T23:01:05Z")

</div>

Yeah thanks mike that worked.

The slave server is very slow ☹

Dunno why its going so slow but thanks for all your help

Mick

Posted at Nginx Forum:

> **[Re: Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,92830#msg-92830)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [June 1, 2010, 11:43am UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/12 "2010-06-01T11:43:44Z")

</div>

Thank you very much for your time MIKE 😉

You are right it was not worth the hassle of doing it this way ☹

I have decided to use my 4 servers like this

1. Private server (not open to public) - MYSQL
2. Weserver - PHP
3. Image Server
4. attachment server.

This way I can reduce load of attachments and images and leave the PHP  
to the 2) Webserver.

Thanks Mike I still need help but I think I better start a new thread  
with new parameters.

Cheers for the help

Mickie

Posted at Nginx Forum:

> **[Re: Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,93057#msg-93057)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [June 1, 2010, 6:18pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/13 "2010-06-01T18:18:57Z")

</div>

Nginx can handle both php and static assets easily on the same  
machine. No need to have them on separate machines. I think you need  
some architecture help 🙂

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [May 31, 2010, 11:14pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/14 "2010-05-31T23:14:32Z")

</div>

well it looks like you’re using a public IP, which means it’s over the  
WAN, which you should make sure is firewalled (iptables or something)  
so only the webserver(s) can connect, and depending on the latency  
between the machines it might be so slow that it doesn’t make sense.

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [June 1, 2010, 6:35pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/15 "2010-06-01T18:35:57Z")

</div>

Thanks Mike you are probably right but I have high amounts of concurrent  
users on my site at any one time… from 500 - 800.

I found fine tuning xcache, and memcache today has really helped out my  
server the pages are zipping open now.

But I still want to offset the images and memcached to my new server…  
with nginx.

When you are using proxy\_pass do you have to use network (local) ip’s or  
should you use public ip’s ?

Thank you

Mick

Posted at Nginx Forum:

> **[Re: Load Balancing PHP](https://forum.nginx.org/read.php?2,92738,93201#msg-93201)**
>
> Hi there I am trying to load balance my website between 2 servers. I have set them both up to mirror each other But they are a forum and using PHP ??? This is the code I have but it will only use one site. It only uses the 127.0.0.1 local a

---

<div class="post-metadata">

**Author:** ![Mickie\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@Mickie\_D](https://www.ruby-forum.com/u/Mickie_D)\
**Post date:** [June 1, 2010, 6:48pm UTC](https://www.ruby-forum.com/t/load-balancing-php/189423/16 "2010-06-01T18:48:50Z")

</div>

Always use private connections where you can. For php fastcgi engines,  
mysql, memcached, everything.
