最简单的LAMP环境搭建当然是通过yum来安装,但由于镜像仓库中的软件版本更新较慢,经常会遇到版本过旧的问题,尤其是安装一些新版本的CMS时的PHP。
这时我们需要手动编译PHP,Linux编译安装经常是./configure--make--make install三大步,但是实践中发现简单地这样安装会发现通过php -v呈现的版本号已经改变但是通过phpinfo()函数查看到的httpd使用的php版本还是没有改变。
回顾apache引入php支持的配置过程,其中最主要的是在httpd.conf中配置了LoadModule php5_module modules/libphp5.so,结合系统php已更新而apache中的php却未更新的现像,可以推测apache调用php并不是直接调用系统的php而就是调用libphp5.so;新编译安装php未成功替换掉apache中的php的原因就是没编译生成新的libphp5.so去替换旧的libphp5.so。
而最终的测试可以验证这种推测是正确的。以下是Apache升级PHP的步骤,这里有些操作比较粗操比如提示未知的编译选项--with-exec-dir等并未理会,生产环境按此升级需谨慎。
1.下载php
下载链接:http://php.net/downloads.php
选择需要版本下载,5.x版本和7.x版本还是有点区别的,建议原先是5.x的选择5.x版本下载7.x的版本选择7.x下载
2.安装apxs
apxs是apache扩展的编译程序,要确保apxs已安装
which apxs
如果不存在该命令则通过安装httpd-devel来安装:
装完后再次使用which命令定位apxs安装路径:
3.查看当前使用php编译参数
在apache网站根目录下写个phpinfo文件,然后在浏览器访问
cat > phpinfo.php << EOF <?php phpinfo(); ?> EOF
Configure Command右侧即当前PHP编译命令,我们使用一样的命令编译新的PHP即可(注意--with-apxs2的值是否上步安装的apxs所在的路径)
4.编译安装PHP
将压下载的PHP进入解压出的目录,使用./configure--make--make install三大步进行编译安装
./configure就直接复制上边查到的,单引号不影响的,有可能有些包没安装安装即可,有可能有些选项新版不支持这里我没管。如我这里使用:
\'./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=/var/lib\' \'--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\' \'--without-gdbm\' \'--with-gettext\' \'--with-gmp\' \'--with-iconv\' \'--with-jpeg-dir=/usr\' \'--with-openssl\' \'--with-pcre-regex=/usr\' \'--with-zlib\' \'--with-layout=GNU\' \'--enable-exif\' \'--enable-ftp\' \'--enable-magic-quotes\' \'--enable-sockets\' \'--enable-sysvsem\' \'--enable-sysvshm\' \'--enable-sysvmsg\' \'--with-kerberos\' \'--enable-ucd-snmp-hack\' \'--enable-shmop\' \'--enable-calendar\' \'--without-sqlite\' \'--with-libxml-dir=/usr\' \'--enable-xml\' \'--with-system-tzdata\' \'--with-apxs2=/usr/sbin/apxs\' \'--without-mysql\' \'--without-gd\' \'--disable-dom\' \'--disable-dba\' \'--without-unixODBC\' \'--disable-pdo\' \'--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\' make make install
configure报错:checking for BZip2 in default path... not found
解决办法:yum install -y gzip2-devel
configure报错:configure: error: Unable to locate gmp.h
解决办法:yum install -y gmp-devel
make报错:ext/pcre/.libs/php_pcre.o: In function `zm_info_pcre\': /usr/myapp/php-5.6.30/ext/pcre/php_pcre.c:133: undefined reference to `php_pcre_version\'....
解决办法:看意思是结构体没定义,可能是php_pcre.c没正确include但这可能性不大,也可能是安装的pcre版本太旧;不太清楚,最后我直接把./configure中的--with-pcre-regex=/usr去掉了,慎用。
5.再次查看phpinfo.php页面确认升级成功
先重启apache再刷新页面
service httpd restart
参考:
https://segmentfault.com/q/1010000005115503/a-1020000005116821
http://blog.csdn.net/work888study/article/details/8610467