Nginx (engine-x) is a web server that is regarded to be faster than Apache and with a better performance on heavy load. The difference is summed up succinctly in a quote by Chris Lea on the Why Use Nginx? page: "Apache is like Microsoft Word, it has a million options but you only need six. Nginx does those six things, and it does five of them 50 times faster than Apache."
Technically speaking, Apache is a process-and-thread-driven application, while Nginx is event-driven. In practice this means that Nginx needs much less memory than Apache to do the work, and also can work faster. There are claims that Nginx, working in a server of 512MB RAM, can handle 10,000 (yes, ten thousands) concurrent requests without problem, while Apache with such a load would just commit harakiri (suicide). Besides, the configuration of Nginx, once you get used to it, is simpler and more intuitive than that of Apache.
It seemed like something that I should definitely give a try, since my web server already had performance problems and I cannot afford to pay for increasing its capacity. Here I describe the steps for installing and configuring Nginx to suit the needs of my web application (which is based on Drupal7, running on a 512MB RAM server at Rackspace).
Table of Contents
- Installing nginx and php5-fpm
- Configuring php5-fpm
- Configuring nginx
- Configuration for phpMyAdmin
- SSL (HTTPS) support
- Avoid any DOS attacks
- Full configuration of the site
1. Installing nginx and php5-fpm
In ubuntu server this is very easy:
2. Configuring php5-fpm
The main config file (
/etc/php5/fpm/php-fpm.conf) did not need to be changed at all.
On the pool configuration file (
/etc/php5/fpm/pool.d/www.conf) I made only some small modifications:
- Listen to a unix socket, instead if a TCP socket:
- Other modified options:
I also made these modifications on
Finally restarted the service php5-fpm:
3. Configuring nginx
On ubuntu, the configuration of Nginx is located at
- Create a configuration file for the website, based on the drupal example configuration file:
/etc/nginx/sites-enabled/btranslator_devmodify servername and root, and also add accesslog and errorlog:
/etc/nginx/sites-enabled/btranslator_dev, modify the name of the unix socket at the fastcgipass line:
/etc/nginx/sites-enabled/btranslator_dev, add the index line as well, at the root location:
/etc/nginx/sites-enabled/btranslator_dev, allow only localhost to access txt and log files:
/etc/nginx/nginx.conf, decrease worker processes to 1 or 2:
These modifications are all we need, and then we can reload or restart the nginx service:
4. Configuration for phpMyAdmin
Add these lines inside the server section, at
Then reload the nginx service.
5. SSL (HTTPS) support
Add these lines at
Since SSL connections have some overhead, to make them more efficient, add these lines as well at
/etc/nginx/nginx.conf(in order to increase session timeout and and use less expensive encryption):
Then reload nginx.
6. Avoid any DOS attacks
In order to avoid any DOS attacks, add these lines at
7. Full configuration of the site
A full version of the file
/etc/nginx/sites-enabled/btranslator_devlooks like this:
Date: 2012-10-22 01:00:10 CEST
HTML generated by org-mode 6.33x in emacs 23