WHAT IS NGINX?
NginX (pronounced Engine-X) is supposedly the fastest HTTP server available. In terms of speed only LightTPD comes a close second. Most PHP developers start off with Apache as the de-facto web server to work with and spend more or less the rest of their lives working with it. Apache+PHP is more or less a standard configuration which works great. For Windows based developers ,XAMPP or WAMP is the starting point for a web server. For those who have explored deeper, NginX, LightTPD, GWAN, Cherokee etc. offer interesting capabilities.
NginX is different from Apache in its basic architecture. Apache is a process based webserver while NginX is an asynchronous event based webserver.
The main advantage of the asynchronous approach is scalability. In a process-based server, each simultaneous connection requires a thread which incurs significant overhead. An asynchronous server, on the other hand, is event-driven and handles requests in a single (or at least, very few) threads.
While a process-based server can often perform on par with an asynchronous server under light loads, under heavier loads they usually consume far too much RAM which significantly degrades performance. Also, they degrade much faster on less powerful hardware or in a resource-restricted environment such as a VPS.
NginX offers the following features:
- Static file serving.
- SSL/TLS support.
- Virtual hosts.
- Reverse proxying.
- Load balancing.
- Access controls.
- URL rewriting.
- Custom logging.
- Server-side includes.
- Limited WebDAV.
- FLV streaming.
Some benchmarks are given below. These graphs are from http://www.techtatva.com
It is interesting to note that as claimed by NginX, it definitely beats Apache in terms of speed. But there are very few sites which run on pure static HTML pages.
Since here we are concerned with PHP, you will note that Apache 2 beats NginX in terms of speed, though NginX comes in better than the rest. This however does not mean that Apache is better than NginX in serving PHP pages. Apache has an unfair advantage of being able to use mod_php whereas NginX (and the others) have to use PHP as a Fast CGI module. So maybe if Apache is configured with PHP as FastCGI, the results would be much different.
The third graph shows more or less the same performance across all web servers because Mysql is the bottleneck here.
THE APACHE VS.NGINX DEBATE
There is a lot of debate which has been going on for a long time whether to go for Apache or NginX. Most comparisons are based on speed. Most people agree that for static content, NginX beats Apache in terms of serving content,specially at high loads. Even if speed is debatable, the advantage of a smaller memory footprint used by NginX while handling high loads is very clear. Because Nginx is event-based it doesn’t need to spawn new processes or threads for each request, so its memory usage is very low .
This article is not about testing or benchmarking Apache against NginX. You can find such benchmarks in a lot of other places:
There are cases where Apache and NginX run together to give great performance, each focusing on its own strengths.
This article is aimed at people who are familiar with setting up Apache with PHP and want to port their existing php website to NginX. One of the main disadvantages which NginX supposedly has compared to Apache, is its lack of support for .htaccess files. This is not really true. Though NginX does not support .htaccess files, its configuration files provide the same functionality, so its not really an issue.
NginX.com is the official website. NginX is available for Linux/FreeBSD and Windows platforms.
Note that it is not recommended for production use under Windows . As per their official site:
“nginx for Windows uses the native Win32 API (not the Cygwin emulation layer). Only the select() connection processing method is currently used, so high performance and scalability should not be expected. Due to this and some other known issues version of nginx for Windows is considered to be a beta version.”
Installation instructions for Windows are available at http://nginx.org/en/docs/windows.html . This article wont be focusing on Windows.
Prebuilt packages are available for available as yum repositories for RHEL and CentOS.
For Debian and Ubuntu, packages are available for installation for apt-get
Complete instructions are available at http://nginx.org/en/download.html
nginx is available in the ports collection. You can either install it from source using portmaster or make. The port is available in /usr/ports/www/nginx . You can also install as a binary package using pkg_add though compiling it from source is recommended. Make sure you select the HTTP_REWRITE module before compiling.
In most cases, after the installation is complete, Nginx is ready to run. By default Nginx will run on port 80 for http and port 443 for HTTPS. So if you already have apache running on that port, its best to stop it before starting NginX.
The default document root is /usr/share/nginx/www.
The default document root is /var/www which is not there by default and needs to be created.
The default document root is /usr/share/nginx/html
The default document root is /usr/local/www/nginx/
Fire up your browser and type in http://localhost or your local IP http://192.168.0.100. You should see the page below:
END OF PART 1
At this stage , you have successfully installed NginX. You already have Apache and PHP installed. Mysql may or not be installed as part of your PHP app development, but this really makes no difference to installing NginX since Apache or NginX have nothing to do with Mysql. It is PHP which interacts with Mysql.
In the next part we will see about
- Configuring NginX to handle multiple virtual hosts
- Making it work with PHP
- Taking care of htaccess rules for a website