After using Lighttpd for a couple of months, I wasn’t really happy with it, so I have decided to move from Lighttpd to Nginx, which is another open-source lightweight web server. In this tutorial, I am going to go over setting up Nginx, MySQL, and PHP5 on an Ubuntu server.
1. Install Nginx

Code:
aptitude install nginx
For some reason Nginx does not start automatically when it is installed, so we need to start Nginx.
Code:
/etc/init.d/nginx start
Now we want to make sure that Nginx installed properly. Open your web browser and point it to your servers IP address. If all is working fine you should see a page that says “Welcome to nginx!”


2. Install MySQL

Code:
aptitude install mysql-server mysql-client
3. Install PHP5

Code:
aptitude install php5-cgi php5-mysql php5-curl php5-gd  php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache  php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp  php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json
Now edit /etc/php5/cgi/php.ini
nano /etc/php5/cgi/php.ini
and add:
Code:
cgi.fix_pathinfo = 1
to the end of the file.
4. Install Lighttpd

I know.. Your saying “but I wanted Nginx!” The reason we are installing Lighttpd is because Nginx does not come with a FastCGI package, and there isn’t a standalone package yet. So we are going to install Lighttpd, disable it, and use spawn-fcgi from the Lighttpd package.
Code:
aptitude install lighttpd
You will get an error message saying that port 80 is already in use. That’s fine because we don’t care about running Lighttpd anyway. Now we will remove Lighttpd from our servers startup:
Code:
update-rc.d -f lighttpd remove
5. Setup spawn-fcgi

Since we are going to use spawn-fcgi to handle PHP, we need to set it up to start when our server starts and make an init script so that we can control the processes. For starters, create the init script:
Code:
nano /etc/init.d/php-fastcgi
and add the following code to it and save it:
Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-fastcgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop php-cgi in external FASTCGI mode
# Description:       Start and stop php-cgi in external FASTCGI mode
### END INIT INFO

# Author: Kurt Zankl <[EMAIL PROTECTED]>

# Do NOT "set -e"

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="php-cgi in external FASTCGI mode"
NAME=php-fastcgi
DAEMON=/usr/bin/php-cgi
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
PHP_CONFIG_FILE=/etc/php5/cgi/php.ini

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# If the daemon is not enabled, give the user a warning and then exit,
# unless we are stopping the daemon
if [ "$START" != "yes" -a "$1" != "stop" ]; then
        log_warning_msg "To enable $NAME, edit /etc/default/$NAME and set START=yes"
        exit 0
fi

# Process configuration
export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS
DAEMON_ARGS="-q -b $FCGI_HOST:$FCGI_PORT -c $PHP_CONFIG_FILE"

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
                || return 1
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
                --background --make-pidfile --chuid $EXEC_AS_USER --startas $DAEMON -- \
                $DAEMON_ARGS \
                || return 2
}

do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE > /dev/null # --name $DAEMON
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
        # Wait for children to finish too if this is a daemon that forks
        # and if the daemon is only ever run from this initscript.
        # If the above conditions are not satisfied then add some other code
        # that waits for the process to drop all resources that could be
        # needed by services started subsequently.  A last resort is to
        # sleep for some time.
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
        [ "$?" = 2 ] && return 2
        # Many daemons don't delete their pidfiles when they exit.
        rm -f $PIDFILE
        return "$RETVAL"
}
case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac
There is no editing needed for the code above. Now we need to make it executable.
Code:
chmod +x /etc/init.d/php-fastcgi
Now we will create the configuration file for spawn-fcgi:
Code:
nano /etc/default/php-fastcgi
and add:
Code:
START=yes # Which user runs PHP? (default: www-data)
 EXEC_AS_USER=www-data
 # Host and TCP port for FASTCGI-Listener (default: localhost:9000)
 FCGI_HOST=localhost
FCGI_PORT=9000
 # Environment variables, which are processed by PHP
 PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
In this file, you can change the FCGI_PORT, PHP_FCGI_CHILDREN, and PHP_FCGI_MAX_REQUESTS. If you change the port, make sure to note what you change it to because we will need it for later. Now, we want to make sure that spawn-fcgi starts when the server starts:
Code:
update-rc.d php-fastcgi defaults
6. Configure Nginx and your default vhost

For help with configuring Nginx beyond just the default configuration, please refer to the Nginx Wiki. We are now going to configure the default vhost so that we can verify that PHP is working with Nginx.
/etc/nginx/sites-available/default
In the default vhost, change the following lines to look like this:
Code:
        server_name  _;
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /var/www/nginx-default$fastcgi_script_name;
                include fastcgi_params;
        }
What we did here was changed the server_name to accept all incoming requests, uncommented the location stanza and defined where Nginx needs to look for spawn-fcgi. If you changed the port above in the /etc/default/php-fastcgi file, then you need to change the port on the fastcgi_pass line as well. On the line fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;, you need to change /var/ww/nginx-default to match your web root. On the line include fastcgi_params; you need to add a space between the include and the fastcgi_params as there is a bug in the default configuration and that space was omitted. Once you save the default-vhost, you need to restart Nginx.
Code:
/etc/init.d/nginx restart
Now we will create an info.php file in your web root:
Code:
nano /var/www/nginx-default/info.php
Add the following code and save the file:
Code:
<?php
phpinfo();
?>
Now in your web browser, pull up the newly created file (http://your-ip/info.php). You should see information about the version of PHP that you are running.
Note: When I installed Nginx, it did not start automatically, however it was setup to start when the server booted. I have heard stories of users having issues with Nginx starting automatically when the server boots. If Nginx does not start when you boot your server, run the following command which will make it start on boot:
Code:
update-rc.d nginx defaults
Enjoy!
References:
Nginx Wiki
Nginx, PHP and a PHP FastCGI daemon init script
Installing Nginx With PHP5 And MySQL Support On Ubuntu 8.10