梦想的生活,就是去新西兰放羊

[LTMP搭建] Centos 6.5 安装配置 PHP

接上篇:http://www.cnblogs.com/antarctican/p/3748427.html (安装Mysql)

 (此处含PHP5.2.17 和 5.4.39)

 

 ★ 部分重要lib支持: 

  以下这些要看看有没有,如果没有,要先装上以供php编译GD用。

  1. freetype,常用并且很重要。它作为GD的一部分,提供统一的字体调用接口,通常用于支持验证码、文字水印等。前面的yum安装,可能多此一举动。

# wget -c http://softlayer-sng.dl.sourceforge.net/project/freetype/freetype2/2.5.3/freetype-2.5.3.tar.gz
# tar -zxvf freetype-2.5.3.tar.gz
# cd freetype-2.5.3
# ./configure --prefix=/usr/local/lib/freetype

  如果遇到错误,重试一次,或者搜索freetype-config复制一份到 freetype解压目录/builds/unix/:

    /usr/bin/install: cannot stat ‘./builds/unix/freetype-config’: No such file or directory

  2. libjpeg。php也经常要处理jpeg格式的图。

  jpeg:  当我所有东西安装完成后,发现 php 的 imagecreatefromjpeg() 不存在,。于是尝试装 jpegsrc(下载)

# wget -c http://www.ijg.org/files/jpegsrc.v9a.tar.gz
# ./configure --prefix=/usr/local/lib/jpeg # make && make install

  3. libpng 这个一样,略过。

   

一、编译安装

下载PHP。 我选择了日本线路,比内地和台湾线路快得多。

PHP的configure选项见 http://www.php.net/manual/zh/configure.about.php , ./configure --help 可查看具体参数

  血的教训:我的GD并未在此编译,而是使用扩展载入。我之所以要独立安装GD,原因如下:

   如果仅仅指定--with-gd, 而未指定--with-png-dir, --with-jpeg-dir, --with-freetype,当然容易顺利编译完成,但同时可能会无libjpeg、libpng等的支持,php中会找不到函数 imagecreatefromjpeg(), imagecreatefrompng()等情况;

   倘若指定正确的libpng等路径直接编译php,仍然非常容易报错,尤其是libXpm的提示——这个不能用RPM安装,源码包又难找到下载。

 已编译的扩展,不能动态载入。 因为GD相当于一套组件,所以留在后面,以extension方式载入。

 [root@localhost src]# wget -c http://jp2.php.net/distributions/php-5.3.28.tar.gz
[root@localhost local]# cd /usr/local/src/php-5.3.28
[root@localhost php-5.3.28]# ./configure
  --prefix=/usr/local/php
  --with-config-file-path=/usr/local/php/etc
  --with-mysql                         // 如果是RPM安装的mysql,不要指定这两项的路径,会导致出错
  --with-mysqli
  --with-pdo-mysql                      // 如果加路径,为 mysql 安装目录 
   --with-pdo-sqlite --with-iconv-dir=/usr/local/libiconv   --with-zlib   // --with-gd                        // 这4个选项我是GD需要的
   // --with-freetype-dir=/usr/local/lib/freetype   // --with-jpeg-dir=/usr/local/lib/jpeg
  // --with-png-dir=   --with-curl   --with-curlwrappers   --with-mcrypt                       // 再次在centos 7下安装是总找不到。 建议安装libmcrypt时不要--prefix   --with-openssl   --with-mhash   --without-pear   --with-ldap=shared   --with-ldap-sasl   --with-xmlrpc   // --with-pthread                    ## 要真正的支持多线程,开启 pthread
  // --enable-fastcgi                  ## 注: PHP 5.3+ 包含php-fpm, 默认开启fastcgi,所以没有这个选项。 此选项仅适用于PHP 4.3.0 - 5.3.3(不含)   --enable-fpm   --enable-gd-native-ttf   --enable-xml   --disable-rpath   // --enable-discard-path                ## 此选项,貌似apache才用。(我参考的原文貌似也有借鉴成分)   --enable-bcmath   --enable-shmop   --enable-sysvsem   --enable-inline-optimization   --enable-mbregex   --enable-mbstring   --enable-pcntl   --enable-sockets   --enable-soap   #### 合并,5.3.3 以下版PHP使用fastcgi须加上 --enable-fastcgi   # ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli --with-pdo-mysql --with-pdo-sqlite --with-iconv-dir=/usr/local/libiconv --with-zlib --with-exif --with-curl --with-curlwrappers --with-mcrypt --with-openssl --with-mhash --without-pear --with-ldap=shared --with-ldap-sasl --with-xmlrpc --enable-fpm --enable-gd-native-ttf --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-pcntl --enable-sockets --enable-soap   make && make install

   

        @configure 时错误提示: 如果在一同编译GD可能出现错误: configure: error: GD build test failed

   If configure fails try --with-xpm-dir=<DIR>
   configure: error: GD build test failed. Please check the config.log for details.

  经测以上 configure 顺利通过, Thank you for using PHP.   

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

     在 make test 或 make install 的时候, 末尾出现:

 
   /usr/bin/ld: cannot find -lltdl
  collect2: ld returned 1 exit status
  make: *** [sapi/fpm/php-fpm] Error 1

  @error 例如我遇到的  /usr/bin/ld: cannot find -lltdl 。这样类似错误的, 是由于无法找到 liblltdl (lxxx=libxxx)。

[root@localhost libmcrypt-2.5.7]# cd /usr/local/src/libmcrypt-2.5.7/libltdl
[root@localhost libltdl]# ./configure --enable-ltdl-install
[root@localhost libltdl]# make && make install

  通常这样已经安装到了/usr/lib/. 如果仍然不行,就是其他问题。 可查看 /etc/ld.so.conf.d/*,总之这里面conf文件必须要包含当前使用的lib路径,如 /usr/lib、/usr/local/lib等,lib目录找不到就建立软链接。

  ln -s /usr/lib/libltdl.so.x.x.x  /usr/local/ltdl)
  @error  而  collect2: ld returned 1 exit status, 这是由前面异常引起安装终止。 如:

    /usr/local/src/php-5.3.28/ext/iconv/iconv.c:1248: undefined reference to `libiconv'  

    iconv的解决方法是指定安装位置 --with-iconv-dir=/usr/local/libiconv]

  

  @error:  (据说是因为64位下lib路径的问题,俺未证实)


/usr/bin/ld: ext/ldap/ldap.o: undefined reference to symbol 'ber_strdup'
/usr/bin/ld: note: 'ber_strdup' is defined in DSO /lib64/liblber-2.4.so.2 so try
 adding it to the linker command line
/lib64/liblber-2.4.so.2: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1

 

  百度会有千篇一律的说法是在 makefile 加东西,但我尝试这个方法轻松解决了:

  使 ./configure --with-ldap=shared ;

  # ln -s /usr/lib64/liblber* /usr/lib/ ;

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

   编译PHP需要几十分钟时间, 耐心等。

 

二、 PHP 扩展模块 

   1. eAccelerator

[root@localhost src]# wget -c https://codeload.github.com/eaccelerator/eaccelerator/legacy.tar.gz/master      ## wget下载后文件包名为 master
[root@localhost src]# tar -zvxf eaccelerator-42067ac.tar.gz
[root@localhost src]# cd eaccelerator-42067ac
  #### 此时不能直接用 ./configure, 需要用到php自带的 phpize

 [root@localhost eaccelerator-42067ac]# export PHP_PREFIX=/usr/local/php

 [root@localhost eaccelerator-42067ac]# $PHP_PREFIX/bin/phpize

 [root@localhost eaccelerator-42067ac]# ./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config

 [root@localhost eaccelerator-42067ac]# make && make install

 

  提示  Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/  

  检查/etc/php.d是否存在,存在则复制 eaccelerator.ini 到该目录, 不存在,则在PHP中配置为PHP扩展。 必须将 [eaccelerator] 放在 [zend] 之前。

[eaccelerator]
extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/data/cache/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

 

2. 独立安装PDO_MYSQL(pdo_mysql早已附带在php源码包内,基本不用独立安装)——如果PHP已指定了正确参数安装了内建pdo_mysql,忽略此部分

   再次编译php5.4和mysql5.5都为较高版本时,无论怎么编译都载入不了这extension,我在这个上面足足浪费了一整天时间, 直到深夜也不能宽心,再查找,才知道我被PDO_MYSQL玩弄了,被常规思维所束缚。官方网站上面的最新版也是2006年出的,并且说:

    Do not use this, as PDO has been moved into core (the php source) so this pecl extension is dead.

    ——其实这个我下午就看到了,我也注意到了php-x.x.x/ext/pdo_mysql, 但并没有尝试用内置的。直到当晚我刚好顺便下了个PHP手册,发现上面的编译参数,我在官网下载的pdo_mysql并不能用。

  在编译php时加了 --with-pdo-mysql 而没有报错的话,就已经装编译进去了。可以使用命令查看已编译extension:

    /usr/local/php/sbin/php-fpm -m 

 

  如果实在没有编译,再尝试下面的方法手动编译:

# cd /usr/local/php/src/php-5.3.39/ext/pdo_mysql
# /usr/local/php/bin/phpize
// 注意:如果是RPM安装的Mysql,无须指定路径 --with-pdo-mysql 路径, 其值可以是[dir, mysqlnd]
# ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
[root@localhost pdo_mysql]# make && make install

     pdo_mysql 会被安装到 /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/pdo_mysql.so, 到php.ini加 extension=绝对路径。

   顺便提一下:如果使用本文的php-fpm脚本不能停止|重启等操作,kill强制结束php-fpm,重启nginx, 请看该脚本关于【php-fpm控制脚本无效】的说明。

 

  ========================================================================

   以下错误是我单独装的 mysql 5.5 的 RPM包,再装PDO_MYSQL出现的,不含官网列表中其他包,如果有下次,直接整mysql bundle 包得了。

    ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql

  @可能出现的错误:(mysql 是 5.5 的 RPM包安装的)

    checking for MySQL support for PDO... yes, shared

           checking for mysql_config... not found

           configure: error: Cannot find MySQL header files under

   于是我找了个 mysql-devel 包来安装跳过这个错误,

    rpm -ivh http://dev.mysql.com/get/Downloads/MySQL-5.5/MySQL-devel-5.5.39-2.el7.x86_64.rpm

  @又来一错误

     error: mysql_query missing!?  

 

   于是俺有安装了个mysql shared compat包, 解决了次

    rpm -ivh http://dev.mysql.com/get/Downloads/MySQL-5.5/MySQL-shared-5.5.39-2.el7.x86_64.rpm

 

  @make时再来一错误 

        usr/local/src/PDO_MYSQL-1.0.2/php_pdo_mysql_int.h:25:19: fatal error: mysql.h: No such file or directory  

  Google百度半小时无果。 ~~  正当我要吐血而亡的时候, 尝试了这个,

   # find / -name mysql.h    // 我的在 /usr/include/mysql/mysql.h

   # ln -s /usr/include/mysql/*  /usr/local/include/ 

  然后 install 完成。 

 

 

3.  独立安装GD2——其实可以不用独立安装。 但折腾的滴滴血泪告诉我,要独立装。 

  由于GD的部分图片格式不支持,我不得不重新编译php。 这一次,独立编译GD作为extension!

  这样如果GD还需要更多图片格式的lib或字体api的话,重新编译GD即可,不影响php,  GD相当于一个套件组。

// 可安装php解压目录下ext附带的GD
[root@localhost src]# cd php-5.4.32/ext/gd
// 或下载
[root@localhost src]# wget -c http://cznic.dl.sourceforge.net/project/gd2/gd-2.0.35.tar.gz [root@localhost src]# tar -zxvf gd-2.0.35.tar.gz [root@localhost src]# cd gd-2.0.35

[root@localhost gd-2.0.35]# /usr/local/php/bin/phpize
[root@localhost gd-2.0.35]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-jpeg-dir=/usr/local/lib/jpeg --with-png-dir=/usr/local/lib/libpng --with-freetype-dir=/usr/local/lib/freetype
[root@localhost gd-2.0.35]# make && make install

  这里的几个参数和php的几乎一抹一样,这是因为php也使用同样的参数从源码包ext里安装。

 和其他扩展一样,它会自动生成一个so文件:

  /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/gd.so

 在php.ini中载入即可。

 

三、 php-fpm配置

   cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

     编辑php-fpm.conf, 找到以下项: (php-fpm.conf 和 nginx.conf 配置相同的 user、group )

    pid = run/php-fpm.pid

    user=www

    group=www

    php_flag[display_errors] = on

  @ 创建php-fpm启动脚本:

  /etc/init.d/php-fpm :  

#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php-fpm
# config: /usr/local/php/etc/php.ini
 
# Source function library.
. /etc/rc.d/init.d/functions
 
PHP_PATH=/usr/local/php
DESC="php-fpm daemon"
NAME=php-fpm
# php-fpm路径
DAEMON=$PHP_PATH/sbin/$NAME
# 配置文件路径
CONFIGFILE=$PHP_PATH/etc/php-fpm.conf
# PID文件路径(在php-fpm.conf设置)
PIDFILE=$PHP_PATH/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
 
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
 
rh_start() {
  $DAEMON -y $CONFIGFILE || echo -n " already running"
}
 
rh_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
 
rh_reload() {
  kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
 
case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        rh_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        rh_stop
        echo "."
        ;;
  reload)
        echo -n "Reloading $DESC configuration..."
        rh_reload
        echo "reloaded."
  ;;
  restart)
        echo -n "Restarting $DESC: $NAME"
        rh_stop
        sleep 1
        rh_start
        echo "."
        ;;
  *)
         echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
         exit 3
        ;;
esac
exit 0 

  加入开机启动:

[root@localhost Desktop]# chkconfig --level 2345 php-fpm on
[root@localhost Desktop]# chmod +x /etc/init.d/php-fpm
[root@localhost Desktop]# /etc/init.d/php-fpm start

  =【php-fpm控制脚本无效】=

   如果使用此脚本停止php-fpm,但php仍能运行, 这样的情况很大可能就是pid的指定不一致。 使 php/etc/php-fpm.conf 的 pid 行与此脚本中一致或注释掉即可。

 

四、 php配置

 php.ini 

## 运行测试发现,很多编译时已选择的extension已经载入了, 这个php.ini已经不需开启这些extension, 除了编译PHP时未选择的 extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/pdo_mysql.so

 

 

其他:

  【重装php的过渡处理】:

    如果当前的php还在使用中又不想停止以免网站下线时间太长,  那就安装到一个新文件夹如php5,安装所有的其他配置都使用这个新目录;

 

    更改开机启动脚本里的 PHP_PATH 变量。

    如果因为某些原因,phpinfo里仍然使用了原先的php.ini, 可以停止php-fpm, 给它指定正确的php.ini:

      /usr/local/php5/sbin/php-fpm -c /usr/local/php5/etc


    若仍要使用原来的php安装路径,可以更改php文件夹名字来替换,然后有/usr/local/php/sbin/php-fpm -h 里有参数,指定正确的路径,再重启即可。

 

 

  接下篇: http://www.cnblogs.com/antarctican/p/3759816.html (配置Tengine)

 

 

posted @ 2014-05-26 12:19  Shautch Donne  阅读(3895)  评论(0编辑  收藏  举报