Published January 13th, 2008 in Linux • Optimization • Web ServerOne thing which I hate is slow sites. If I’m googling for something, and a page I’m trying to load is taking its time, I usually click the next interesting link. So when I had finished nuefoo using a pretty optimized Wordpress system, I was pretty let down with my cheap Dreamhost hosting (to be honest - what did I expect?).
As the months went on, the gap between my first blog post, and a new one was ever growing, mainly because I realised that anything I did post would probably never be seen, as if anything I did post actually got Dugg to the front page (which I would be utterly proud off), or even got linked by somebody, the visitors would either be put off by the slow loading (upwards of 5-10 seconds at times), or the fact that Wordpress would return the database “Wordpress can’t connect to your database” error or even worse, not load at all, leading to “lol digg killed ur server!!!” comments on digg. And remember this was a simple styled Wordpress blog, which I expect is utterly polished and optimized by several people - what would happen if I tried to run a custom app using the CakePHP framework?
So earlier last week, I decided that I would start looking for a new host, a VPS, or a cheap dedicated box. Luckily a post on Dominiek.com about how he built Wigitize.com led me to Slicehost, his chosen host for the project. Slicehost is based around the idea that every developer - from amateur to professional - needs good, well performing hosting for there small to mid-range webapps….without the price tag of a dedi.
I ordered my server a few days ago (Ubuntu Gusty, 256MB of RAM, 10GB HDD, 100GB bandwidth - $20 month, 3months prepaid), and it was delivered in, errrm, 4 seconds? Well, the time it took to charge my account for $60. Within a few more seconds I had Putty open and installing Apache, PHP, MySQL, etc.
It was nice and I was using around 140MB of RAM even after stress testing it with pretty low values in ApacheBench, but the pages were loading pretty fast, way faster than before anyway! But it just wasn’t enough for me, I knew I could squeeze a lot more power out of it; So over the next few days, I transferred my domain over, and started playing around with several ways to speed up the core of the system.
WP-Cache
First of all, for people who run a Wordpress blog, you will probably of heard of WP-Cache, it reduces the amount of queries and PHP execution immensely - but I won’t talk much about it today, it does a great job, and at the end of the day, if you use Wordpress - get it! Its a quick plugin, a few options to set, and your off! Woo!
Pros
+ Will speed your Wordpress blog up a lot for multiple pageviews of the same page within your desired expire limit.
+ Good if you really have no desire to move to a VPS or dedicated server, so can’t do any of the stuff below which requires you to install stuff.
Cons
- It helps, but it won’t make you digg or slashdot proof unless you are using a crap load of server power, you really shouldn’t just leave it at this if its possible to optimize other areas I mention below.
Lighttpd
Lighthttpd is a very light weight replacement for Apache, many large sites like Mininova & TPB use it due to the mass of hits they get daily, and you can tell why, it uses far less RAM than Apache, and can deliver way more pages per second. The only drawbacks really are the features you take for granted in Apache, such as .htaccess files, and VirtualHosts. Lighttpd does have its own versions of them, which are actually very good, but will take you a while to get used to if your at home with Apache.
Pros
+ Far better speeds, less RAM & CPU usage, and overall better performance over Apache with very little configuration.
+ Some very nice alternatives for Apache features. They may be different than your used too, but that doesn’t mean they are second best.
Cons
- Everybody uses Apache now, its simple to setup, and there’s tons of docs about all the different aspects, i.e. VirtualHosts. There is an awful lot less for Lighttpd, so you have to usually work it out yourself, search a lot harder on Google for what you need to know, or end up hopping on an IRC channel or post on a forum asking for help.
Memcached
While I haven’t really had much chance to test Memcached yet, the principle is very simple - caching stuff in memory is a very efficient way of keeping and serving the same data you need for several hundred or even thousand users, compared to reading it from disk every time.
Many big sites use it, for example Digg, go to to the digg.com homepage and log out (so you don’t get personalised results), thousands of people a day see those story titles and descriptions, and it takes a good few hours for the stories to get from the top of the page to the bottom, so why keep that information in a database, just to get it out, put it in your RAM, and use it as a variable in your code, when it can be kept in your RAM ready?
Though the biggest drawback is it needs to be implemented into every line where your application needs to to add, remove and update content which you wish to cache. If your just starting to build your PHP app, your going to have an easy time; But if you already have an app live, or want to use it in a system you didn’t build (Wordpress for example), this may be a bigger task. It also may work slower than your standard MySQL query only system if you have a small site with very few unique visitors on at any one time.
Pros
+ Excellent way of keeping information you need to deliver to a lot of people in a short amount of time.
+ Very nice PHP module and class to add, update, and replace keys into the cache, so its not hard to implement into your existing code.
Cons
- As mentioned above - it may be slower than a bog standard mysql_query() query, if you have very few (Maybe below 100?) visitors who visit your site at any one time.
- Far from a “quick fix” to your performance problems.
Other stuff….
Apart from those pretty major things to your web server setup, you can also do small things to MySQL and PHP to speed things up more!
You can optimize MySQL to save time and RAM, there’s tons of tips here for optimizing your installation of mysql, and 10 more at WhenPenguinsAttack for optimizing your actual queries. And don’t forget PHP, that’s one of the main things to get hammered on a site, and its also the thing which you can probably optimize till the end of time, check this out and tell me you tell me you do everything on the list 100% of the time….
Xcache or eAccelerator are both PHP optimizers which you should also consider, personally I have only used Xcache so far, but will be trying eAccelerator sometime to compare the two. In eAccelerator’s words, it “increases the performance of PHP scripts by caching them in their compiled state”, which I assume is the same with Xcache. Its another one of those tried and tested methods which while it makes no massive improvements on its own, every little helps when used with other things to speed your overall site up and lower process power of the server. A lot of people recommend them both.
I by no means all myself an expert, truth be told, this is the first time I have ever tried to optimize my web server apart from using the basic WP-Cache and sticking as best as I can do the preferred ways of doing things in MySQL and PHP, but I have been playing with it all the past few days, and I think I have done a pretty good job at making my setup faster than it could have been if I was lazy. I could have thrown double the price I pay for this VPS at a dedicated server, and I doubt it would be faster than it is now, if I had been lazy with it. More RAM would probably help if you get a burst of traffic, but I idle at around 73/256MB now, so I have a lot of space to grow if that does happen.
Have fun and happy optimizing! ![]()
2005-2007 A.W. - Except where explicitly stated, all rights are reserved, and content should not be copied, adapted, redistributed, or otherwise used without the prior written permission.
[…] Tu blog / sitio web […]
[…] final article is aimed particularly at the server but is also worth a read. It is entitled Don’t just wait for Digg to kill you - be prepared!. It looks at the “Digg effect” and how it can hammer your server. Definitely worth a […]
[…] Don’t just wait for Digg to kill you - be prepared! Ou comment faire pour que son serveur ne succombe pas à un afflux massif de visiteurs en provenance d’un site à fort trafic comme Digg ou TechCrunch. Cet article s’intéresse principalement aux blogs, mais il contient des conseils applicables à n’importe quel autre type de site. […]
There is a neat hack of WP-Cache for “Huge Speed Increase” at http://www.askapache.com/wordpress/wp-cache-speed-hack.html
It´s worth a try…