编译安装PHP及内置PHP-FPM

nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端(浏览器)。

nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回给nginx,然后nginx返回给浏览器。

下面将以php-fpm为例介绍如何使nginx支持PHP

0.什么是php-fpm

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的.

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以查看这里

1.安装前准备:编译环境及依赖

 编译环境:

yum -y install gcc automake autoconf libtool make gcc-c++ glibc

依赖包(按需安装):

yum -y install libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel

2.安装php及内置php-fpm

cd ~/download #若无此目录请创建
wget -O php-5.6.14.tar.gz http://cn2.php.net/get/php-5.6.14.tar.gz/from/this/mirror
tar zxvf php-5.6.14.tar.gz
cd php-5.6.14
./configure --enable-fpm \
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir

出现如下Thank you字样,表示配置成功:

再通过make进行编译,make install进行安装:

make
make install

最终安装后的目录路径信息如下:

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20131226/
Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/php/man/man1/
Installing PHP FPM binary:        /usr/local/sbin/
Installing PHP FPM config:        /usr/local/etc/
Installing PHP FPM man page:      /usr/local/php/man/man8/
Installing PHP FPM status page:      /usr/local/php/php/fpm/
Installing PHP CGI binary:        /usr/local/bin/
Installing PHP CGI man page:      /usr/local/php/man/man1/
Installing build environment:     /usr/local/lib/php/build/
Installing header files:          /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/lib/php/
[PEAR] Archive_Tar    - installed: 1.3.12
[PEAR] Console_Getopt - installed: 1.3.1
[PEAR] Structures_Graph- installed: 1.0.4
[PEAR] XML_Util       - installed: 1.2.3
[PEAR] PEAR           - installed: 1.9.5
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
/root/download/php-5.6.14/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f phar.phar /usr/local/bin/phar
View Code

3.给php创建专有的用户和组www-data

groupadd www-data
useradd -g www-data www-data

4.生成修改php-fpm的默认配置,并使php-fpm以www-data用户的身份运行:

cd /usr/local/etc/
cp php-fpm.conf.default php-fpm.conf
vi php-fpm.conf

找到并修改为以下行(149行):

user = www-data
group = www-data

找到并修改以下行(164行)(为了支持使用任意IP访问,为后面负载均衡做准备):

listen = [::]:9000

以上修改成功后,php-fpm是可以直接提供服务了

5.下面我们希望将其注册为服务,并可以方便的start/stop,需要如下工作

首先,修改php-fpm的配置文件,设置pid文件存放路径:

vi /usr/local/etc/php-fpm.conf

找到并修改如下内容(25行):

pid = /var/run/php-fpm.pid

在/etc/init.d/中我们创建一个服务脚本php-fpm

vi /etc/init.d/php-fpm

写入以下内容:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/usr
exec_prefix=/usr

php_fpm_BIN=/usr/local/sbin/php-fpm
php_fpm_CONF=/usr/local/etc/php-fpm.conf
php_fpm_PID=/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF"


wait_for_pid () {
        try=0

        while test $try -lt 35 ; do

                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;

                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac

                echo -n .
                try=`expr $try + 1`
                sleep 1

        done

}


case "$1" in
        start)
                echo -n "Starting php-fpm "

                $php_fpm_BIN $php_opts

                if [ "$?" != 0 ] ; then
                        echo " failed"
                        exit 1
                fi

                wait_for_pid created $php_fpm_PID

                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;

        stop)
                echo -n "Gracefully shutting down php-fpm "

                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi

                kill -QUIT `cat $php_fpm_PID`

                wait_for_pid removed $php_fpm_PID

                if [ -n "$try" ] ; then
                        echo " failed. Use force-quit"
                        exit 1
                else
                        echo " done"
                fi
        ;;

        force-quit)
                echo -n "Terminating php-fpm "

                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi

                kill -TERM `cat $php_fpm_PID`

                wait_for_pid removed $php_fpm_PID

                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;

        restart)
                $0 stop
                $0 start
        ;;

        reload)

                echo -n "Reload service php-fpm "

                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi

                kill -USR2 `cat $php_fpm_PID`

                echo " done"
        ;;

        *)
                echo "Usage: $0 {start|stop|force-quit|restart|reload}"
                exit 1
        ;;

esac
View Code

给该脚本赋予执行权限:

chmod +x /etc/init.d/php-fpm

注册为服务并随系统启动:

chkconfig php-fpm on

6.以上完成后,我们就可以测试并运行php了(将运行在9000端口)

service php-fpm start

我们可以通过下面的命令查看到php-fpm的进程:

 ps aux|grep php

将打印出进程信息:

到此,php-fpm已经可以提供服务了

posted on 2015-10-29 12:24  重生  阅读(4223)  评论(0编辑  收藏  举报