Nginx and php-fastcgi on CentOS 5

Source: Internet
Author: User
Tags vps
Nginx and php-fastcgi on CentOS 5

Published:monday, December 14th, by Phil Paradis

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

It is assumed so you ' ve already followed the steps outlined in our Getting Started guide. These steps should is performed via a root login to your Linode VPS over SSH.

Contents

Basic System Configuration Install Required Packages Configure Your Site Configure spawn-fcgi Test PHP with FastCGI more I nformation

Basic System Configuration

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

echo "Hostname=plato" >>/etc/sysconfig/networkhostname "Plato"

Edit your/etc/hosts file to resemble the following, substituting your Linode's public IP address for 12.34.56.78, your ho Stname for "Plato," and your primary domain name for "example.com.":

File:/etc/hosts

127.0.0.1   localhost.localdomain   localhost12.34.56.78 plato.example.com   Plato

Install Required Packages

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

RPM-UVH http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

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

Yum updateyum install nginx php-cli php make automake gcc gcc-c++ spawn-fcgi wgetchkconfig--add nginxchkconfig--level 35 Nginx Onservice nginx Start

You'll be asked to accept the key for EPEL, as it gets imported the first time you install a EPEL package. Various additional dependency packages would be installed along with the ones we requested. Once the installation process finishes, you could wish to make sure Nginx was running by browsing to your Linode ' s IP address (Found 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 "example.com" as our example site. You should substitute your own domain name in the configuration steps that follow. First, we'll need to the create directories to the our content and log files:

Mkdir-p/srv/www/www.example.com/public_htmlmkdir/srv/www/www.example.com/logschown-r nginx:nginx/srv/www/ www.example.com

Issue the following commands to create virtual hosting directories:

Mkdir/etc/nginx/sites-availablemkdir/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/*.co NF ":

File excerpt:/etc/nginx/nginx.conf

# Load Virtual Host configuration files.include/etc/nginx/sites-enabled/*;

Next, define your site ' s virtual Host file:

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

server {    server_name www.example.com example.com;    Access_log/srv/www/www.example.com/logs/access.log;    Error_log/srv/www/www.example.com/logs/error.log;    root/srv/www/www.example.com/public_html;    Location/{        index index.html index.htm index.php;    }    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.example.com/public_html$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 a security risk by allowing arbitrary code execution. The short explanation-behavior is a properly crafted URI which ends in ". php", in combination with a Malicio US 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, your may wish to review the information provided on Neal Poole ' s BL og.

To mitigate this issue, the wish to modify your configuration to include a try_files directive. Please note, 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_pass 127.0.0.1:9000;    Fastcgi_index index.php;    Fastcgi_param Script_filename/srv/www/www.example.com/public_html$fastcgi_script_name;}

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

Location ~ \.php$ {    include/etc/nginx/fastcgi_params;    if ($uri!~ "^/images/") {    fastcgi_pass 127.0.0.1:9000;    }    Fastcgi_index index.php;    Fastcgi_param Script_filename/srv/www/www.example.com/public_html$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/www.example.comservice nginx Restart

Wish to create a test HTML page Under/srv/www/www.example.com/public_html/and view it in your browser to verify That Nginx is properly serving your site (PHP won't work yet). Please note the This would 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 I NIT script run at startup, and launch it for the first time:

Cd/optwget-o php-fastcgi-rpm.sh HTTP://LIBRARY.LINODE.COM/ASSETS/696-PHP-FASTCGI-RPM.SHMV php-fastcgi-rpm.sh/usr/ Bin/php-fastcgichmod +x/usr/bin/php-fastcgiwget-o php-fastcgi-init-rpm.sh http://library.linode.com/assets/697- PHP-FASTCGI-INIT-RPM.SHMV Php-fastcgi-init-rpm.sh/etc/rc.d/init.d/php-fastcgichmod +x/etc/rc.d/init.d/ Php-fastcgichkconfig--add php-fastcgichkconfig 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/www.example.com/public_html/test.php

 
  

When you visit http://www.example.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

wish to consult the following resources for additional information on this topic. While these is provided in the hope that they would be useful, please note this we cannot vouch for the accuracy or Timeli Ness of externally hosted materials.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.