Nginx is an open source Web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols, with a strong focus on high concurrency, performance and low memory usage. It is licensed under a BSD-like license and it runs on Unix, Linux, BSD variants, Mac OS X, Solaris, AIX, HP-UX and Microsoft Windows. Nginx uses an asynchronous event-driven approach to handling requests, instead of the Apache HTTP Server model that defaults to a threaded or process-oriented approach. Nginx’s event-driven approach can provide more predictable performance under high loads.

Nginx and Lighttpd are probably the two best-known asynchronous servers. The main advantage of the asynchronous approach is scalability. The main advantages of Nginx is performance and efficiency. Whether you are looking to get the most out of your VPS or are attempting to scale one of the largest Web sites in the world, Nginx may be the best tool for the job. It’s fast, stable and easy to use.

Nginx has all the features you would expect from a leading Web server:

  • Static file serving.
  • SSL/TLS support.
  • Virtual hosts.
  • Reverse proxying.
  • Load balancing.
  • Compression.
  • Access controls.
  • URL rewriting.
  • Custom logging.
  • Server-side includes.
  • Limited WebDAV.
  • FLV streaming.
  • FastCGI.

To configure NGINX to handle PHP requests, you can write a virtual host entry as per the following format.

server {

listen serverip:80;
location / {
root /var/www/;
index index.php index.html index.htm;
access_log /var/log/nginx.vhost.access.log main;
location ~ .php$ {
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;

*Please make the necessary changes for the fields serverip, domain, and access_log

It assumes that the PHP is running as CGI and it should run as a daemon. To set up the same, you will have to install spawn-fcgi in the server and create an init script to start with. The following format can be used to create an init script, “/etc/init.d/php”

PHP_SCRIPT='/usr/bin/spawn-fcgi -a -p 9005 -u www-data -C 8 -f /usr/local/bin/php-cgi'
case "$1" in
pkill -9 php-cgi
pkill -9 php-cgi
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
exit $RETVAL

You should check and confirm that the binary path for spawn-fcgi and php are correct in the above scripts.

Restart nginx and start PHP. It should work.

Contact Us On WhatsApp