I would like to know if an error 504 Gateway timeout always come from the load balancer or it can also be originated from Apache and the PHP code it's running ?
It usually happens when the load balancer is waiting for a response from apache and apache is not giving a response within the timeout period of load balancer. So it returns a 504 gateway timeout. About what causes apache to not respond is something only you can say as you have not given much background information on what apache is serving. Look at apache logs to know more details . usually access logs and error logs. Increase the timeout value of loadbalancer, to see if it helps. It is always good to have clustering on apache so you dont get 504 because of a dead apache instance. I think mod_proxy allows you to achieve software clustering. I am not sure check out on apache site.
Usually from balancing because Apache(mod_php) or PHP(FastCGI) down or busy.
Sure it can be from php. If php script is in some loop and wont 'spit' out the content connection will eventually time out and balancer will report gateway timeout.
That 'loop' does not necessarily need to be loop, it can be waiting on connection to some service to read data from like mysql/memcache or something...