Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

178 lines (132 sloc) 8.177 kb
author description keywords license alias modified modified_by published title
name email
Serve dynamic websites and applications with the lightweight nginx web server and PHP-FastCGI on CentOS 5.
nginx,nginx centos 5,nginx fastcgi,nginx php
Tuesday, May 17th, 2011
Monday, December 14th, 2009
Nginx and PHP-FastCGI on CentOS 5

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 CentOS 5 Linux VPS.

It is assumed that 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.

Basic System Configuration

Issue the following commands to set your system hostname, substituting a unique value for "hostname." :

echo "HOSTNAME=hostname" >> /etc/sysconfig/network
hostname "hostname"

Edit your /etc/hosts file to resemble the following, substituting your Linode's public IP address for, your hostname for "hostname," and your primary domain name for "" :

{: .file } /etc/hosts : ~~~ localhost.localdomain localhost hostname ~~~

Install Required Packages

CentOS doesn't include nginx in their repositories, so we'll need to add support for EPEL (Extra Packages for Enterprise Linux) from the Fedora project. Issue the following command:

rpm -Uvh

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

yum update
yum install nginx php-cli php make automake gcc gcc-c++ spawn-fcgi wget
chkconfig --add nginx
chkconfig --level 35 nginx on
service nginx start

You'll be asked to accept the key for EPEL, as it gets imported the first time you install an EPEL package. 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 "Remote Access" tab in the Linode Manager). You should get the default ngnix page.

Configure Your Site

In this guide, we'll be using the domain "" 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/
mkdir /srv/www/
chown -R nginx:nginx /srv/www/

Issue the following commands to create virtual hosting directories:

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

Add the following lines to your /etc/nginx/nginx.conf file, immediately after the line for include /etc/nginx/conf.d/*.conf:

{: .file-excerpt } /etc/nginx/nginx.conf : ~~~ nginx # Load virtual host configuration files. include /etc/nginx/sites-enabled/*; ~~~

Next, define your site's virtual host file:

{: .file } /etc/nginx/sites-available/ : ~~~ nginx server { server_name; access_log /srv/www/; error_log /srv/www/; root /srv/www/;

    location / {
        index index.html index.htm index.php;

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/$fastcgi_script_name;

Important security note: If you're planning to run applications that support file uploads (images, for example), the above configuration may expose you to a security risk by allowing arbitrary code execution. The short explanation for this behavior is that a properly crafted URI which ends in ".php", in combination with a malicious image file that actually contains valid PHP, can result in the image being processed as PHP. For more information on the specifics of this behavior, you may wish to review the information provided on Neal Poole's blog.

To mitigate this issue, you may wish to modify your configuration to include a try_files directive. Please note that this fix requires nginx and the php-fcgi workers to reside on the same server.

location ~ \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /srv/www/$fastcgi_script_name;

Additionally, it's a good idea to secure any upload directories your applications may use. The following configuration excerpt demonstrates securing an "/images" directory.

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    if ($uri !~ "^/images/") {
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /srv/www/$fastcgi_script_name;

After reviewing your configuration for potential security issues, issue the following commands to enable the site:

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/
service nginx restart

You may wish to create a test HTML page under /srv/www/ 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

Issue the following command sequence to download scripts to control spawn-fcgi and php-fastcgi, set privileges, make the init script run at startup, and launch it for the first time:

cd /opt
wget -O
mv /usr/bin/php-fastcgi
chmod +x /usr/bin/php-fastcgi
wget -O
mv /etc/rc.d/init.d/php-fastcgi
chmod +x /etc/rc.d/init.d/php-fastcgi
chkconfig --add php-fastcgi
chkconfig php-fastcgi on
/etc/init.d/php-fastcgi start

Test PHP with FastCGI

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

{: .file } /srv/www/ : ~~~ php <?php echo phpinfo(); ?> ~~~

When you visit 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

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.

Jump to Line
Something went wrong with that request. Please try again.