在centos5下编译安装php记录
最近有一款软件需要以php模块的方式来运行,于是研究了下如何在centos5下编译安装php,整个过程还是蛮多心得的,记录下来和大家分享经验。
一、编译安装php环境准备
编译之前,我们要做的工作是把编译安装php的环境建立起来,这点在php的官方网站上也可以找到,我们需要安装的软件有:
- autoconf: 2.13 (2.59+ for PHP 5.4+)
- automake: 1.4+
- libtool: 1.4.x+ (except 1.4.2)
- bison: 1.28, 1.35, 1.75, 2.0 or higher
- flex (PHP 5.2 and earlier): 2.5.4 (not higher)
- re2c: 0.13.4+
另外还要准备gcc编译器和make,这个一般的linux环境下默认都是有的。当然也不排除RP问题一个都没有(例如笔者^_^!),这也不用担心,使用yum全部安装一遍即可:
yum install gcc libxml2-devel make automake autoconf libtool bison flex
上面提到的re2c,这个比较特殊需要下载安装:
wget http://ftp5.gwdg.de/pub/opensuse/repositories/home:/fisu81:/wsf/CentOS_CentOS-5/x86_64/re2c-0.13.6-76.1.x86_64.rpm
rpm -Uvh re2c-0.13.6-76.1.x86_64.rpm
除此之外,还要一些额外的依赖,本例中php将以apache模块的方式安装,所以需要安装httpd。
yum install httpd httpd-devel mysql-server mysql-devel
yum install bzip2-devel gmp-devel
准备工作全部完成,接下来去php网站下载源代码。
二、php版本的选择
php版本有5.3、5.4、5.5甚至更新的6.0,可以从git上download下来,如果不会使用git请参考《centos使用yum安装git》。如果有的话可以忽略这篇文章,直接看继续看后面:
git clone http://git.php.net/repository/php-src.git
cd php-src
git checkout PHP-5.4
上面获取的php是最新的开发版本,如果是生产环境,我们一般会选择stable稳定版本,可以直接从官网上下载、解压。
下面说两点注意事项:
- 关于php线程安全的问题。php编译有TS(Thread Safe)和NTS(Non Thread Safe)两种方式,默认为NTS方式,一般用于cgi情况下,如nginx+php;而TS方式多用于Apache+php,如apache运行在MPM线程安全环境,则php强制使用TS方式编译。
- php扩展的加载方式。很多人喜欢自己编译安装php的原因之一就是可以将自己常用的php扩展模块静态编译到php中。然而,笔者并不推荐以静态方式加载扩展模块。原因很简单,扩展本生就是为了灵活使用,牺牲灵活性换回的一点点性能完全没有必要。
三、php编译参数的选择
下面是笔者使用的php编译参数:
‘./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=/usr/com’ ‘–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’ ‘–with-t1lib=/usr’ ‘–without-gdbm’ ‘–with-gettext’ ‘–with-gmp’ ‘–with-iconv’ ‘–with-jpeg-dir=/usr’ ‘–with-openssl’ ‘–with-pcre-regex’ ‘–with-zlib’ ‘–with-layout=GNU’ ‘–enable-exif’ ‘–enable-ftp’ ‘–enable-magic-quotes’ ‘–enable-sockets’ ‘–with-kerberos’ ‘–enable-ucd-snmp-hack’ ‘–enable-shmop’ ‘–enable-calendar’ ‘–with-libxml-dir=/usr’ ‘–enable-xml’ ‘–with-system-tzdata’ ‘–with-mhash’ ‘–with-apxs2=/usr/sbin/apxs’ ‘–libdir=/usr/lib64/php’ ‘–enable-pdo=shared’ ‘–with-mysql=shared,/usr’ ‘–with-mysqli=shared,/usr/lib64/mysql/mysql_config’ ‘–with-pdo-mysql=shared,/usr/lib64/mysql/mysql_config’ ‘–with-pdo-sqlite=shared,/usr’ ‘–without-gd’ ‘–disable-dom’ ‘–disable-dba’ ‘–without-unixODBC’ ‘–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’
这么多参数,笔者在这里不打算意义介绍了,大家可以google或baidu下,如果需要线程安全需要使用下面选项。
–enable-maintainer-zts
激活php的线程安全机制(Thread Safe Resource Manager(TSRM)/Zend Thread Safety(ZTS)),使我们开发出的程序是线程安全的。下面是shell代码:
$ cd php-src
$ ./buildconf
$ ./configure ……参数
$ make
$ make install
四、常见php编译错误
/php-src/Zend/zend_language_parser.y:50.1-5: invalid directive: `%code’
/php-src/Zend/zend_language_parser.y:50.7-14: syntax error, unexpected identifier
make: *** [/freemouse/php-src/Zend/zend_language_parser.c] 错误 1
这是由于bison版本太低的缘故造成的,如果编译php5.4以上版本,bison版本<=2.3,会出现上述错误,可以从bison官方网站上2.3+以上版本编译、安装。
wget http://ftp.gnu.org/gnu/bison/bison-2.4.1.tar.gz
tar -zxf bison-2.4.1.tar.gz
cd bison-2.4.1
./configure –prefix=/usr
make
make install
collect2 ld returned 1 exit status make *** sapi/cli/php error 1
反复编译可能会出现的错误,make clean清理即可。
cp: cannot stat `sapi/cli/php.1′: No such file or directory
最后安装到目录时会出现的错误,要从头开始:
./buildconf –force
这个错误不是每次都会出现的,所以仅供各位参考一下。