Php – How to configure the server to handle big traffic

apache-2.2configurationPHPtraffic

Right now I have 1000 concurrent users online. But my site is very slow. I could have 8000 concurrent users, but the server can't handle the traffic, and I'm losing visitors.

My server is a VPS server:

Dedicated CPU
7.2GHz

RAM
4512MB

The memory usage is way below the available resources. (right now 632 MB) What should i do? Should i configure the apache server? If yes, how? What about caching, maybe memcached?

This is a virtual server, built up from nodes, each node representing 600 Mhz cpu and 376MB ram.

I'm running a website on it built with php and mysql.

Best Answer

Benchmark, benchmark, benchmark. That's the only solution. There are a million things that can make a website slow. Here's what I would do:

  • Load up Firefox with Firebug. Check the net panel for the loading times of your main request. If it gives a long time for "connecting", you may have network or bandwidth issues, or Apache cannot handle that many requests at the same time. If there's a long "waiting" or "transferring" time, then your PHP scripts could be the culprit. They take too long to render the page.
  • When dealing with PHP, the first suspect is usually your (My)SQL server. If you use a central SQL library, it should not be too hard to benchmark your queries. Just track how many queries your page needs, and how long those queries take. Compare that with the total page rendering time. If you have an old app that uses plain mysql(i)_ functions all over the place, then jump down two bullits.
  • If the issue is with MySQL then the first suspect is usually proper indexes. Run an EXPLAIN on all your queries and see if indexes are used. Define proper indexes. Re-benchmark. I/O is also a common cause of MySQL slowdown (use a bigger query cache, issue less queries (application cache), tweak your MySQL settings or just split the webserver and SQL server.
  • If the issue is PHP itself, load up xdebug and run your page through the profiler (make sure only for your request or you could easily kill your server). Load up the dump in KCacheGrind and figure out where your application spends all it's time. Refactor.

This is just what I would do. Every step depends very much on the results you got in the previous step. There is no generic advice that we can give you. Just benchmark, benchmark, benchmark and refactor your code and server setup accordingly.

Related Topic