CentOS 编译安装PHP5.6,7以上也通用

由于公司有新服务器需要构建一套LNMP平台,且需要编译安装各个部件,所以记录下此文章。

这是安装PHP涉及到的软件包(可以自行决定使用哪个版本):

├── libiconv-1.15.tar.gz
├── libmcrypt-2.5.8.tar.gz
├── openssl-1.0.2o.tar.gz
├── php-5.6.38.tar.gz
└── zend-loader-php5.6-linux-x86_64.tar.gz 

涉及到的开机启动脚本:

1. init.d.php-fpm

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for php-fpm on Debian. Place in /etc/init.d and
# run 'update-rc.d -f php-fpm defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add php-fpm'

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts php-fpm
### END INIT INFO

# Author:   licess
# website:  https://lnmp.org

php_fpm_BIN=/usr/local/php/bin/php-cgi
php_fpm_CONF=/usr/local/php/etc/php-fpm.conf
php_fpm_PID=/usr/local/php/logs/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 --fpm $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 "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 -TERM `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

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

    quit)
        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"
            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"
    ;;
        
        status)

                if [ -f "$php_fpm_PID" ]; then  
            echo "PHP-FPM is running."
        else
            echo "PHP-FPM is stopped."
        fi 
        ;;

    logrotate)

        echo -n "Re-opening php-fpm log file "

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

        kill -USR1 `cat $php_fpm_PID`

        echo " done"
    ;;

    *)
        echo "Usage: /etc/init.d/php-fpm {start|stop|quit|restart|reload|status|logrotate}"
        exit 1
    ;;

esac

1)安装依赖包

yum install make cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel patch wget crontabs libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel unzip tar bzip2 bzip2-devel libzip-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap diffutils ca-certificates net-tools libc-client-devel psmisc libXpm-devel git-core c-ares-devel libicu-devel libxslt libxslt-devel xz expat-devel libaio-devel rpcgen libtirpc-devel perl

 

2)安装openssl

tar vxzf openssl-1.0.2o.tar.gz
cd openssl-1.0.2o
./config -fPIC --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
make depend
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install

3)安装libiconv

cd ../
tar vxzf libiconv-1.15.tar.gz
cd libiconv-1.15
./configure --enable-static
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install
echo '/usr/local/lib' >> /etc/ld.so.conf && /sbin/ldconfig

4)安装libmcrypt

cd ../
tar vxzf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install
cd libltdl/
./configure --enable-ltdl-install
make -j `grep 'processor' /proc/cpuinfo | wc -l`
make install
ln -sf /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -sf /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ldconfig

5)安装php

5.6版本

./configure  --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl=/usr/local/openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --enable-intl --with-xsl

7以上版本

./configure  --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl=/usr/local/openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --enable-intl --with-xsl
make ZEND_EXTRA_LIBS='-liconv' -j `grep 'processor' /proc/cpuinfo | wc -l`
make install

  

6)添加软链接

ln -sf /usr/local/php/bin/php /usr/bin/php
ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
ln -sf /usr/local/php/bin/pear /usr/bin/pear
ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm

7)创建配置文件目录

mkdir -p /usr/local/php/{etc,conf.d}
cp php.ini-production /usr/local/php/etc/php.ini

9)修改php.ini配置文件

sed -i 's/post_max_size =.*/post_max_size = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /usr/local/php/etc/php.ini
sed -i 's/short_open_tag =.*/short_open_tag = On/g' /usr/local/php/etc/php.ini
sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
sed -i 's/max_execution_time =.*/max_execution_time = 300/g' /usr/local/php/etc/php.ini
sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini

10)安装扩展工具

pear config-set php_ini /usr/local/php/etc/php.ini
pecl config-set php_ini /usr/local/php/etc/php.ini

  

11)安装ZendGuardLoader(php7以上不用安装)

cd ../
tar vzxf zend-loader-php5.6-linux-x86_64.tar.gz
mkdir -p /usr/local/zend/
cp zend-loader-php5.6-linux-x86_64/ZendGuardLoader.so /usr/local/zend/
cat >/usr/local/php/conf.d/002-zendguardloader.ini<<EOF
[Zend ZendGuard Loader]
zend_extension=/usr/local/zend/ZendGuardLoader.so
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path=
EOF

12)添加用户组

groupadd www
useradd -s /sbin/nologin -g www www

13)修改php-fpm配置文件

cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice

[www]
listen = /tmp/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF

14)开机启动

cp php-5.6.38/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # init.d.php-fpm 见文章头
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

15)启动测试

service php-fpm start