Nginx supports FastCGI technology to work with many external tools and servers. PHP itself can be runned as FastCGI application and can process FastCGI requests from nginx.
So, first of all we need to install PHP with fastcgi support and run it on some local tcp port. Installation process can go different ways, but I will describe here how to compile PHP from sources because it is generic way. To get fastcgi-enabled version of PHP interpreter, you may use following commands:
CODE:
# ./configure --prefix=/opt/php --enable-fastcgi
...
# make
...
# make install
...
#When all of these commands will be completed, you will be able to run your fastcgi server. But there are two different ways to do it:* Running PHP’s built-in FastCGI server - this method don’t require any third party tools.
* Running PHP inside some third-party wrapper - it can be more comfortable than first method because of more flexibility.
If you want to run PHP using its built-in FastCGI manager, you can use following script:
CODE:
#!/bin/bash
## ABSOLUTE path to the PHP binary
PHPFCGI="/opt/php/bin/php"
## tcp-port to bind on
FCGIPORT="8888"
## IP to bind on
FCGIADDR="127.0.0.1"
## number of PHP children to spawn
PHP_FCGI_CHILDREN=5
## number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=1000
# allowed environment variables sperated by spaces
ALLOWED_ENV="ORACLE_HOME PATH USER"
## if this script is run as root switch to the following user
USERID=www-data
################## no config below this line
if test x$PHP_FCGI_CHILDREN = x; then
PHP_FCGI_CHILDREN=5
fi
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN"
ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS"
ALLOWED_ENV="$ALLOWED_ENV FCGI_WEB_SERVER_ADDRS"
if test x$UID = x0; then
EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID"
else
EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT"
fi
echo $EX
# copy the allowed environment variables
E=
for i in $ALLOWED_ENV; do
E="$E $i=${!i}"
done
# clean environment and set up a new one
nohup env - $E sh -c "$EX" &> /dev/null &If you want to use third party software to start PHP as FastCGI-server,you can take a look at spawn-fcgi from Lighttpd project.So, your PHP FastCGI server has been started and now last thing you need to do is to configure nginx server to forward php-requests to PHP’s tcp-port. It can be done with following config file snipet (full version is here):
CODE:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:8888
#
location ~ .php$ {
fastcgi_pass 127.0.0.1:8888;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}That is all! Now you can use your nginx to serve any PHP-enabled sites and its performance will be very close to Apache mod_php module, but you will get more memory to process more requests from your site visitors.As always, if you have some questions or suggestions, you can post them in comments area or email me. If you like this article, please vote for it on digg.com.
From: http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/