php-fpm 的安装与LNMP测试

1. 下载并安装PHP

1.1 编译安装

1.1.1 安装前的准备

 下载并安装libmcrypt 和 libmcrypt-devel
[root@test /opt/soft]# wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home:/csbuild:/centosextra/CentOS_CentOS-6/x86_64/libmcrypt-devel-2.5.8-4.3.x86_64.rpm [root@test /opt/soft]# wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home:/csbuild:/centosextra/CentOS_CentOS-6/x86_64/libmcrypt-2.5.8-4.3.x86_64.rpm [root@test /opt/soft]# rpm -ivh libmcrypt-* warning: libmcrypt-2.5.8-4.3.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 04b8b50a: NOKEY Preparing... ########################################### [100%] 1:libmcrypt ########################################### [ 50%] 2:libmcrypt-devel ########################################### [100%]
建议:使用yum源epel来安装 libmcypt 和 libmcypt-devel,推荐使用aliyun的yum源
  [epel]
  name=epel
  mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-$releasever&arch=$basearch
  enabled=1
  gpgcheck=0

安装开发工具组和必要的编译软件包
[root@test /opt/soft]# yum groupinstall -y "Development Tools"
[root@test /opt/soft]# yum -y install autoconf automake bzip2 bzip2-devel curl curl curl-devel e2fsprogs e2fsprogs-devel freetype freetype-devel gcc gcc-c++ gd gettext glib2 glib2-devel glibc glibc-devel krb5-devel krb5-libs krb5-workstation libiconv libiconv-devel libidn libidn-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel libxslt libxslt-devel libxml2 libxml2-devel libXpm libXpm-devel mhash mhash-devel ncurses ncurses-devel nss-pam-ldapd openldap openldap-clients openldap-devel openldap-servers openssl openssl-devel t1lib t1lib-devel zlib zlib-devel

1.1.2 下载PHP

[root@test /opt]# wget http://cn2.php.net/distributions/php-5.3.29.tar.gz
[root@test /opt]# tar xf php-5.3.29.tar.gz

1.1.3 编译安装

[root@test /opt/php-5.3.29]# ./configure --prefix=/usr/local/php --disable-rpath --enable-bcmath --enable-calendar --enable-dba --enable-debug --enable-embed=fpm --enable-exif --enable-fpm --enable-ftp --enable-gd-jis-conv --enable-gd-native-ttf --enable-inline-optimization --enable-intl=shared --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-pcntl --enable-pdo --enable-shmop --enable-short-tags --enable-soap --enable-sockets --enable-static --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-xml --enable-zend-multibyte --enable-zip --infodir=/usr/local/share/info --mandir=/usr/local/share/man --with-bz2 --with-config-file-path=/usr/local/php/etc --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-imap --with-imap-ssl --with-jpeg-dir --with-kerberos --with-libxml-dir --with-mcrypt --with-mhash --with-mysql=/usr/local/mysql --with-openssl --with-pcre-regex --with-pear --with-png-dir --with-t1lib --with-xmlrpc --with-xpm-dir --with-xsl --with-zlib --with-fpm-user=nginx --with-fpm-group=nginx
这里需要注意的是--with-mysql这个参数:
  如果不指定,php默认使用php自带mysql库文件即--with-mysql=mysqldn,
  如果指定,就需要赋予mysql的base目录路径。(--with-mysqli 和 --with-pdo-mysql 后面以扩展的方式安装)

 可能遇到的一些错误:

1. configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing
这个是由于编译加入了imap支持需要使用c-client库

解决方法:
[root@test /opt/php-5.3.29]# yum install -y libc-client libc-client-devel
2. configure: error: libXpm.(a|so) not found.
解决方法:
[root@test /opt/php-5.3.29]# ln -s /usr/lib64/libXpm.so* /usr/lib/

Cannot find imap library (libc-client.a).
解决方法:
[root@test /opt/php-5.3.29]# ln -s /usr/lib64/libc-client.so* /usr/lib/
3. configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.
解决方法:
[root@test /opt/php-5.3.29]# yum install libicu-devel libicu
4. [...]/sapi/cli/php:error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
解决方法:
[root@test /opt/php-5.3.29]# ln -s 【mysql的base目录】/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

[root@test /opt/php-5.3.29]# echo "【mysql的base目录】/lib/libmysqlclient.so.18" > /etc/ld.so.conf.d/mysql-x86_64.conf
[root@test /opt/php-5.3.29]# ldconfig

安装完成:

...
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
编译完成提示信息
[root@test /opt/php-5.3.29]# make install

编译时可能出现的错误:--(这种情况出现在使用mysql的二进制包,没有找到库文件libmysqlclient.so.18)
/opt/php-5.3.29/sapi/cli/php: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or director
make: *** [ext/phar/phar.phar] Error 127
[root@test /opt/php-5.3.29]#
[root@test /usr/local/mysql/lib]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18.1.0 /usr/lib/libmysqlclient.so
[root@test /usr/local/mysql/lib]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18.1.0 /usr/lib/libmysqlclient.so.18
[root@test /usr/local/mysql/lib]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18.1.0 /usr/lib64/libmysqlclient.so
[root@test /usr/local/mysql/lib]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18.1.0 /usr/lib64/libmysqlclient.so.18
[root@test /usr/lib]# ldconfig
清理已经编译过文件,重新编译
[root@test /opt/php-5.3.29]# make clean;make

PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.

...
Installing shared extensions:     /usr/local/php/lib/php/extensions/debug-zts-20131226/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/share/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM config:        /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/share/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/share/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:           /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/share/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/opt/php-5.6.21/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo
安装完成的提示信息
编译安装memcache扩展,此步骤非必须wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar xf memcache-2.2.7.tar.gz
cd memcache-2.2.7
/usr/local/php/bin/phpize 
./configure --with-php-config=/usr/local/php/bin/php-config 
make
make install

编译安装eaccelerator扩展
wget http://ufpr.dl.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.zip
tar xf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1
/usr/local/php/bin/phpiz
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

编译安装mysqli
cd php-5.3.29/ext/mysqli
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

编译安装pdo_mysql
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
/usr/local/php/bin/phpiz
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr
make
make install

1.2 相关设置

设置php.ini文件,这里需要注意的是,因为在编译时,我修改了php配置文件目录路径,所以存放时要放在编译时制定的目录--with-config-file-path=/usr/local/php/etc,而不是默认目录:

查看默认目录:
[root@test /opt/php-5.3.29]# ./configure --help | grep -B 1 php.ini --with-config-file-path=PATH Set the path in which to look for php.ini [PREFIX/lib]
拷贝到制定的配置文件目录中:
[root@test /opt/php-5.3.29]# cp php.ini-production /usr/local/php/etc/php.ini

 编辑Nginx配置文件,添加以下配置(蓝色部分内容):

[root@test /usr/local/nginx]# egrep -v "^$|#"  conf/nginx.conf
user   nginx;
worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    
        location ~* .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO    $fastcgi_script_name;
        include fastcgi_params;
        }
error_page
500 502 503 504 /50x.html; location = /50x.html { root html; } } }

PS:  nginx的SPEC打包安装:http://www.cnblogs.com/jonsea/p/5481580.html

 mysql 5.7.12安装:http://www.cnblogs.com/jonsea/p/5503267.html

2. 启动php-fm

制作服务脚本并启动php-fpm:

方法1----拷贝源文件中的制作好的脚本

[root@test /opt/php-5.3.29]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@test /opt/php-5.3.29]# chmod 755 /etc/rc.d/init.d/php-fpm

[root@test /opt/php-5.3.29]# /etc/init.d/php-fpm start
Starting php-fpm  done

[root@test /opt/php-5.3.29]# ps -ef | grep php-fpm root 9410 1 0 17:53 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nobody 9411 9410 0 17:53 ? 00:00:00 php-fpm: pool www nobody 9412 9410 0 17:53 ? 00:00:00 php-fpm: pool www

[root@test ~]# netstat -anlp | grep php-fpm
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      9533/php-fpm


[root@test /opt/php-5.3.29]# /etc/init.d/php-fpm stop
Gracefully shutting down php-fpm . done
[root@test /opt/php-5.3.29]# chkconfig --add php-fpm --level 35

方法2----制作启动脚本(推荐)

#!/bin/bash
# php-fpm     startup script for php-fpm
#
# chconfig: - 82 14
# processname: php-fpm
#
### BEGIN INIT INFO
# Default-Start : 3 5
# Default-Sop: 0 1 2 4 6
# Short-Description: start and stop php-fpm
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.  
. /etc/sysconfig/network

# Check that networking is up.  
[ "$NETWORKING" = "no" ] && exit 0

phpfpm="/usr/local/php/sbin/php-fpm"
prog=$(basename ${phpfpm})

lockfile=/var/lock/subsys/phpfpm

start() {
    [ -x ${phpfpm} ] || exit 5
    echo -n $"Starting $prog: "  
    daemon ${phpfpm}
    retval=$?
    echo  
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "  
    killproc $prog -QUIT
    retval=$?
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "  
    killproc ${phpfpm} -HUP
    RETVAL=$?
    echo  
}

force_reload() {
    restart
}

configtest() {
  ${phpfpm} -t
}

rh_status() {
    status $prog
}

rh_status_q() {  
    rh_status >/dev/null 2>&1  
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status)
        rh_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"  
        exit 2
esac
php-fpm脚本内容
[root@test /opt/php-5.3.29]# /etc/init.d/fpm start
Starting php-fpm:                                          [  OK  ]

[root@test /opt/php-5.3.29]# ps -ef | grep php-fpm
root      9473     1  0 18:04 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nobody    9474  9473  0 18:04 ?        00:00:00 php-fpm: pool www          
nobody    9475  9473  0 18:04 ?        00:00:00 php-fpm: pool www          

[root@test /opt/php-5.3.29]# /etc/init.d/fpm stop
Stopping php-fpm:                                          [  OK  ]

3. 测试web和数据库链接

web测试代码和mysql测试代码

[root@test /usr/local/nginx/html]# cat t.php 
<?php
    phpinfo();
?>

[root@test /usr/local/nginx/html]# cat m.php 
<?php
$link=mysql_connect("localhost","root","【你的mysql账户密码】");
if(!$link) echo "FAILD!连接错误,用户名密码不对";
else echo "OK!可以连接";
?> 

 

测试通过!!!

 

 

 

原文地址:https://www.cnblogs.com/jonsea/p/5499866.html