HowtoinstallPHP-fpm + NginxonCentOS5.3

Source: Internet
Author: User
Tags php cli sapi
./Configure -- pid-path =/usr/local/nginx/logs/nginx. pid -- sbi

In this tutorial I will try to install php-fpm with nginx on centos 5.3. But first:

What is PHP-FPM?

PHP-FPM is a patch for PHP4/5 to greatly improve PHP's FastCGI SAPI capabilities and administration. this means that you don't install php through your distribution's package manager, but rather download the package from the PHP-website, and the patch from another site. you patch up the sourcecode, compile things, and get it started.


The following is a comparison chart of problems and how php-fpm handles them, when enabled with the FastCGI SAPI:

Description Php "out of the box" Spawn-fcgi + spawn-php.sh + daemontools Php-fpm
Php daemonization: pid file, log file, setsid (), setuid (), setgid (), chroot () (-) (+) (+)
Process Management. Ability to "graceful" stop and start php workers without losing any queries. The possibility of gradually update the configuration and binary without losing any queries. Php4 (-), php5 (only graceful completion) (-) (+)
Restricting ip addresses from which requests can come from a web server Php4 (-), php5 (+) (from 5.2.2) (-) (+)
Dynamic number of processes, depending on the load (-) (-) TODO
Starting the workers with different uid/gid/chroot/environment and different php. ini option. You do not need a safe mode! (-) (-) (+)
Logging stdout & stderr business processes (-) (-) (+)
Emergency restart all the processes in the event of accidental destruction of shared memory opcode cache, if used accelerator (-) (-) (+)
Forcing the completion of process if set_time_limit () fails (-) (-) (+)

Compare to spawn-fastcgi installation, php-fpm installation is much slower and you can refer to http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/ for spawn-fcgi installation. Okay let's start the installation.

Installation

1. Install some dependecies first
# Yum install gcc libjpeg-devel libpng-devel libmcrypt-devel pcre-devel

2. We need to get PHP-sourcecode and php-fpm patch, in this tutorial I use php-5.2.10 and php-5.2.10-fpm-0.5.13
# Wget http://id.php.net/get/php-5.2.10.tar.bz2/from/us.php.net/mirror
Wget http://php-fpm.org/downloads/php-5.2.10-fpm-0.5.13.diff.gz

3. Extract and run the patch
# Tar xvf php-5.2.10.tar.bz2
# Gzip-cd php-5.2.10-fpm-0.5.13.diff.gz | patch-d php-5.2.10-p1

4. Configure and compile cd php-5.2.10 and hp-5.2.10-fpm-0.5.13 patch
#. /Configure -- enable-fastcgi -- enable-fpm -- with-mcrypt -- enable-mbstring -- enable-mysql -- with-mysql =/usr/include/mysql -- with-mysql-sock = /tmp/mysql. sock -- with-curl -- with-sockets -- with-gd -- with-zlib -- with-iconv -- with-dom -- with-jpeg-dir =/usr/lib
Make
Make install

You shoshould see this on the end of installation


Installing php sapi module: cgi
Installing php cgi binary:/usr/local/bin/
Installing FPM config:/usr/local/etc/php-fpm.conf
(Installing as php-fpm.conf.default)
Installing init. d script:/usr/local/sbin/php-fpm
Installing php cli binary:/usr/local/bin/
Installing php cli man page:/usr/local/man/man1/
Installing build environment:/usr/local/lib/php/build/
Installing header files:/usr/local/include/php/
^ [[BInstalling helper programs:/usr/local/bin/
Program: phpize
Program: php-config
Installing man pages:/usr/local/man/man1/
Page: phpize.1
Page: php-config.1
Installing PEAR environment:/usr/local/lib/php/
Pear/pear dependency package "pear/Archive_Tar" installed version 1.3.3 is not the recommended version
1.3.2, but may be compatible, use -- force to install
Pear/Archive_Tar cannot be installed, conflicts with installed packages
[PEAR] Archive_Tar-upgraded: 1.3.3
[PEAR] lele_getopt-already installed: 1.2.3
[PEAR] Structures_Graph-already installed: 1.0.2
[PEAR] XML_Util-installed: 1.2.1
[PEAR] PEAR-upgraded: 1.8.0
Wrote PEAR system config file at:/usr/local/etc/pear. conf
You may want to add:/usr/local/lib/php to your php. ini include_path
Installing PDO headers:/usr/local/include/php/ext/pdo/

5. Installing Init Script for PHP-FPM
# Cd/etc/init. d/
# Ln-s/usr/local/sbin/php-fpm

Add this to/etc/rc. local if you want php-fpm to start from booting
# Vi/etc/rc. local
/Etc/init. d/php-fpm start

6. Installing and configure nginx, when I write this tutorial the latest stable versions are nginx-0.7.61 you can go to http://nginx.net/for latest nginx sourcode
Wget http://sysoev.ru/nginx/nginx-0.7.61.tar.gz
Tar xvf http://sysoev.ru/nginx/nginx-0.7.61.tar.gz
Cd nginx-0.7.61
. /Configure -- pid-path =/usr/local/nginx/logs/nginx. pid -- sbin-path =/usr/local/sbin/nginx -- with-md5 =/usr/lib -- with-sha1 =/usr/lib -- with-http_ssl_module -- with-http_dav_module -- without-mail_pop3_module -- without-mail_imap_module
# Make
# Make install

7. Installing Nginx Daemon for CentOS, you can copy paste this script to your/etc/init. d/nginx

#! /Bin/sh
### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $ local_fs $ remote_fs
# Required-Stop: $ local_fs $ remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Example initscript
# Description: This file shocould be used to construct scripts to be
# Placed in/etc/init. d.
### END INIT INFO
#
# Author: Ryan Norbauer
#
Set-e
PATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC = "nginx daemon"
NAME = nginx
DAEMON =/usr/local/sbin/$ NAME
PIDFILE =/usr/local/nginx/logs/$ NAME. pid
SCRIPTNAME =/etc/init. d/$ NAME
# Gracefully exit if the package has been removed.
Test-x $ DAEMON | exit 0
# Read config file if it is present.
# If [-r/etc/default/$ NAME]
# Then
#./Etc/default/$ NAME
# Fi
#
# Function that starts the daemon/service.
#
D_start (){
Start-stop-daemon -- start -- quiet -- pidfile $ PIDFILE \
-- Exec $ DAEMON \
| Echo-n "already running"
}
#
# Function that stops the daemon/service.
#
D_stop (){
Start-stop-daemon -- stop -- quiet -- pidfile $ PIDFILE \
-- Name $ NAME \
| Echo-n "not running"
}

#
# Function that sends a SIGHUP to the daemon/service.
#
D_reload (){
Start-stop-daemon -- stop -- quiet -- pidfile $ PIDFILE \
-- Name $ NAME -- signal 1
}

Case "$1" in
Start)
Echo-n "Starting $ DESC: $ NAME"
D_start
Echo "."
;;
Stop)
Echo-n "Stopping $ DESC: $ NAME"
D_stop
Echo "."
;;
# Reload)
#
# If the daemon can reload its configuration
# Restarting (for example, when it is sent a SIGHUP ),
# Then implement that here.
#
# If the daemon responds to changes in its config file
# Directly anyway, make this an "exit 0 ".
#
# Echo-n "Reloading $ DESC configuration ..."
# D_reload
# Echo "done ."
#;;
Restart | force-reload)
#
# If the "reload" option is implemented, move the "force-reload"
# Option to the "reload" entry above. If not, "force-reload" is
# Just the same as "restart ".
#
Echo-n "Restarting $ DESC: $ NAME"
D_stop
# One second might not be time enough for a daemon to stop,
# If this happens, d_start will fail (and dpkg will break if
# The package is being upgraded). Change the timeout if needed
# Be, or change d_stop to have start-stop-daemon use -- retry.
# Notice that using -- retry slows down the shutdown process somewhat.
Sleep 1
D_start
Echo "."
;;
*)
Echo "Usage: $ SCRIPTNAME {start | stop | restart | force-reload}"> & 2
Exit 3
;;
Esac
Exit 0

Or you can download it from http://www.magnet-id.com/download/nginx/nginx-daemon
Wget http://www.magnet-id.com/download/nginx/nginx-daemon-O/etc/init. d/nginx
And don't forget to set the permission
# Chmod 750/etc/init. d/nginx

Install start-stop-daemonvand add nginx on the start up

Wget http://www.magnet-id.com/download/nginx/apps-sys-utils-start-stop-daemon-IR1_9_18-1.tar.gz
Tar zxvf apps-sys-utils-start-stop-daemon-IR1_9_18-1.tar.gz
Cd apps/sys-utils/start-stop-daemon-IR1_9_18-1/
Gcc-o start-stop-daemon start-stop-daemon.c
Cp start-stop-daemon/usr/sbin
Chkconfig -- add nginx
Chkconfig -- level 345 nginx on

Nginx. conf configuration

Go to nginx configuration file: "/usr/local/nginx/conf/nginx. conf", and add the php configuration

Location ~ \. Php $ {
Root/usr/share/nginx/html; # itmena the root of the coument are located on/usr/share/nginx/html
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/usr/share/nginx/html $ fastcgi_script_name;
Include fastcgi_params;
}

Go to/usr/share/nginx/html and create php files
# Vi index. php

8. Start the nginx and php-fpm
# Service php-fpm start
# Service nginx start
9. You shoshould go to your domain or server address and see the phpinfo

Related Article

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.