Linux CentOS环境下PHP升级至5.5.0全流程解析
服务器运行环境为CentOS 5.4系统。
本次操作旨在将PHP版本由5.4.14升级至5.5.0。
整体升级过程较为顺畅,但需注意eAccelerator插件无法兼容PHP 5.5.0。鉴于PHP 5.5.0已默认集成Zend OPcache缓存机制,习惯使用eAccelerator的用户在升级后需暂时弃用该插件,转而依赖新的内置加速功能。
第一步是完成PHP 5.5.0的安装部署。
首先从官方镜像站点获取安装包,下载地址为:http://www.php.net/get/php-5.5.0.tar.gz/from/a/mirror
具体执行代码见下方。
# 解压缩安装包
tar zxvf php-5.5.0.tar.gz
# 进入解压后的目录
cd php-5.5.0
# 执行编译配置命令
./configure \
--prefix=/usr/local/webserver/php-d/php-5.5.0 \
--with-config-file-path=/usr/local/webserver/php-d/php-5.5.0/etc \
--with-config-file-scan-dir=/usr/local/webserver/php-d/php-5.5.0/etc/php.d \
--with-curl=/usr/local/lib \
--with-freetype-dir=/usr/lib64 \
--with-gd \
--with-gettext \
--with-iconv-dir=/usr/local/lib \
--with-jpeg-dir=/usr/lib64 \
--with-kerberos \
--with-ldap \
--with-ldap-sasl \
--with-libdir=lib64 \
--with-libxml-dir=/usr/lib64 \
--with-mcrypt \
--with-mhash \
--with-mysql \
--with-mysqli \
--with-openssl \
--with-pcre-regex=/usr \
--with-pdo-mysql=shared \
--with-pdo-sqlite=shared \
--with-pear=/usr/local/lib/php \
--with-png-dir=/usr/lib64 \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip \
--disable-rpath
make ZEND_EXTRA_LIBS='liconv'
make install
cp php.ini-production /usr/local/webserver/php-d/php-5.5.0/etc/php.ini
以下部分需要特别说明几点注意事项:
在安装过程中,请根据实际业务需求增删附加组件,并相应调整目录路径设置。
务必确保开启OPcache支持,即添加参数:–enable-opcache
PHP 5.3.10版本中曾包含–enable-safe-mode选项,但在PHP 5.4.0及更高版本中已移除。可通过 ./configure –help | grep “safe-mode” 验证,若无输出则表明该选项不再受支持。
同样被移除的参数还包括:'–enable-discard-path','–enable-fastcgi','–enable-force-cgi-redirect','–with-curlwrappers'。
第二步,编译安装PHP 5.5.0所需的扩展模块。
首先是安装imagick扩展。
此模块依赖服务器已安装ImageMagick库。因与PHP升级本身无直接关联,此处省略安装步骤,如有需要可自行搜索相关教程。
下载页面地址:http://pecl.php.net/package/imagick
相关代码示例如下。
tar xvzf imagick-3.1.0RC2.tgz
cd imagick-3.1.0RC2
/usr/local/webserver/php-d/php-5.5.0/bin/phpize
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
./configure --with-php-config=/usr/local/webserver/php-d/php-5.5.0/bin/php-config
make
make install
注意事项:
为防止兼容性问题,请使用最新版本,切勿使用3.0.1之前的旧版。
若安装时出现报错 make: *** [imagick_file.lo] 错误 1,请检查并调用pkgconfig工具解决。
上述所有目录路径均需根据个人实际情况进行修改。
接下来安装memcache扩展。
该模块要求服务器支持memcached服务。因与PHP升级核心流程无关,此处省略前置条件安装说明,有需要的用户请自行查阅资料。
下载地址:http://pecl.php.net/package/memcache
tar xvzf memcache-3.0.tgz
cd memcache-3.0.8
/usr/local/webserver/php-d/php-5.5.0/bin/phpize
./configure \
--enable-memcache \
--with-php-config=/usr/local/webserver/php-d/php-5.5.0/bin/php-config
make
make install
注:请勿使用2.2.6及以下版本,这些版本不兼容当前PHP环境。
随后安装phpredis-master扩展。
此模块依赖服务器已部署Redis服务。因与PHP升级无直接关系,此处省略Redis安装步骤,有需要者请自行搜索。
下载地址:https://github.com/nicolasff/phpredis
unzip master
cd phpredis-master
/usr/local/webserver/php-d/php-5.5.0/bin/phpize
./configure \
--enable-redis \
--with-php-config=/usr/local/webserver/php-d/php-5.5.0/bin/php-config
make
make install
至此,所有必需的扩展模块均已安装完毕。
第三步,配置php.ini文件。
vi /usr/local/webserver/php-d/php-5.5.0/etc/php.ini
# 找到extension_dir配置项
extension_dir = "/usr/local/webserver/php-d/php-5.5.0/lib/php/extensions/no-debug-non-zts-20121212/"
extension = "imagick.so"
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "redis.so"
# 找到date.timezone配置项
date.timezone = Asia/Shanghai
# 找到session.save_handler配置项
session.save_handler = redis
# 找到session.save_path配置项
session.save_path = "tcp://127.0.0.1:6379?weight=1"
第四步,配置Zend OPcache。
长期以来,许多开发者习惯使用eAccelerator为PHP提供性能加速,但目前面临两个主要问题:
eAccelerator目前尚不兼容PHP 5.5.0版本。
eAccelerator与Zend Opcache存在冲突,无法共存。
幸运的是,PHP 5.5.0默认提供了Zend Opcache作为替代加速方案,具体配置方法如下:
zend_extension = /usr/local/webserver/php-d/php-5.5.0/lib/php/extensions/no-debug-non-zts-20121212/opcache.so
; 上面zend_extension路径为opcache.so的实际路径
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
注意:如果在您的PHP扩展模块目录中找不到opcache.so文件,说明安装失败,请重新进行安装。
第五步,配置php-fpm.conf文件。
pid = /usr/local/webserver/php-d/php-5.5.0/var/run/php-fpm.pid
error_log = /usr/local/webserver/php-d/php-5.5.0/logs/php-fpm.log
log_level = notice
emergency_restart_threshold = 10
emergency_restart_interval = 60s
process_control_timeout = 5s
daemonize = yes
rlimit_files = 65535
rlimit_core = 0
user = www
group = www
listen.backlog = -1
listen.owner = www
listen.group = www
listen.mode = 0666
listen.allowed_clients = 127.0.0.1
pm = static
pm.max_children = 64
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1024
ping.response = pong
slowlog = /usr/local/webserver/php-d/php-5.5.0/logs/$pool.log.slow
request_slowlog_timeout = 0
request_terminate_timeout = 0
catch_workers_output = yes
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f jht2718@163.com
php_flag[display_errors] = on
备注:请根据自身服务器环境和需求修改配置文件中的各项参数。
第六步,修改启动项以完成部署。
cp /usr/local/webserver/php-d/php-5.5.0/bin/php /etc/init.d/php