After my first post about nginx web and reverse-proxy server, I have received many e-mail comments and questions. One of the most popular questions was “How to use PHP with nginx?”. This small howto-like article is about how to do it.
Since this article has been published long time ago, there is MUCH better option to manage PHP FastCGI processes than the one I described below. Please check out php-fpm project from Andrei Nigmatulin which IMHO is the best possible way to manage PHP processes.
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:
# ./configure --prefix=/opt/php --enable-fastcgi
# 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:
## ABSOLUTE path to the PHP binary
## tcp-port to bind on
## IP to bind on
## number of PHP children to spawn
## number of request before php-process will be restarted
# allowed environment variables sperated by spaces
ALLOWED_ENV="ORACLE_HOME PATH USER"
## if this script is run as root switch to the following user
################## no config below this line
if test x$PHP_FCGI_CHILDREN = x; then
if test x$UID = x0; then
EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID"
EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT"
# copy the allowed environment variables
for i in $ALLOWED_ENV; do
# 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):
GeSHi Error: GeSHi could not find the language nginx (using path /home/hosting/kovyrin/kovyrin.net/releases/20131207204950/wp-content/plugins/codecolorer/lib/geshi/) (code 2)
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.