Optimising Your Web Stack Performance for Magento
One of the key performance aims is to get as much out of your platform without getting to the point where it is overloaded. The slowest component on modern computers is usually the hard drives. Anything you can do to reduce the need to read or write from the hard drives is ideal. A major cause of disk usage and general slow performance is if your server needs to use swap (or virtual) memory in order to function. Swap memory is used when the physical RAM on your machine becomes full - RAM contents must be ‘swapped’ to disk in order to make room.
The key to avoiding swapping is to configure your system within its limits. If you only have 256MB of RAM, there’s no point allocating half of it to MySQL then expecting your machine to happily run 5 Apache/mod_php processes.
If you have access-time logging enabled on any of your mysql, web server or cache partitions, try turning it off for a performance boost. If you’re using ext3 or reiserfs there may be faster journal write methods you can use. For more information see http://www.linux.com/feature/116693.
Apache is a hugely popular web server (the A in LAMP). It can serve PHP sites using mod_php, FastCGI or CGI.
NginX is becoming an increasingly popular alternative to Apache and Lighttpd due to its stability, light weight and speed. NginX can server PHP sites using FastCGI or CGI.
Wordpress, Comodo, Hulu, MagentoCommerce and other enterprises choose this platform to run their websites on.
Lighttpd is another popular lightweight alternative to Apache. Like NginX it is able to run PHP as either FastCGI or CGI. Lighttpd contains a nice tool - spawn-fgi for managing FastCGI processes
* Set memory limit to 64M
mod_php for Apache is probably the most popular way to run PHP applications. There are a few down-sides to mod_php though: you’re forced to use Apache’s less efficient prefork MPM and your Apache processes will get large. Serving static files with 32MB+ Apache processes isn’t very efficient.
When PHP is run as a CGI program the web server spawns a process each time a request is made. This can be less efficient unless your operating system is good at spawning processes quickly. Unlike mod_php, CGI application won’t bloat your Apache, so the processes can remain small for service static files - important for high traffic sites.
When PHP is run as a FastCGI, a number of daemon processes are started and sit waiting for the web server to request them to run PHP code. This avoids the cost of spawning new processes each time in addition to not bloating web server processes. Due to the nature of long-running processes, FastCGI may require some periodic process maintenance. Lighttpd’s spawn-fgi script provides a method to limit the number of requests a single FCGI process is allowed to serve in an attempt to reduce the effect of memory leakage.
PHP Opcode caches reduce the processing overhead involved in reading and compiling PHP scripts. They utilise shared memory as a way of storing and re-using compile scripts.
APC http://uk2.php.net/apc is a free PHP opcode cache written by the PHP team and packaged using PECL. APC can be used with mod_php, FastCGI and CGI.
Most operating systems install a lightweight MySQL configuration by default, a good LAMP server should provide a decent amount of its resources to MySQL. Machines with multiple cores and over 2GB RAM need to be configured carefully to make sure available RAM and cores are being used efficiently.
There are some excellent tips in this Magento blog post http://www.magentocommerce.com/blog/comments/performance-is-key-notes-on-magentos-performance/
After your server has been running for at least 48 hours the “MySQL Performance Tuning Primer Script” analyses your server statistics against your configuration and offers guidance: http://www.day32.com/MySQL/
InnoDB (http://www.innodb.com/) is an alternative storage engine which makes use of MySQL’s client/server architecture. Whilst it shares some configuration settings with MyISAM (MySQL’s default storage engine) it has many settings of its own and will compete for server resources with MyISAM. It’s possible to tune MySQL server purely for InnoDB if no MyISAM tables are being used. Magento does however use MyISAM for some small tables so MyISAM is still required.
On larger sites MySQL’s replication functionality can be used to spread SELECT queries across multiple machines. Writes operations will still be run on every server in order to maintain data synchronisation.
Magento uses file caching to store often-used data in an easy to use format. The speed of file caching is directly dependent on speed of the filesystem on which the cache files are being read or written. Again, since hard drives are slow moving this to RAM can really help. Having said that, operating systems will attempt to buffer reads/writes in memory in order to increase speed.
tmpfs is an in-memory filesystem designed to deliver the best possible performance.
Apache with mod_proxy using the worker MPM is a popular reverse proxy. There are probably also much leighter-weight alternatives. Setting up effect reverse proxies requires a good understanding of the application being proxied and the way in which it is used.