Ketil Froyn's blog

Main blog

Sun, 11 Apr 2010

Optimizing LAMP - the Linux Apache MySQL PHP stack - for speed, without touching any application code

There are a bunch of pretty easy things you can do to optimize your website for speed, improving the user experience and scalability of your website - without even touching your code!

This is Ketil's checklist of easy optimizations that will probably give your site a healthy speed boost:

  1. Don't let MySQL do a DNS lookup for every single connect from the webserver to the SQL server. Avoid this by adding the following option to the [mysqld] section of my.cnf:
    Make sure to always use IP-addresses in the GRANT command if you do this.
  2. Compiling the php script before execution can take hundreds of milliseconds - for every hit to your webserver. Install and enable PHP APC on your webserver to cache the compiled code, avoiding compilation delays for every access. You don't need to write any code to use this, even though APC can be used for other sorts of caching as well
  3. Browsers need to download and parse css and javascript files before showing anything to the user. Speed up the download by using HTTP level compression. Install and enable the headers and deflate modules on your webserver (deflate is usually already enabled), and make sure your css and javascript files also get compressed with this configuration in deflate.conf
    <IfModule mod_deflate.c>
            AddOutputFilterByType DEFLATE text/html text/plain text/xml
            AddOutputFilterByType DEFLATE application/xhtml+xml text/javascript text/css application/x-javascript application/javascript
            BrowserMatch ^Mozilla/4 gzip-only-text/html
            BrowserMatch ^Mozilla/4\.0[678] no-gzip
            BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
            Header append Vary User-Agent env=!dont-vary
  4. Let browsers cache static files for up to a month. Install and enable the expires module on your webserver, and add a config something like this to your virtualhost config to let browser cache images for up to 1 month:
            ExpiresActive On
            ExpiresByType image/gif "access plus 1 month"
            ExpiresByType image/jpeg "access plus 1 month"
            ExpiresByType image/jpg "access plus 1 month"
            ExpiresByType image/png "access plus 1 month"

For further improvements, some more advanced, please check back for the next posting on this subject.


posted at: 15:16 | path: /2010/04 | permanent link to this entry