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;
server_name domain.com www.domain.com;
location / {
root /var/www/www.domain.com;
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_pass 127.0.0.1:9005;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/www.domain.com$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”
#!/bin/bash
PHP_SCRIPT='/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9005 -u www-data -C 8 -f /usr/local/bin/php-cgi'
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
pkill -9 php-cgi
RETVAL=$?
;;
restart)
pkill -9 php-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
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.