In case you’ve been visiting my web pages, you’ve probably noticed that they’ve been getting slower over time. Most of the reason behind this had to do with Apache2 and the way that it launches new PHP servers – combined with my low cost 256mb Rackspace VPS (only $11 a month) and a combination of websites including about 5 WordPress blogs another handful of open source projects (including thinkup), some stuff being tried for work, and my old company (Gifted Soft.)
Nginx has been getting a lot of attention as the new way to run PHP, Ruby, and Django applications as it’s lightweight like lighttp, but is significantly cleaner – and lacks some memory leaks. The install was pretty easy on Debian / Ubuntu, just had to do
apt-get install nginx
And then virtual servers are set up very similarly to apache with a sites-available and site-enabled directory (though no nginx_ensite that I could find)
Initially, I setup my new cloud server with spawn-fcgi, which worked to start php-fastcgi. Unfortunately, before the end of the night, I noticed my web pages returning bad gateways (502) thanks to Rigor’s emails.
I did some research – and found the php5-fpm would fix a lot of my crashes. I read the reports about the new FPM in the newest versions of php and its many improvements. Installing php-fpm worked really easily as well.
Already Ran Out Of Memory
Next, I noticed that, under load, my mysql instance was crashing. Default Debian error logs only log to /var/log/syslog (so I also set up a conf line to log to /var/log/mysql/error.log) because the system was running out of memory (and this default Debian setup didn’t come with a swap setup, so mysqld was being prematurely shut down.)
More research showed me that php-fpm was starting 20 instances along with some other default pool settings. A little digging around and using my old Apache worker trials, I’ve arbitrarily set the following for php-fpm.
pm = dynamic pm.max_children = 30 pm.start_servers = 5 pm.max_requests = 50 pm.min_spare_servers=3 pm.max_spare_servers=5
Which should shutdown memory leaks better and keep my php processes refreshed. I also changed some of my apc settings.
Ruby With Passenger
A lot of the reason that I switched to Nginx is for the prospect of doing more work in Ruby and publishing easily (and performance-ly) with some trial projects instead of just used WebBrick. Installing Ruby through the RVM and setting up passenger on Nginx has a bajillion guides on the internet.
Premature Optimization with MySql Percona & APC
I’ve been experimenting a lot with a few optimizations to make things even better. I’ve recently checked out AriaDB and Percona forks of MySQL. I’m using Percona on my server simply because AriaDB is still on 5.3 (versus 5.5) though Maria probably has some nice benefits for Worpress pages that have little advantage of going InnoDB / XtraDB route. One confusing aspect is that AriaDB has an option for the stock InnoDB and lists XtraDB, but Percona only lists InnoDB (which is really XtraDB)
APC allows for a little extra caching for things. But with a low memory footprint, it seems like a good idea to limit the memory available for APC. I’m sticking with 32mb for now. I’m also setting a really high TTL for unchanged files (stat will recache any changed files anyway)