Setting up Nginx plus PHP FastCGI on Debian 6.0.6 Squeeze

Valic —  November 24, 2012 — 2 Comments

This article is about setting up Nginx + PHP-FastCGI on Debian 6.0.6 Squeeze

Step1. Install nginx and php5-cgi :

apt-get install nginx php5-cgi

Step2. Replace Nginx default virtualhosts config with the following content:

server {
listen [::]:80;
server_name debian-tutorials.com;
root /var/www/debian-tutorials.com;
index index.html index.htm index.php;
access_log /var/log/debian-tutorials.com.access.log;

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

NOTE: Replace debian-tutorials.com with your own domain.

Step3. Create a  Debian init script to control the php-cgi daemon.

nano /etc/init.d/php-fcgi

And paste the following to it:

#!/bin/bash
BIND=/tmp/php.socket
USER=www-data
PHP_FCGI_CHILDREN=2
PHP_FCGI_MAX_REQUESTS=5000
PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0
start() {
echo -n "Starting PHP FastCGI: "
start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
RETVAL=$?
echo "$PHP_CGI_NAME."
}
stop() {
echo -n "Stopping PHP FastCGI: "
killall -q -w -u $USER $PHP_CGI
RETVAL=$?
echo "$PHP_CGI_NAME."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: php-fcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL

Step4. Make the file executable and make it to start on boot.

chmod +x /etc/init.d/php-fcgi
invoke-rc.d php-fcgi start

Step5. Now create a phpinfo file to check that it is working properly.

echo "<?php phpinfo(); ?>" > /var/www/phpinfo.php

Now go to http://your_domain.tld/phpinfo.php and you will see the PHP information page.

References:
Nginx: http://nginx.org/
nginx Fcgiwrap: http://wiki.nginx.org/Fcgiwrap

Enjoy.

Valic

Posts Twitter Facebook

Editor in Chief at Debian-Tutorials, Linux enthusiast.

2 responses to Setting up Nginx plus PHP FastCGI on Debian 6.0.6 Squeeze

  1. I have encountered two potential problems when connecting to php-fcgi via sockets instead of TCP/IP for high loads. Make sure to monitor your nginx error logs for potential upstream timeouts. The number of simultaneous allowed sockets might have been reached and/or the socket connection backlog may overflow and drop requests. You might have to tweak /proc/sys/net/core/somaxconn and/or /proc/sys/net/core/netdev_max_backlog

Trackbacks and Pingbacks:

  1. Debian copy/paste Tutorials - Linux Tutorials - How to Customize Server Header using NginX headers-more module - March 4, 2013

    [...] You can use this guide to do it: Setting up Nginx plus PHP FastCGI on Debian 6.0.6 Squeeze [...]

Leave a Reply