Building PHP from Sources

安装环境依赖包

$ yum -y install gcc gcc+ gcc-c++ gcc-g77 flex bison autoconf snmp automake\
  bzip2-devel zlib-devel readline-devel ncurses-devel libjpeg-devel \
  libpng-devel libtiff-devel freetype-devel pam-devel openssl-devel \
  mod_ssl libxml2-devel gettext-devel pcre-devel curl-devel mysql-devel \
  libevent-devel libtool-ltdl gmp-devel openldap openldap-devel \
  ImageMagick-devel libtool net-snmp-devel libwebp libwebp-devel

下载编译包

  • libiconv

      $ cd /usr/local/src
      $ wget https://ftp.gnu.org/gnu/libiconv/libiconv-1.15.tar.gz
    
  • libmcrypt

      $ wget https://ncu.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
    
  • mcrypt

      $ wget https://ncu.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
    
  • mhash

      $ wget https://ncu.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
    
  • php

      $ wget http://cn2.php.net/distributions/php-7.1.12.tar.gz
    
  • 解压

      $ tar zxf libiconv-1.15.tar.gz
      $ tar zxf libmcrypt-2.5.8.tar.gz
      $ tar zxf mcrypt-2.6.8.tar.gz
      $ tar zxf mhash-0.9.9.9.tar.gz
      $ tar zxf php-7.1.12.tar.gz
    

安装编译包

  • 加载lib

      $ echo "/usr/lib" >> /etc/ld.so.conf
      $ echo "/usr/lib64" >> /etc/ld.so.conf
      $ echo "/usr/local/lib" >> /etc/ld.so.conf
      $ echo "/usr/local/lib64" >> /etc/ld.so.conf
      $ /sbin/ldconfig
    
  • 安装libiconv

    ## libiconv库为需要做转换的应用程序提供了一个iconv命令,以实现一个字符编码到另一个字符编码的转换,比如它可以将UTF8编码转换成GB18030编码,反过来也行
    
      $ cd /usr/local/src/libiconv-1.15
      $ ./configure --prefix=/usr/local
      $ make && make install
      $ /sbin/ldconfig
    
  • 安装libmcrypt

      $ cd /usr/local/src/libmcrypt-2.5.8
      $ ./configure && make && make install
      $ /sbin/ldconfig
      $ cd libltdl/
      $ ./configure --prefix=/usr/local --enable-ltdl-install
      $ make && make install
      $ /sbin/ldconfig
    
  • 安装mhash

      $ cd /usr/local/src/libmcrypt-2.5.8
      $ ./configure
      $ make && make install
    
  • 安装mcrypt

      $ cd /usr/local/src/mhash-0.9.9.9
      $ ./configure
      $ make && make install
      $ /sbin/ldconfig
    
  • 安装php

    • 编译php

          $ cd /usr/local/src/php-7.1.12
          $ ./configure --prefix=/usr/local/php-7.1.12 \
                --with-config-file-path=/usr/local/php-7.1.12/etc \
                --enable-fpm --enable-pcntl \
                --with-pear=/usr/share/php \
                --with-mysql --with-mysqli \
                --with-pdo_mysql --with-iconv-dir=/usr/local \
                --with-zlib --with-bz2 \
                --with-curl --with-libxml-dir \
                --with-gd --with-jpeg-dir \
                --with-png-dir --with-zlib-dir \
                --with-freetype-dir --with-openssl \
                --with-gettext --with-snmp \
                --with-mhash --with-mcrypt \
                --enable-gd-native-ttf --enable-gd-jis-conv \
                --enable-shmop --enable-sockets \
                --enable-zip --enable-ftp \
                --enable-bcmath --enable-soap \
                --enable-calendar --enable-dba \
                --disable-ipv6 --enable-opcache
          
          $ make && make install php.
          
          $ /usr/bin/make ZEND_EXTRA_LIBS='-liconv' && /usr/bin/make install
          $ ln -s /usr/local/php-7.1.12 /usr/local/php
      

安装php module依赖包

  • ImageMagick

        ## 下载ImageMagick
        $ wget http://transloadit.imagemagick.org/download/ImageMagick-7.0.7-11.tar.gz
          or
        $ wget http://transloadit.imagemagick.org/download/ImageMagick-6.9.9-23.tar.gz
    
        $ ## 编译依赖库ImageMagick,centos自带的ImageMagick版本较低
        $ cd /usr/local/src
        $ tar -zxf ImageMagick-7.0.7-11.tar.gz
        $ cd ImageMagick-7.0.7-11
        $ ./configure
        $ make && make install
        $ /sbin/ldconfig
    
  • libevent

        ## 下载libevent
        $ wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
    
        ## 编译拓展libevent
        $ cd /usr/local/src
        $ tar xzf libevent-${libevent_ver}-stable.tar.gz
        $ cd libevent-${libevent_ver}-stable
        $ ./configure --prefix=/usr/local
        $ make && make install
        $ /sbin/ldconfig
    

安装php module

安装php module有两种手动方式,追求最新的功能可以手动编译安装,或者使用pecl(类似于pip) 安装

  • 手动安装

    • imagick

          ## 下载imagick
          $ cd /usr/local/src
          $ wget http://pecl.php.net/get/imagick-3.4.3.tgz
      
          ## 编译拓展imagick
          $ tar zxf imagick-3.4.3.tgz
          $ cd imagick-3.4.3
          $ /usr/local/php/bin/phpize
          $ ./configure --with-php-config=/usr/local/php/bin/php-config
          $ make && make install
      
    • memcache

          ## 下载memcache
          $ wget http://pecl.php.net/get/memcache-3.0.8.tgz
      
          ## 编译拓展memcache
          $ cd /usr/local/src
          $ tar zxf memcache-3.0.8.tgz
          $ cd memcache-3.0.8
          $ /usr/local/php/bin/phpize
          $ ./configure --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir --enable-memcache
          $ make && make install
      
    • phpredis

          ## 下载phpredis
          $ wget https://pecl.php.net/get/redis-3.1.4.tgz
      
          ## 编译拓展redis
          $ cd /usr/local/src
          $ tar zxf redis-3.1.4.tgz
          $ cd redis-3.1.4
          $ /usr/local/php/bin/phpize
          $ ./configure --with-php-config=/usr/local/php/bin/php-config --enable-redis
          $ make && make install
      
    • mongo

          ## 下载mongo
          $ wget https://pecl.php.net/get/mongo-1.6.16.tgz
      
          ## 编译拓展mongo
          $ cd /usr/local/src
          $ tar xzf mongo-1.6.16.tgz
          $ cd mongo-1.6.16
          $ /usr/local/php/bin/phpize
          $ ./configure --with-php-config=/usr/local/php/bin/php-config
          $ make && make install
      
    • xdebug

          ## 下载xdebug
          $ wget https://xdebug.org/files/xdebug-2.5.5.tgz
      
          ## 编译拓展xdebug
          ### 如果需要将 Xdebug 扩展和 OPcache 一起使用,必须在 Xdebug 扩展之前加载 OPcache 扩展。
          $ cd /usr/local/src
          $ tar xzf xdebug-2.5.5.tgz
          $ cd xdebug-2.5.5
          $ /usr/local/php/bin/phpize
          $ ./configure --with-php-config=/usr/local/php/bin/php-config
          # make && make install
          $ make -j `grep processor /proc/cpuinfo | wc -l` && make install
      
  • pecl安装

        $ /usr/local/php/bin/pecl install imagick
        $ /usr/local/php/bin/pecl install memcache
        $ /usr/local/php/bin/pecl install redis
        $ /usr/local/php/bin/pecl install mongo
        ### 如果需要将 Xdebug 扩展和 OPcache 一起使用,必须在 Xdebug 扩展之前加载 OPcache 扩展。
        $ /usr/local/php/bin/pecl install xdebug
    

配置php

  • 配置php

        # set php-fpm config file.
    
        $ cp /usr/local/php/etc/php-fpm.conf{.,default}
        $ cat > /usr/local/php/etc/php-fpm.conf <<EOF
        [global]
        pid = /usr/local/php-7.1.12/var/run/php-fpm.pid
        error_log = /usr/local/php-7.1.12/var/log/php-fpm.log
        log_level = error
        [www]
        listen = /usr/local/php/etc/php-cgi.socket
        user = nginx
        group = nginx
        listen.owner = nginx
        listen.group = nginx
        listen.mode = 0660
        pm = static
        pm.max_children = 200
        pm.start_servers = 20
        pm.min_spare_servers = 5
        pm.max_spare_servers = 30
        pm.max_requests = 5000
        request_slowlog_timeout = 5s
        slowlog = /usr/local/php-7.1.12/var/log/php-fpm.log.slow
        rlimit_files = 51200
        EOF
    
        # set php.ini config file.
    
        $ cp /usr/local/src/php-7.1.12/php.ini-production /usr/local/php-7.1.12/etc/php.ini
        $ cp /usr/local/php-7.1.12/etc/php.ini{.,default}
        $ ext_path=`ls /usr/local/php-7.1.12/lib/php/extensions/`
        $ cat >> /usr/local/php-7.1.12/etc/php.ini <<EOF
        extension_dir = /usr/local/php/lib/php/extensions/${ext_path}
        extension = "memcache.so"
        extension = "imagick.so"
        extension = "redis.so"
        zend_extension = "opcache.so"
        output_buffering = On
        cgi.fix_pathinfo=0
        EOF
    
        # 修改文件上传大小
        $ sed -i "s#^upload_max_filesize = 2M#upload_max_filesize = 5M#" /usr/local/php-7.1.12/etc/php.ini
    
        # 限制php运行目录
        $ sed -i "s#^;open_basedir =#open_basedir = /tmp:/data/wwwroot/:/data/ftp/:/www/web#" /usr/local/php-7.1.12/etc/php.ini
    
        # 关闭php版本信息
        $ sed -i "s#^expose_php = On#expose_php = Off#" /usr/local/php-7.1.12/etc/php.ini
        $ sed -i "s#^allow_url_fopen = On#allow_url_fopen = Off#" /usr/local/php-7.1.12/etc/php.ini
    
        # opcache
        $ sed -i "s#^;opcache.enable=0#opcache.enable=1#" /usr/local/php-7.1.12/etc/php.ini
        $ sed -i "s#^;opcache.enable_cli=0#opcache.enable_cli=1#" /usr/local/php-7.1.12/etc/php.ini
        $ sed -i "s#^;opcache.memory_consumption=64#opcache.memory_consumption=128#" /usr/local/php-7.1.12/etc/php.ini
        $ sed -i "s#^;opcache.interned_strings_buffer=4#opcache.memory_consumption=8#" /usr/local/php-7.1.12/etc/php.ini
        $ sed -i "s#^;opcache.max_accelerated_files=2000#opcache.max_accelerated_files=4000#" /usr/local/php-7.1.12/etc/php.ini
        $ sed -i "s#^;opcache.revalidate_freq=2#opcache.revalidate_freq=60#" /usr/local/php-7.1.12/etc/php.ini
        $ sed -i "s#^;opcache.fast_shutdown=0#opcache.fast_shutdown=1#" /usr/local/php-7.1.12/etc/php.ini
    
        # disable_functions 本指令允许你基于安全原因禁止某些函数。接受逗号分隔的函数名列表作为参数。
        # disable_functions 不受安全模式的影响。 本指令只能设置在 php.ini 中
        $ sed -i "s#^disable_functions =#disable_functions = passthru,ini_restore,eval#" /usr/local/php-7.1.12/etc/php.ini
    
        # 修改时区,具体时区参考:http://php.net/manual/zh/timezones.php
        $ sed -i "s#^;date.timezone =#date.timezone = Asia/Shanghai#" /usr/local/php-7.1.12/etc/php.ini
    
  • 配置php-fpm启动脚本

        $ cat > /etc/init.d/php-fpm <<EOF
        #! /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/local/php
        exec_prefix=${prefix}
    
        php_fpm_BIN=${exec_prefix}/sbin/php-fpm
        php_fpm_CONF=${prefix}/etc/php-fpm.conf
        php_fpm_PID=${prefix}/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 --daemonize $php_opts && chown -R nginx.nginx ${prefix}/etc/php-cgi.socket
    
                    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
            ;;
    
            status)
                    if [ ! -r $php_fpm_PID ] ; then
                            echo "php-fpm is stopped"
                            exit 0
                    fi
    
                    PID=`cat $php_fpm_PID`
                    if ps -p $PID | grep -q $PID; then
                            echo "php-fpm (pid $PID) is running..."
                    else
                            echo "php-fpm dead but pid file exists"
                    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|status}"
                    exit 1
            ;;
    
        esac
        EOF
    
        $ chmod 755 /etc/init.d/php-fpm
        $ chkconfig --level 35 php-fpm on