CentOS 源码编译安装PHP 5.4 (mod_php)

PHP 5.4.0 正式版发布了,该版本包含大量的新特性,同时也修复了很多的 bug ,其中新特性有 traits、一些数组语法的提升、内建的 Web 服务器、性能提升等等,官方发行说明:http://php.net/releases/5_4_0.php

安装约定

php源码路径:/usr/local/src
php安装路径:/usr/local/php
php配置文件路径:/usr/local/php/etc/php.ini
php-fpm配置文件路径:/usr/local/php/etc/php-fpm.conf

下载源代码包

在官网如下地址下载最新版
http://www.php.net/downloads.php#v5

# cd /usr/local/src/
# wget http://cn2.php.net/distributions/php-5.4.x.tar.gz

创建www用户和组

# groupadd www
# useradd -g www -c "www user" -d /var/lib/www -s /sbin/nologin www

安装gcc、make等

# yum -y install gcc gcc-c++ make autoconf automake

安装编译php所需的库

# yum -y install zlib-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

安装php

# tar zxvf php-5.4.x.tar.gz
# cd php-5.4.x
# ./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd
# make
# make install

拷贝配置文件

# cp php.ini-production /usr/local/php/etc/php.ini

编辑php.ini,找到date.timezone,将前面的注释去掉,改为date.timezone = PRC,否则php -i | grep configure查看编译参数报警告如下

PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0

隐藏php版本信息

# sed -i 's/expose_php = On/expose_php = Off/' /usr/local/php/etc/php.ini

拷贝启动脚本

# mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm

设置php-fpm开机启动

# chkconfig php-fpm on

设置php环境变量

# vim /etc/profile

在其文件末尾添加如下变量

export PATH=$PATH:/usr/local/php/bin
export PATH=$PATH:/usr/local/php/sbin

或者用以下命令添加

# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/php/bin' /etc/profile
# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/php/sbin' /etc/profile

运行如下命令使环境变量生效

# source /etc/profile