Linode Library Home
Categories
Getting Started
Beginner's Guide
Using Linux
Linode Manager
Networking
LAMP Guides
LEMP Guides
Web Servers
Apache
Nginx
Installation
Configuration
Perl-FastCGI
PHP-FastCGI
Cherokee
Lighttpd
Web Applications
Email Guides
High Availability
SSL Guides
Databases
Server Monitoring
Development
Communications
Advanced
Troubleshooting
How to Contribute
Sitemap
Linode Library RSS Feed
Linode Library Home :: Web Servers :: Hosting Websites with Nginx :: PHP-FastCGI with Nginx :: Nginx and PHP-FastCGI on Ubuntu 9.10 (Karmic)
Print View Download PDF Download RST

Nginx and PHP-FastCGI on Ubuntu 9.10 (Karmic)

Author: Phil ParadisExternal Link
Published: December 14, 2009
Revised: June 7, 2010

The nginx web server is a fast, lightweight server designed to efficiently handle the needs of both low and high traffic websites. Although commonly used to serve static content, it's quite capable of handling dynamic pages as well. This guide will help you get nginx up and running with PHP and FastCGI on your Ubuntu 9.10 Linux VPS.

We assume you've already followed the steps outlined in our getting started guide. These steps should be performed via a root login to your Linode VPS over SSH.

These instructions work with the Linode platform. If you don't have a Linode yet, sign up for a Linux VPS and get started today.

Install Required Packages Link

Make sure you have the "universe" repositories enabled in /etc/apt/sources.list. Your file should resemble the following:

File: /etc/apt/sources.list

## main & restricted repositories
deb http://us.archive.ubuntu.com/ubuntu/ karmic main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic main restricted

deb http://security.ubuntuu.com/ubuntu karmic-security main restricted
deb-src http://security.ubuntu.com/ubuntu karmic-security main restricted

## universe repositories
deb http://us.archive.ubuntu.com/ubuntu/ karmic universe
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic universe
deb http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe

deb http://security.ubuntu.com/ubuntu karmic-security universe
deb-src http://security.ubuntu.com/ubuntu karmic-security universe

Issue the following commands to update your system and install the nginx web server, PHP, and compiler tools:

apt-get update
apt-get upgrade
apt-get install nginx php5-cli php5-cgi spawn-fcgi

Various additional dependency packages will be installed along with the ones we requested. Once the installation process finishes, you may wish to make sure nginx is running by browsing to your Linode's IP address (found on the "Network" tab in the Linode Manager). You should get the default ngnix page.

Configure Your Site Link

In this guide, we'll be using the domain "bambookites.com" as our example site. You should substitute your own domain name in the configuration steps that follow. First, we'll need to create directories to hold our content and log files:

mkdir -p /srv/www/www.bambookites.com/public_html
mkdir /srv/www/www.bambookites.com/logs
chown -R www-data:www-data /srv/www/www.bambookites.com

Next, we'll need to define our site's virtual host file:

File: /etc/nginx/sites-available/www.bambookites.com

server {
    listen   80;
    server_name www.bambookites.com bambookites.com;
    access_log /srv/www/www.bambookites.com/logs/access.log;
    error_log /srv/www/www.bambookites.com/logs/error.log;

    location / {
        root   /srv/www/www.bambookites.com/public_html;
        index  index.html index.htm;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  /srv/www/www.bambookites.com/public_html$fastcgi_script_name;
    }
}

Issue the following commands to enable the site:

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/www.bambookites.com
/etc/init.d/nginx restart

You may wish to create a test HTML page under /srv/www/www.bambookites.com/public_html/ and view it in your browser to verify that nginx is properly serving your site (PHP will not work yet). Please note that this will require an entry in DNS pointing your domain name to your Linode's IP address.

Configure spawn-fcgi Link

Create a wrapper script for spawn-fcgi:

File: /usr/bin/php-fastcgi

#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u www-data -f /usr/bin/php5-cgi

Make the script executable with the following command:

chmod a+x /usr/bin/php-fastcgi

Create an init script for fast-cgi:

File: /etc/init.d/php-fastcgi

#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
FASTCGI_USER=www-data
RETVAL=0
case "$1" in
    start)
      su - $FASTCGI_USER -c $PHP_SCRIPT
      RETVAL=$?
  ;;
    stop)
      killall -9 php5-cgi
      RETVAL=$?
  ;;
    restart)
      killall -9 php5-cgi
      su - $FASTCGI_USER -c $PHP_SCRIPT
      RETVAL=$?
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

Issue the following commands to make the init script run at startup and launch it for the first time:

chmod 755 /etc/init.d/php-fastcgi
update-rc.d php-fastcgi defaults
/etc/init.d/php-fastcgi start

Test PHP with FastCGI Link

Create a file called "test.php" in your site's "public_html" directory with the following contents:

File: /srv/www/www.bambookites.com/public_html/test.php

<? echo phpinfo(); ?>

When you visit http://www.bambookites.com/test.php in your browser, the standard "PHP info" output is shown. Congratulations, you've configured the nginx web server to use PHP-FastCGI for dynamic content!

More Information Link

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

License Link

This guide is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States LicenseExternal Link. Please feel free to redistribute unmodified copies of it as long as attribution is provided, preferably via a link to this page.

Comments

Comment poster gravatar. Submitted by innov8ion on Monday, December 14 2009 at 20:02:14 GMT

PHP-FPM is better than spawn-fcgi and is being forked into PHP itself: http://php-fpm.org/

Comment poster gravatar. Submitted by Jason Morehouse on Monday, December 14 2009 at 20:08:39 GMT

spawn-fcgi is included in karmic.

`apt-get install spawn-fcgi`

Should do the job.

nginx + xcache + fastcgi is awesome!

Comment poster gravatar. Submitted by Phil Paradis on Monday, December 14 2009 at 22:46:24 GMT

@innov8ion - Thanks for the suggestion; we're sticking with spawn-fcgi for now, as it's well-supported and stable.

Comment poster gravatar. Submitted by Phil Paradis on Monday, December 14 2009 at 22:56:40 GMT

@Jason - Thanks for the tip :). The guide has been updated to reflect your suggestion.

Comment poster gravatar. Submitted by knight on Saturday, December 26 2009 at 01:17:39 GMT

i think is best to mention "Once the installation process finishes" and you need to start nginx '/etc/init.d/nginx start ' , else you won't be able to see the default ngnix page

Got a comment?

BBCode formatting is allowed. Email addresses are confidential, and are only used for gravatars and sending document/comment updates if requested. Please refer to our privacy policy. All comments are moderated and may take some time to appear on this page.