Nginx and php-fastcgi on Debian 6 (Squeeze)

Source: Internet
Author: User
Tags vps fully qualified domain name
Nginx and php-fastcgi on Debian 6 (Squeeze)

Published:monday, May 9th, 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 via FastCGI on your Debian 6 (Squeeze) 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

Set the Hostname Install Required Packages Configure Virtual hostingtest PHP with FastCGI Create directories UNIX Sockets Configuration Example TCP Sockets configuration Example Important Security considerations Enable and Start Services more I nformation

Set the Hostname

Before you to begin installing and configuring the components described in this guide, please make sure your ' ve followed our I Nstructions for setting your hostname. Issue the following commands to make sure it is set properly:

Hostname

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install Required Packages

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

Apt-get Update
Apt-get Upgrade

Configure Virtual Hosting

Create Directories

The This guide, the domain "example.com" are used as an example site. You should substitute your own domain name in the configuration steps that follow. First, create directories to hold content and log files:

Mkdir-p/srv/www/www.example.com/public_html
Mkdir/srv/www/www.example.com/logs

UNIX Sockets Configuration Example

Next, you'll need to define the site ' s virtual host file. This example uses a UNIX socket to connect to Fcgiwrap. Be sure to change all instances of "example.com" to your domain name.

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

server {
Error_log/srv/www/www.example.com/logs/error.log;
root/srv/www/www.example.com/public_html;
Location/{
Index index.htmlindex.htm;
}
Location ~ \.php$ {
Include/etc/nginx/fastcgi_params;
Fastcgi_passunix:/var/run/php-fastcgi/php-fastcgi.socket;
Fastcgi_index index.php;
Fastcgi_param Script_filename/srv/www/www.example.com/public_html$fastcgi_script_name;
}

Create a file named/usr/bin/php-fastcgi with the following contents:

file:/usr/bin/php-fastcgi

#!/bin/bash
Fastcgi_user=www-data
Fastcgi_group=www-data
Socket=/var/run/php-fastcgi/php-fastcgi.socket
Pidfile=/var/run/php-fastcgi/php-fastcgi.pid
Children=6 php5=/usr/bin/php5-cgi
/usr/bin/spawn-fcgi-s $SOCKET-P $PIDFILE-C $CHILDREN-u $FASTCGI _user-g $FASTCGI _group-f $PHP 5

Make it executable by issuing the following command:

TCP Sockets Configuration Example

Alternately, wish to use TCP sockets instead. If So, modify your Nginx virtual host configuration file to resemble the following example. Again, make sure to replace all instances of ' example.com ' with your domain name.

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

server {
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;
}
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;
}

Create a file named/usr/bin/php-fastcgi with the following contents:

file:/usr/bin/php-fastcgi

php5=/usr/bin/php5-cgi  

Make it executable by issuing the following command:

Important Security Considerations

If you ' re planning to run applications. Support file uploads (images, for example), the above configurations may expos E-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.

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 Unix:/var/run/php-fastcgi/php-fastcgi.socket;
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 Unix:/var/run/php-fastcgi/php-fastcgi.socket;
}
Fastcgi_index index.php;
Fastcgi_param Script_filename/srv/www/www.example.com/public_html$fastcgi_script_name;

Enable and Start Services

Issue the following commands to enable the site:

cd/etc/nginx/sites-enabled/

Create a file named/etc/init.d/php-fastcgi with the following contents:

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

#!/bin/bash
php_script=/usr/bin/php-fastcgi
Fastcgi_user=www-data
Fastcgi_group=www-data
pid_dir=/var/run/php-fastcgi
Pid_file=/var/run/php-fastcgi/php-fastcgi.pid
Ret_val=0
Case "$" in
Start
if [[!-D $PID _dir]]
Then
mkdir $PID _dir
Chown $FASTCGI _user: $FASTCGI _group $PID _dir
chmod 0770 $PID _dir
Fi
if [[-R $PID _file]]
Then
echo "php-fastcgi already running with PID ' cat $PID _file '"
Ret_val=1
Else
$PHP _script
Ret_val=$?
Fi
;;
Stop
if [[-R $PID _file]]
Then
Kill ' Cat $PID _file '
RM $PID _file
Ret_val=$?
else  echo "Could not find PID file $PID _file"
Ret_val=1
Fi
;;
Restart
if [[-R $PID _file]]
Then  kill ' cat $PID _file '
RM $PID _file
Ret_val=$?
Else
echo "Could not find PID file $PID _file"
Fi
$PHP _script
Ret_val=$?
;;
Status
if [[-R $PID _file]]
Then
echo "php-fastcgi running with PID ' cat $PID _file '"
Ret_val=$?
Else
Echo ' Could not ' find PID file $PID _file, php-fastcgi does not appear ' be running '
Fi
;;
*)
echo "usage:php-fastcgi {start|stop|restart|status}"
Ret_val=1
;;
Esac

Start php-fastcgi and Nginx by issuing the following commands:

chmod +x/etc/init.d/php-fastcgi
UPDATE-RC.D php-fastcgi Defaults
/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/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.

The Nginx homepage FastCGI Project homepage PHP documentation Basic Ngnix Configuration
  • 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.