FacebookTwitter

A little server optimisation

Well instead of redesigning this site as I've been meaning to do for over a month now I decided to get it running a little better and optimise the server big time for running dynamic sites.
Originally my servers ran Apache2 and mod_php with a MySQL database backend just installed from the aptitude install commands, however I noticed that even under not much strain the server would get bogged down and eventually just crash out needing a reboot. It was about time I stopped thinking about the front-end of the site and looked into the system hosting it, as if the site went down it doesn't matter how pretty it is!
I started reading about all sorts of ways to get Apache's memory footprint down, and how to optimise Apache as it stood, which worked to a degree but I still wasn't happy with the results as there was still too much lag for my liking. This is when I started looking at alternative web server implementations, enter Nginx.
Nginx (pronounced Engine-X even though I still pronounce it en-jinx) is a small lightweight web server implementation with all the bloat of apache removed. The disadvantage to this was the there was no mod_php for including PHP with the server, and the rewrites were completely different so took a lot of getting used to! Another great thing about Nginx is that out of the box it supports multi-core systems, and as I run a 4-core server I wanted to make use of all 4 cores to get more headroom, which until this point using the standard Apache2 setup for some reason wasn't possible. To get PHP working on Nginx I had to use FCGI PHP which runs PHP as separate processes which is great to keep memory usage down when not serving PHP pages and content as PHP isn't always running in the background as it does with mod_php.
This setup ran great for about a week and still is running a few sites I have hosted however I STILL wasn't happy with the performance so yet again looked at other methods of doing things, enter Apache-mpm-worker. This is a type of Apache server which creates 'worker' nodes for multi-core (threaded) serving which is exactly what I wanted! This method of running Apache also reduces the amount of memory needed for each worker as mod_php again isn't usable with it just like Nginx. FCGI PHP was the way to go again with this setup and once I'd installed this I already noticed a difference in the stability and speed of the server.
With the platform decided I set about optimising things a fair bit I started on the Apache processes, once that was sorted the PHP was next on my hit list. Those optimisations managed to get the memory usage a little lower however there was still the job of getting the page load times down to a more respectable level. For this I decided to use some op-code cache systems which included eAccelerator, APC, and memcache. I had the best results with eAccelerator however the latest version of this has removed the user entries which I needed while running Drupal sites. APC was next on my hit list and that gave absolutely tremendous results, for the PHP scripts I ran on the server, I also installed memcache to split out more cache bins and use only a specific amount of memory for those bins.
This again increased site responsiveness and also reduced the CPU load on the server which was awesome. I'm now in the process of migrating all my sites across to this server and phasing out my other servers. When I get more time I'll look into making a post with my full config files in just incase anyone else is interested in doing this sort of thing also.