How to improve your website speed in four easy steps

It’s fairly easy to improve your AMP (Apache, MySQL, PHP) website speed:

1. Enable HTTP KeepAlives and persistent connections

2. Enable server-side output compression

3. Enable MySQL query caching

4. Install a PHP accelerator and optimizer


 

1. Enable HTTP KeepAlives and persistent connections

From Apache 2.2 manual:
The Keep-Alive extension to HTTP/1.0 and the persistent connection feature of HTTP/1.1 provide long-lived HTTP sessions which allow multiple requests to be sent over the same TCP connection. In some cases this has been shown to result in an almost 50% speedup in latency times for HTML documents with many images. To enable Keep-Alive connections, set KeepAlive On in your httpd.conf file [1].

 

2. Enable server-side output compression

Use Apache Module mod_deflate to enable on the fly server-side output compression before being sent to the client over the network. Compression decreases the bandwidth usage and also decreases the page load time [2].

First, make sure the mod_deflate module is loaded in httpd.conf:

LoadModule deflate_module mod_deflate.so

Enable compression for text files:

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript

 

3. Enable MySQL query caching

The MySQL query cache is a unique caching strategy that is currently not utilized by other database engines, and one that can greatly enhance the performance of most any system that experiences high degrees of read activity. Because both physical and logical I/O activity is all but eliminated by the query cache, even systems that must bear the brunt of inefficient SQL statements can many times perform faster than those on other database platforms. [3]

To enable MySQL query caching, add the following two lines under [mysqld] block in your my.cnf (/etc/my.cnf on RHEL / CentOS) file:

query_cache_type = 1
query_cache_size = 16M

and restart your mysql server.

 

4. Install a PHP accelerator and optimizer

I use eAccelerator which is a free open-source PHP accelerator & optimizer. It increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compiling is almost completely eliminated. It also optimizes scripts to speed up their execution. eAccelerator typically reduces server load and increases the speed of your PHP code by 1-10 times.

On RHEL / CentOS you can install eAccelerator from Fedora’s Extra Packages for Enterprise Linux (EPEL):

yum install php-eaccelerator

and restart your web server:

service httpd restart

The configuration file is at /etc/php.d/eaccelerator.ini, default settings should work for most users.
Precompiled PHP files are stored in /var/cache/php-eaccelerator/.

To display old and stale cache files, run:

find /var/cache/php-eaccelerator/ -type f -atime +30

 

[1] Apache Core Features – KeepAlive
[2] Measuring the Performance Effects of mod_deflate in Apache 2.2
[3] A Practical Look at the MySQL Query Cache