Now running with nginx!

I was really fed up with Apache on this server. It would use huge amounts of RAM, even after all the visitors left the website. Having done all tweaks to the memory usage of Apache and PHP, the amount of RAM used would never get below 450MB (out of the 512MB this VPS has). Hell, Apache was consuming even more memory than MySQL!

For those who don’t know, nginx is an alternative, lightweight webserver which is generally used (by many popular websites) as a load balancer. However, it can also act as the single web server on a system, like what Apache and Lighttpd do. I had worked with nginx before on some small websites on low-resource servers, and I was quite satisfied with it. As I explained with an earlier blog post, nginx is great as long as the website you want to serve with it does work with nginx – that is, doesn’t heavily depend on Apache rules or some Apache-specific thing. Sure, those rules can be converted to nginx config options, but I never succeeded on making eyeOS 1.x work fully with nginx.

WordPress is one of the scripts that works best with nginx. Since this website is mainly powered by nginx (although I have some custom scripts laying around, mainly the scripts providing alternative WiiMC internet media), I made my mind and decided I would go through the hassle of switching from Apache to nginx. It wasn’t a big hassle after all: apart from having to restart the server at some point due to a RAM outage, the website wasn’t offline much time, and there was no data loss.

After putting Apache off-use and starting nginx, the server was still using 300MB of RAM. I though nginx couldn’t be using so much RAM, and there was another problem laying around. Turns out to be a problem in MySQL config: I don’t need InnoDB functionality, so usually I add a “skip-innodb” line to my.cnf. The problem was, this line needs to be added under the [mysqld] section and in my case, it was somewhere else. So I moved skip-innodb to the right place, restarted MySQLd, and that’s it:

The server is now using 240MB of RAM, which still fits inside the dedicated RAM (256MB), so I’m not taking any of the burst RAM, which resides in the server swap space. The RAM usage is still high, because I have other things running such as dovecot for mail delivery.

It also looks faster to load pages, but probably someone with a faster connection than me will notice a bigger difference.

Apache, nginx or other web server?

Apache is a long well-known web server software, the most used in web servers around the internet. I met nginx when I started looking into getting free low-end VPS for my websites and scripts.

Basically, what I have learned is that most scripts won’t work well even if you convert the .htaccess’es into nginx config files. In my opinion, it’s simply not worth to get a VPS with less than 256MB of dedicated RAM, because on fewer RAM you won’t be able to run Apache properly (most of the times, Apache will just fill up the RAM leaving you locked out of your VPS because you can’t SSH as Linux doesn’t have enough free RAM to start a new shell session.

On servers with few RAM (512MB, for example) you can still run Apache and have some RAM free. If you have MySQL installed, the trick to reduce RAM consists on disabling inno-db and other database engines you don’t need. Apache can also be configured to waste less RAM on low traffic websites – you can reduce the number of max clients and connections, but this can make the site unresponsive (slow) if you get many hits at a time.

In conclusion, if you’re sure you can setup your script with nginx and cgi-php (or other lightweight webserver, e.g. lighthttpd), go for it. I have nothing against nginx, I only wish developers supported nginx (and lighthttpd and etc.) more instead of the already much explored (and at times exploited) Apache.