LAMP Server Performance Tips

Solution 1:

It really depends on your workload.

  • for the L part

    • get a lot of memory,
    • if you can go over 4GB, go 64bit.
    • for partitions where your content, logs and MySQL data are use mount options: noatime, nodiratime.
    • use separate physical drives / raid sets, ideally keep SQL data, logs, content you serve - each on separate spindle.
  • for the A part of your stack - well maybe you want to replace it completely with nginx or lighthttpd, or maybe just leave Apache for dynamic content and have separate server (like those two or mathopd) for static content. Take a look here for more options. If you are going to run both Apache and another server at the same box, a 2nd IP address will be handy. To decrease latency for the end-user use http/1.1 with keep-alive. Consider using a CDN for static content.

  • for the M part of your lamp - take a look at mysqlperformanceblog. from the top of my head:

    • log slow queries,
    • give enough memory,
    • consider using innodb.
    • if you have a lot of text to search across - use sphinx and have a batch job that rebuilds the index.
    • consider killing queries that run longer than XYZ seconds. It's better to upset 1% of users than to bring the whole site down at the peak time. But that really depends if you process cash transactions or show nice pictures.
    • use memcached if you can, to cache result of more 'expensive' SQL queries. Keep in mind to invalidate the cache when you change content of SQL. On the other hand I have quite few sites where all data fits in memory comfortably and for that MySQL is blazing fast and there is no need of additional cache.
  • for P

    • set execution timeout for scripts.
    • consider using some PHP accelerator / opcode cache. I was quite satisfied with xcache, but I don't use it now.
    • if you have CPU intensive processing - cache results and store them in SQL or memcached

Not really a performance tip, but do take offsite backups. Really.

Solution 2:

I really suggest separating MySQL and Apache/PHP on two different machines.

For example, I had one machine (C2D E6600) that always spiked to 2.0 and above load average. I put the MySQL on a second machine (P4C 3Ghz) and after that both load averages did not go above 0.2-0.3. So I went from a really slow site to a fast site with two servers having a lot of performance margin.

Tags:

Lamp