Page load speed is everything. Tests done by Amazon have shown that an increase in page loading times by 100ms can reduce sales by 1%; when Google added 500ms to its response times traffic dropped 20%. The premise is simple: a faster website means faster feedback to the user which enables a faster user learning curve.
If like me you have a website that is powered by the LLMP (Linux Lighttpd MySQL PHP) stack then there are some simple steps you can take to decrease your page load times. If your running Apache and not Lighttpd then maybe its time to move
On your server
- Install PHP caching software such as APC Cache – this reduces the time needed to compile a PHP script as it caches the PHP byte code and just executes it again if the script hasn’t changed. Once you have set it up don’t forget to go into your ini files, adjust the cache size, enable apc and turn it on for cli mode (if your using fast-cgi)
apc.enabled=1
apc.shm_size=24
apc.enable_cli=onshm_size is always in MB. Don’t specify it like 24MB or it might not work.
- Make use of the MySQL query cache – most people have this turned off by default. The cache will cache result sets from queries and only adjust them if the underlying tables have changed. The line you want to add to your ini file is
query_cache_size = 8M
- Add eTags and expires headers to your static content. To do this you want to make sure mod_expire is enabled
server.module = (”mod_expire”)
And that you have something similar to
expire.url = (”/blog/” => “access 5 weeks”)
In your lighttpd.conf file – you want the item expire date to be as far in the future as you can afford.
- Enable GZIP compression on all CSS, Javascript, html etc.. anything that can be compressed well. Its not worth enabling this on files that are already compressed such as JPEG and PNG files. To enable gzip make sure you have mod_compress enabled
server.module = (”mod_compress”)
And that you specify what you want to compress eg.
compress.filetype = ( “text/plain”, “text/html”, “text/css”, “text/javascript”, “image/svg+xml”, “application/x-javascript”)
All of those types must be found within the mimetype.assign listing in lighttpd.conf. You must enable compression on dynamic pages via PHP – to enable gzip compression in your php output you need to add
zlib.output_compression = 1
zlib.output_handler =
zlib.output_compression_level = 2
output_buffering = Offto your php.ini file
- Turn off x-powered-by in php – this header is sent out at the top of every php script and is wasting bandwidth. To turn off x-powered-by add
expose_php=0
to your php.ini file. This will also stop any client from working out what php version you are running.
- Make sure keep-alive is turned on – especially for SSL (there is a 7 step handshake involved in SSL just to initialize the connection – you don’t want to be doing that every time you send a request to the server)
server.max-keep-alive-requests = 64
server.max-keep-alive-idle = 10max-keep-alive-requests should be set to at least the maximum number of external elements on any of your pages. So if you have a favicon, a css sheet and an image that would be 3.
In your code
- Optimize your image files – optipng can compress your PNG files much more effectively than normal image processing applications such as GIMP and Photoshop (this compression is also lossless so you wont loose any detail) – The smaller the file the quicker it will download.
optipng -o7 file.png
- Minify all CSS and javascript. This removes all white space within your CSS and JS files and makes for a faster download. Yahoo! YUI compressor can be used for both CSS and JS.
java -jar yuicompressor.jar myfile.js -o myfile-min.js
java -jar yuicompressor.jar myfile.css -o myfile-min.css - Keep your cookies small or host content that doesn’t require them on a separate cookie free domain. Keeping them small should be easy, keeping them on a cookie free domain can be a real hassle.
- Use CSS sprites to reduce the number of downloads from your server – this effectively turns loads of small files into 1 big file and so reduces the number of requests that need to be made.
Other tips are to move the Javascript to the base of the page (if you can – its not always possible), minimize any redirects and avoid 404 errors etc.. If you think that i’ve missed anything major in this list then let me know.
Tags: CSS, JavaScript, Lighttpd, MySQL, PHP, Web development