路漫漫其修远兮
吾将上下而求索

php学习:编译安装php-fpm

先来张图

PHP-Mysql.png

在编译安装前先看下centos6.8系统自带的yum源上面的rpm包装的php-fpm都有哪些内容

[root@localhost ~]#yum info php-fpm
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Available Packages
Name        : php-fpm
Arch        : x86_64
Version     : 5.3.3
Release     : 47.el6
Size        : 1.1 M
Repo        : base
Summary     : PHP FastCGI Process Manager
URL         : http://www.php.net/
License     : PHP
Description : PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI
            : implementation with some additional features useful for sites of
            : any size, especially busier sites.

查看下都有哪些安装文件

[root@localhost ~]#rpm -ql php-common
/etc/php.d
/etc/php.d/curl.ini
/etc/php.d/fileinfo.ini
/etc/php.d/json.ini
/etc/php.d/phar.ini
/etc/php.d/zip.ini
/etc/php.ini
/usr/lib64/php
/usr/lib64/php/modules
/usr/lib64/php/modules/curl.so
/usr/lib64/php/modules/fileinfo.so
/usr/lib64/php/modules/json.so
/usr/lib64/php/modules/phar.so
/usr/lib64/php/modules/zip.so
/usr/lib64/php/pear
/var/lib/php

[root@localhost ~]#rpm -ql php-fpm
/etc/logrotate.d/php-fpm
/etc/php-fpm.conf
/etc/php-fpm.d
/etc/php-fpm.d/www.conf
/etc/rc.d/init.d/php-fpm
/etc/sysconfig/php-fpm
/usr/sbin/php-fpm
/usr/share/doc/php-fpm-5.3.3
/usr/share/doc/php-fpm-5.3.3/LICENSE
/usr/share/doc/php-fpm-5.3.3/php-fpm.conf.default
/usr/share/fpm/status.html
/usr/share/man/man8/php-fpm.8.gz
/var/log/php-fpm
/var/run/php-fpm

日志滚动

[root@localhost ~]#cat /etc/logrotate.d/php-fpm 
/var/log/php-fpm/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
	/bin/kill -SIGUSR1 `cat /var/run/php-fpm/php-fpm.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

service文件

[root@localhost ~]#cat /etc/init.d/php-fpm 
#! /bin/sh
#
# chkconfig: - 84 16
# description:	PHP FastCGI Process Manager
# processname: php-fpm
# config: /etc/php-fpm.conf
# config: /etc/sysconfig/php-fpm
# pidfile: /var/run/php-fpm/php-fpm.pid
#
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: start and stop PHP FPM
# Description: PHP FastCGI Process Manager
### END INIT INFO

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

# Check that networking is up.
. /etc/sysconfig/network

# Additional environment file
if [ -f /etc/sysconfig/php-fpm ]; then
      . /etc/sysconfig/php-fpm
fi

if [ "$NETWORKING" = "no" ]
then
	exit 0
fi

RETVAL=0
prog="php-fpm"
pidfile=${PIDFILE-/var/run/php-fpm/php-fpm.pid}
lockfile=${LOCKFILE-/var/lock/subsys/php-fpm}

start () {
	echo -n $"Starting $prog: "
	dir=$(dirname ${pidfile})
	[ -d $dir ] || mkdir $dir
	daemon --pidfile ${pidfile} /usr/sbin/php-fpm --daemonize
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} php-fpm
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ] ; then
		rm -f ${lockfile} ${pidfile}
	fi
}

restart () {
        stop
        start
}

reload () {
	echo -n $"Reloading $prog: "
	if ! /usr/sbin/php-fpm --test ; then
	        RETVAL=6
	        echo $"not reloading due to configuration syntax error"
	        failure $"not reloading $prog due to configuration syntax error"
	else
		killproc -p ${pidfile} php-fpm -USR2
		RETVAL=$?
	fi
	echo
}


# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status -p ${pidfile} php-fpm
	RETVAL=$?
	;;
  restart)
	restart
	;;
  reload|force-reload)
	reload
	;;
  configtest)
 	/usr/sbin/php-fpm --test
	RETVAL=$?
	;;
  condrestart|try-restart)
	[ -f ${lockfile} ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart|configtest}"
	RETVAL=2
        ;;
esac

exit $RETVAL

里面啥也没有

[root@localhost ~]#cat /etc/sysconfig/php-fpm 
# Additional environment file for php-fpm

通过phpinfo页面查看都编译了哪些选项

'./configure' 
'--build=x86_64-redhat-linux-gnu' 
'--host=x86_64-redhat-linux-gnu' 
'--target=x86_64-redhat-linux-gnu' 
'--program-prefix=' 
'--prefix=/usr' 
'--exec-prefix=/usr' 
'--bindir=/usr/bin' 
'--sbindir=/usr/sbin' 
'--sysconfdir=/etc' 
'--datadir=/usr/share' 
'--includedir=/usr/include' 
'--libdir=/usr/lib64' 
'--libexecdir=/usr/libexec' 
'--localstatedir=/var' 
'--sharedstatedir=/var/lib' 
'--mandir=/usr/share/man' 
'--infodir=/usr/share/info' 
'--cache-file=../config.cache' 
'--with-libdir=lib64' 
'--with-config-file-path=/etc' 
'--with-config-file-scan-dir=/etc/php.d' 
'--disable-debug' 
'--with-pic' 
'--disable-rpath' 
'--without-pear' 
'--with-bz2' 
'--with-exec-dir=/usr/bin' 
'--with-freetype-dir=/usr' 
'--with-png-dir=/usr' 
'--with-xpm-dir=/usr' 
'--enable-gd-native-ttf' 
'--without-gdbm' 
'--with-gettext' 
'--with-gmp' 
'--with-iconv' 
'--with-jpeg-dir=/usr' 
'--with-openssl' 
'--with-pcre-regex=/usr' 
'--with-zlib' 
'--with-layout=GNU' 
'--enable-exif' 
'--enable-ftp' 
'--enable-magic-quotes' 
'--enable-sockets' 
'--enable-sysvsem' 
'--enable-sysvshm' 
'--enable-sysvmsg' 
'--with-kerberos' 
'--enable-ucd-snmp-hack' 
'--enable-shmop' 
'--enable-calendar' 
'--without-sqlite' 
'--with-libxml-dir=/usr' 
'--enable-xml' 
'--with-system-tzdata' 
'--enable-fpm' 
'--without-mysql' 
'--without-gd' 
'--disable-dom' 
'--disable-dba' 
'--without-unixODBC' 
'--disable-pdo' 
'--disable-xmlreader' 
'--disable-xmlwriter' 
'--without-sqlite3' 
'--disable-phar' 
'--disable-fileinfo' 
'--disable-json' 
'--without-pspell' 
'--disable-wddx' 
'--without-curl' 
'--disable-posix' 
'--disable-sysvmsg' 
'--disable-sysvshm' 
'--disable-sysvsem'

php-fpm的主进程为root,日志目录即使是root权限,也可以正常写入

[root@localhost /var/log/php-fpm]#ll -d
drwxrwx--- 2 apache root 4096 May 11  2016 .

====================================================

下面是编译安装php-fpm,之后会将其自动化为脚本文件,

        PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。

1、安装说明,前提要安装好nginx,便于后面的测试

        操作系统:centos6.8

        php-fpm:php-5.6.30  稳定版

        php-fpm安装目录:/usr/local/php,将除了日志文件之外的所有文件都统一放在这个目录中,便于管理

        php-fpm日志目录:/var/log/php

        监听端口:127.0.0.1:9000

2、版本介绍

参考

版本 发布日期 结束支持日期 说明
5.6.0 2014年8月28日 2018年12月31
6.xx 这个大的版本就没有发布 直接跳到7版本 没有
7.0.0 2015年12月3日 2018年12月31日 还在使用

3、准备工作,用到的包,还有运行服务的用户

[root@localhost ~]#yum install -y gcc bison bison-devel zlib-devel mhash-devel openssl openssl-devel \
libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel libxml2 libxml2-devel curl-devel \
libjpeg-devel libpng-devel freetype-devel mcrypt libmcrypt-devel

[root@localhost ~]#useradd -r php -s /sbin/nologin

4、下载源码包

PHP在 5.3.3 之后已经讲php-fpm写入php源码核心了。所以已经不需要另外下载了。php-fpm的下载页面:https://secure.php.net/downloads.php

[root@localhost ~]#wget http://hk1.php.net/get/php-5.6.30.tar.gz/from/this/mirror

[root@localhost ~]#ls	#默认下载的文件名是mirror
 mirror     
[root@localhost ~]#mv mirror php-5.6.30.tar.gz
[root@localhost ~]#tar xf php-5.6.30.tar.gz 
[root@localhost ~]#ls
php-5.6.30 php-5.6.30.tar.gz

5、配置选项

根据帮助文档,只要设置了prefix之后,所有的文件都是以prefix为基础,将文件放在里面的,配置文件里面没有设置log目录的,要手工改了

编译说明文档:http://php.net/manual/zh/configure.about.php

[root@localhost ~]#cd php-5.6.30

[root@localhost ~/php-5.6.30]#./configure --help

[root@localhost ~/php-5.6.30]#./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/conf \
--with-config-file-scan-dir=/usr/local/php/conf/php.d \
--sysconfdir=/usr/local/php/conf \
--datadir=/usr/local/php/data \
--enable-inline-optimization \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--enable-mbstring \
--enable-bcmath \
--enable-soap \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--enable-xml \
--enable-mbregex \
--enable-ftp \
--enable-session \
--enable-gd-native-ttf \
--enable-zip \
--with-fpm-user=php \
--with-fpm-group=php \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--with-libxml-dir \
--with-curl \
--with-zlib \
--with-bz2 \
--with-gd \
--with-readline \
--with-xmlrpc \
--with-jpeg-dir \
--with-freetype-dir

在编译过程中,在configure中设置的参数选项会改变一些配置文件内容:比如用户名,执行文件和配置文件路径等。编译完成后直接将配置文件复制到定义的目录中

6、编译安装,使用4个核心,编译速度快些

[root@localhost ~/php-5.6.30]#make -j 4 && make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM config:        /usr/local/php/conf/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/data/fpm/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/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/php/php/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/conf/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/root/php-5.6.30/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/

#编译完成后既有php执行文件,又有php-fpm执行文件
[root@localhost ~]#cd /usr/local/php/
[root@localhost /usr/local/php]#ls
bin  conf  include  lib  php  sbin  var

7、配置文件修改,将目录添加到系统的PATH环境中

[root@localhost /usr/local/php/conf]#cp /root/php-5.6.30/php.ini-production ./php.ini
[root@localhost /usr/local/php/conf]#cp php-fpm.conf.default php-fpm.conf

[root@localhost /usr/local/php/conf]#vim php-fpm.conf
pid = /usr/local/php/run/php-fpm.pid
error_log = /var/log/php-fpm/php-fpm.log
user = php
group = php

[root@localhost /usr/local/php]#mkdir -pv run/lock    #创建pid文件和锁文件的目录,不用属主为php
[root@localhost /usr/local/php]#ls
bin  conf  data  include  lib  php  run  sbin

[root@localhost /var/log]#mkdir php-fpm    #创建日志目录,不用属主为php

[root@localhost ~]#cat /etc/profile.d/php.sh
PATH=/usr/local/php/bin:/usr/local/php/sbin:$PATH
export PATH
[root@localhost ~]#source /etc/profile.d/php.sh

添加服务文件,这里系统自带的service文件不太好,自己根据rpm包修改

这个不用 # [root@localhost ~/php-5.6.30/sapi/fpm]#cp init.d.php-fpm /etc/init.d/php-fpm
使用下面的这个
[root@localhost ~]#cat /etc/init.d/php-fpm 
#! /bin/sh
#
# chkconfig: - 84 16
# description:	PHP FastCGI Process Manager
# processname: php-fpm
# config: /usr/local/php/conf
# pidfile: /usr/local/php/run/php-fpm.pid
#
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: start and stop PHP FPM
# Description: PHP FastCGI Process Manager
### END INIT INFO

# Standard LSB functions
#. /lib/lsb/init-functions

# Source function library.
. /etc/init.d/functions

# Check that networking is up.
. /etc/sysconfig/network

if [ "$NETWORKING" = "no" ]
then
	exit 0
fi

RETVAL=0
prog="php-fpm"
pidfile=${PIDFILE-/usr/local/php/run/php-fpm.pid}
lockfile=${LOCKFILE-/usr/local/php/run/lock/php-fpm}
fpm_dir="/usr/local/php/sbin"
#以上内容需要修改,下面的不用


start () {
	echo -n $"Starting $prog: "
	dir=$(dirname ${pidfile})
	[ -d $dir ] || mkdir $dir
	daemon --pidfile ${pidfile} ${fpm_dir}/php-fpm --daemonize
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch ${lockfile}
}
stop () {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} php-fpm
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ] ; then
		rm -f ${lockfile} ${pidfile}
	fi
}

restart () {
        stop
        start
}

reload () {
	echo -n $"Reloading $prog: "
	if ! ${fpm_dir}/php-fpm --test ; then
	        RETVAL=6
	        echo $"not reloading due to configuration syntax error"
	        failure $"not reloading $prog due to configuration syntax error"
	else
		killproc -p ${pidfile} php-fpm -USR2
		RETVAL=$?
	fi
	echo
}


# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status -p ${pidfile} php-fpm
	RETVAL=$?
	;;
  restart)
	restart
	;;
  reload|force-reload)
	reload
	;;
  configtest)
 	${fpm_dir}/php-fpm --test
	RETVAL=$?
	;;
  condrestart|try-restart)
	[ -f ${lockfile} ] && restart || :
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart|configtest}"
	RETVAL=2
        ;;
esac

exit $RETVAL

[root@localhost ~]#chmod +x /etc/init.d/php-fpm 
[root@localhost ~]#chkconfig --add php-fpm
[root@localhost ~]#chkconfig --list php-fpm
php-fpm        	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[root@localhost ~]#chkconfig php-fpm on
[root@localhost ~]#chkconfig --list php-fpm
php-fpm        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

添加日志滚动配置,下面的那个发送信号的命令可以发送给其他的pid文件都可以

在bash中 : 冒号,这是一个内建指令:\"什么事都不干\",但返回状态值 0。 

[root@localhost ~]#cat /etc/logrotate.d/php-fpm 
/var/log/php-fpm/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
	/bin/kill -SIGUSR1 `cat /usr/local/php/run/php-fpm.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

然后就可以启动服务了

[root@localhost ~]#service php-fpm start
Starting php-fpm:                                          [  OK  ]

[root@localhost ~]#ss -tnl
State       Recv-Q Send-Q                  Local Address:Port                    Peer Address:Port 
LISTEN      0      128                         127.0.0.1:9000                               *:*     
LISTEN      0      128                                :::22                                :::*     
LISTEN      0      128                                 *:22                                 *:*     
LISTEN      0      100                               ::1:25                                :::*     
LISTEN      0      100                         127.0.0.1:25                                 *:*    
 
[root@localhost ~]#ps aux | grep php-fpm
root       3825  0.0  0.2 212332  5176 ?        Ss   13:29   0:00 php-fpm: master process (/usr/local/php/conf/php-fpm.conf)
php        3826  0.0  0.2 212332  4564 ?        S    13:29   0:00 php-fpm: pool www                      
php        3827  0.0  0.2 212332  4564 ?        S    13:29   0:00 php-fpm: pool www                      
root       3831  0.0  0.0 103312   880 pts/0    S+   13:29   0:00 grep php-fpm

[root@localhost ~]#cd /usr/local/php/run/
[root@localhost /usr/local/php/run]#ll
total 8
drwxr-xr-x. 2 root root 4096 Jun 11 13:29 lock
-rw-r--r--. 1 root root    4 Jun 11 13:29 php-fpm.pid

8、设置测试页面,这里有两个测试页面,一个是phpinfo测试,一个是检测系统运行的文件,

[root@localhost ~]#wget https://andblog.cn/files/20170416-1720675724.zip
[root@localhost ~]#unzip ,,,
[root@localhost ~]#mv tz.php /web

[root@localhost /web]#vim phpinfo.php 
<?php
	phpinfo();

[root@localhost /web]#ls	#另外的是系统运行监测的文件
index.html  phpinfo.php  tz.php

探针站点:http://www.yahei.net/

php文件:

tz.zip

9、设置nginx配置文件,设置root目录为上面的目录,设置fastcgi代理,代理到:127.0.0.1:9000,重读nginx配置文件,

location ~ \.php$ {
    root           /web;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

10、启动服务,检测是否能查到php用户对应的监听端口,是否能请求到资源,

[root@localhost ~]#ss -tnlp | grep php
LISTEN     0      128               127.0.0.1:9000    *:*      users:(("php-fpm",66804,7),("php-fpm",66805,0),("php-fpm",66806,0))

[root@localhost ~]#ps aux | grep php    #进程中也可以查到php用户对应的进程

[root@localhost ~]#curl localhost/phpinfo.php | grep -c "PHP Version 5.6.30" 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 83978    0 83978    0     0  16.1M      0 --:--:-- --:--:-- --:--:-- 64.6M
1
[root@localhost ~]#echo $?
0

11、隐藏浏览器访问服务器,http头部里面有:X-Powered-By: PHP/5.6.30

在php.ini中找到:
expose_php = on
修改为:
expose_php = Off

12、在浏览器可以看到这两个php页面文件

http://192.168.175.16/phpinfo.php
http://192.168.175.16/tz.php

未经允许不得转载:江哥架构师笔记 » php学习:编译安装php-fpm

分享到:更多 ()

评论 抢沙发

评论前必须登录!