lnmp编译安装-(四)-编译php源码

编译安装php,从php 5.3.3开始就已经集成了php-fpm,所以这里就不用再打php-fpm的补丁了
更多php-fpm相关资料可参考:什么是cgi、fastcgi、php-cgi、php-fpm、spawn-fcgi?

  1. Php5.3.3 以前的php配置版本

            ./configure --prefix=/usr/local/php \
            --with-config-file-path=/usr/local/php/etc \
            --with-mysql=/usr/local/mysql \
            --with-mysqli=/usr/local/mysql/bin/mysql_config \
            --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath  \
            --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl \
            --with-curlwrappers --enable-mbregex  --enable-fastcgi --enable-fpm  --enable-force-cgi-redirect --enable-mbstring --with-mcrypt \
            --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets \
            --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap
            make ZEND_EXTRA_LIBS='-liconv'
            make install
            

    Php5.3.8版本

            ./configure php的时候出现下面错误而退出
            configure: error: Cannot find ldap libraries in /usr/lib
            解决办法:
            cp -frp /usr/lib64/libldap* /usr/lib/
            然后再./configure ...即可

    编译php

            tar zxvf php-5.3.8.tar.gz
            cd php-5.3.8/
            ./configure --prefix=/usr/local/php \
            --with-config-file-path=/usr/local/php/etc \
            --with-mysql=mysqlnd  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd\
            --with-iconv-dir=/usr/local \
            --enable-fpm \
            --with-libxml-dir=/usr/local/libxml2 --enable-xml --with-xmlrpc \
            --with-gd --with-freetype-dir --enable-gd-native-ttf --with-jpeg-dir --with-png-dir \
            --with-zlib --enable-zip \
            --with-curl --with-curlwrappers \
            --enable-mbstring \
            --with-mcrypt  --with-mhash \
            --enable-sockets \
            --enable-soap \
            --with-openssl \
            --disable-rpath \
            make ZEND_EXTRA_LIBS='-liconv'
            make install
            命令汇总:
            tar zxvf php-5.3.8.tar.gz;cd php-5.3.8/;./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=mysqlnd  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-iconv-dir=/usr/local --enable-fpm --with-libxml-dir=/usr/local/libxml2 --enable-xml --with-xmlrpc --with-gd --with-freetype-dir --enable-gd-native-ttf --with-jpeg-dir --with-png-dir --with-zlib --enable-zip --with-curl --with-curlwrappers --enable-mbstring --with-mcrypt  --with-mhash --enable-sockets --enable-soap --with-openssl --disable-rpath ;make ZEND_EXTRA_LIBS='-liconv';make install;
            

    php 5.4.0以后的版本和5.3.8版本编译方式一样
    Mysql Native驱动(Mysql Native Driver 简称:mysqlnd )在PHP5.3.0版本中被引入。PHP5.4之后的版本mysqlnd被作为默认配置选项mysqlnd详解


    在PHP5.5开始,PHP 优化加速组件,第一个也是最快的 opcode 缓存工具ZendOpcache已经随着PHP的源代码一起发布了,但是必须使用 –enable-opcache 选项来开启OPcache


    编译参数详解

            #mysqlnd同时支持mysql mysqli pdo_mysql扩展接口
            --with-mysql=mysqlnd  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd
            #开启PHP-FPM(与nginx配套),使用PHP-FPM来控制PHP-CGI的FastCGI进程(apache时请使用--with-apxs2这个)
            --enable-fpm
            #php支持xml的相关扩展,提供库的位置
            --with-libxml-dir=/usr/local/libxml2  --enable-xml
            #支持xmlrpc扩展
            --with-xmlrpc
            #支持GD扩展,GD支持FreeType2字体库和TrueType的ttf字体,支持jpeg,png图像格式
            --with-gd --with-freetype-dir --enable-gd-native-ttf --with-jpeg-dir --with-png-dir
            #支持zlib和zip压缩扩展,libpng也需要zlib库
            --with-zlib --enable-zip
            #支持curl扩展,通过curl打开url流
            --with-curl --with-curlwrappers
            #支持mbstring扩展,多字节处理
            --enable-mbstring
            #支持mhash和mcrypt加密算法的扩展
            --with-mcrypt  --with-mhash
            #支持socket扩展,让php支持基于套接字的通信
            --enable-sockets
            #支持soap扩展
            --enable-soap
            #支持openssl扩展,加密传输时用到的
            --with-openssl
            #关闭额外的运行库文件
            --disable-rpath
            
  2. 添加全局环境变量,将php加入linux系统命令

            #vi /etc/profile
            在文件最后加入:
            PATH=$PATH:/usr/local/php/bin
            export PATH
            #source /etc/profile
            
  3. 创建php的配置文件

            cp -f /usr/local/src/php-5.3.8/php.ini-production /usr/local/php/etc/php.ini
            
  4. 建立存放日志的目录

            mkdir -p /usr/local/php/logs
            
  5. 创建php-fpm配置文件

            #php-fpm配置文件
            cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
            # vim /usr/local/php/etc/php-fpm.conf #比较重要的配置,剩下的参数可以查百度
            [global]
            ; Pid file
            ; Note: the default prefix is /usr/local/php/var
            ; Default Value: none
            pid = run/php-fpm.pid
            ; Error log file
            ; If it's set to "syslog", log is sent to syslogd instead of being written
            ; in a local file.
            ; Note: the default prefix is /usr/local/php/var
            ; Default Value: log/php-fpm.log
            error_log = log/php-fpm.log
            ; Log level
            ; Possible Values: alert, error, warning, notice, debug
            ; Default Value: notice
            log_level = notice
            [www]
            ; Unix user/group of processes
            ; Note: The user is mandatory. If the group is not set, the default user's group
            ;       will be used.
            user = www
            group = www
            

    除了基本设置外,最好对php-fpm的进程管理做一下优化,详细查看Nginx使用的php-fpm的两种进程管理方式及优化

            #注:运行php-fpm前,记得先建立www用户,不然运行后会报错。因为php-fpm配置文件中是用www来运行的
            /usr/sbin/groupadd www;
            /usr/sbin/useradd -g www www;
            
    • 启动php-cgi进程 启动前可先测试下php-fpm.conf的语法是否正确。如出现下面的提示,表示没有问题

                      # /usr/local/php/sbin/php-fpm -t
                      [18-apr-2011 19:53:51] notice: configuration file /usr/local/php/etc/php-fpm.conf test is successful
                      启动php-cgi进程后,监听的是127.0.0.1的9000端口,进程数为64(如果服务器内存小于3gb,可以只开启64个进程),用户为www
                      # /usr/local/php/sbin/php-fpm
                      注:运行php-fpm前,记得先建立www用户,不然运行后会报错。因为php-fpm配置文件中是用www来运行的。
                      
                      启动错误解决:
                      ALERT: [pool www] pm.min_spare_servers(0) must be a positive value
                      编辑php-fpm.conf找到pm.min_spare_servers 去除;号,注意非注释内容pm.min_spare_servers
                      ALERT: [pool www] pm.max_spare_servers(0) must be a positive value
                      编辑php-fpm.conf找到pm.max_spare_servers 去除;号,同样非注释内容pm.max_spare_servers
                      WARNING: [pool www] pm.start_servers is not set. It's been set to 20.
                      编辑php-fpm.conf找到pm.start_servers 去除;号,同样非注释内容pm.start_servers
                      ERROR: bind() for address '127.0.0.1:9000' failed: Address already in use (98)
                      # netstat -ntlp|ps -ef|grep php查看进程,有N多进程
                      nobody 26152 26151 0 15:45 ? 00:00:00 php-fpm: pool www
                      nobody 26153 26151 0 15:45 ? 00:00:00 php-fpm: pool www
                      nobody 26154 26151 0 15:45 ? 00:00:00 php-fpm: pool www
                      nobody 26155 26151 0 15:45 ? 00:00:00 php-fpm: pool www
                      ...........................
                      #killall php-fpm 结束所有php-fpm进程
                      
    • php-fpm 启动脚本
      网上有各种版本的php-fpm开机自动启动脚本, 其实你编译后源目录已经生成自动脚本。不用做任何修改即用。
      cp {php-5.3.x-source-dir}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmd

                      #设置启动文件名
                      vim /etc/init.d/php-fpmd
                      #增加启动bash
                      #!/bin/bash
                      # v 0.0.1
                      # php-fpm – This shell script takes care of starting and stopping php-fpm.
                      #
                      # chkconfig: – 60 50
                      # description: php-fpm is a php-cgi service.
                      # processname: php-fpm
                      # config: /etc/php-fpm.ini
      
                      php_fpm_path="/usr/local/php"
                      php_fpm_conf="/usr/local/php/etc/php-fpm.conf"
                      php_fpm_pid="/usr/local/php/var/run/php-fpm.pid"
                      prog="php-fpm"
                      # Source function library
                     . /etc/rc.d/init.d/functions
                      # Source networking configuration.
                      . /etc/sysconfig/network
                      # Check that networking is up
                      [ ${NETWORKING} = "no" ] && exit 0
                      [ -x $php_fpm_path/sbin/php-fpm ] || exit 0
                      RETVAL=0
                      # Start daemons.
                      start() {
                          if [ -n $php_fpm_pid -a -e $php_fpm_pid ]; then
                              echo $"$prog already running…"
                              exit 1
                          fi
                          if [ -e $php_fpm_conf ]; then
                              echo -n $"Starting $prog: "
                              $php_fpm_path/sbin/php-fpm -y $php_fpm_conf &
                              RETVAL=$?
                              [ $RETVAL -eq 0 ] && success $"$prog"
                              echo
                              [ $RETVAL -eq 0 ] && return $RETVAL
                              pid created $php_fpm_pid
      
                              if [ -n "$try" ]; then
                                  RETVAL=1
                              else
                                  RETVAL=0
                              fi
                          else
                              RETVAL=1
                          fi
                          return $RETVAL
                      }
                      # Stop daemons.
                      stop() {
                          echo -n $"Stopping $prog: "
                          killproc $php_fpm_path/sbin/php-fpm -TERM
                          RETVAL=$?
                          pid removed $php_fpm_pid
      
                          if [ -n "$try" ]; then
                              RETVAL=1
                              failure
                          else
                              RETVAL=0
                              success
                          fi
                          echo
                          [ $RETVAL = 0 ] && rm -f $php_fpm_pid
                          return $RETVAL
                      }
                      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
                          try=$(expr $try + 1)
                          sleep 1
                          done
                      }
                      # See how we were called.
                      case "$1" in
                          start)
                              start
                          ;;
                          stop)
                              stop
                          ;;
                          restart)
                              stop
                              start
                          ;;
                          status)
                              status $prog
                              RETVAL=$?
                          ;;
                      *)
                      echo $"Usage: $0 {start|stop|restart|status}"
                      exit 1
                      esac
                      exit $RETVAL
      
                      #增加执行权限
                      chmod a+x /etc/init.d/php-fpmd
                      

      为方便启动php-fpm,设置变量

                      # vi .bashrc
                      alias  serfpm='service php-fpmd'
                      配置生效
                      # source .bashrc
                      

      开启php-fpm

                      [root@**]# serfpm restart
                      Stopping php-fpm:            [  OK  ]
                      Starting php-fpm:            [  OK  ]
                      
相关文章
  1. php-redis扩展
  2. Gearman-PHP扩展源码编译
  3. Zend Opcache 加速 PHP
  4. php安装memcached扩展
  5. php下添加pdo_mysql扩展
  6. Nginx使用的php-fpm的两种进程管理方式及优化
本站版权
1、本站所有主题由该文章作者发表,该文章作者与尘埃享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和尘埃的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:
二维码
Posted in lnmp源码安装
Comments are closed.