PHP5.5.12使用原生的Zend Opcache替代 Zend Optimizer和Zend Guard Loader

Zend Opcache 原生支持模式 在 PHP 5.5.12 编译参数里加入 –enable-opcache 即可,如下
php.net官方推荐更新到 PHP 5.5.12

1, PHP 5.5.12安装及支持Zend Opcache

先添加用户及用户组

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

编译安装PHP 5.5.12 所需的支持库,按顺序依次安装如下软件:

1.1 下载libiconv 1.14 并安装

Libiconv URL: http://www.gnu.org/software/libiconv/ http://ftp.gnu.org/pub/gnu/libiconv/

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz && cd libiconv-1.14
./configure --prefix=/usr/local/libiconv --enable-extra-encodings && make && make install && cd ../

注:–enable-extra-encodings 选项是为了能支持以下扩展字符集,详情请参考:http://www.gnu.org/software/libiconv/
主要是为了支持Magento的 ISO-2022-JP Mail for Japanese
插件所需要的 ISO-2022-JP-3 字符集

European languages
CP{437,737,775,852,853,855,857,858,860,861,863,865,869,1125}
Semitic languages
CP864
Japanese
EUC-JISX0213, Shift_JISX0213, ISO-2022-JP-3
Chinese
BIG5-2003 (experimental)
Turkmen
TDS565
Platform specifics
ATARIST, RISCOS-LATIN1

1.2, 下载 mhash 0.9.9.9 并安装

URl: http://sourceforge.net/projects/mhash/

wget http://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2 
tar jxvf mhash-0.9.9.9.tar.bz2 && cd mhash-0.9.9.9
./configure && make && make install && cd ../
/sbin/ldconfig

1.3,下载 mcrypt 2.6.8 并安装

myrypt URL: http://sourceforge.net/projects/mcrypt/?source=dlpaz

wget http://hivelocity.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
tar zxf mcrypt-2.6.8.tar.gz && cd mcrypt-2.6.8
./configure && make && make install
/sbin/ldconfig
cd ../

1.4, 下载 libmcrypt 2.5.8 并安装

URL: http://sourceforge.net/projects/mcrypt/files/Libmcrypt/

wget http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar zxf libmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8
./configure && make && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install && make && make install
cd ../../

创建系统软连接

ln -s /usr/local/lib/libmcrypt.* /usr/lib/
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/lib/libmhash.* /usr/lib/

1.5 PHP 5.5.12 下载及安装 http://php.net/downloads.php
注意:
A: –enable-opcache 这个参数是 php 5.5 开始默认支持的
B: Use mysqlnd by default
现在mysql, mysqli, pdo_mysql默认使用mysqlnd本地库,在PHP5.4以前需要:

$./configure --with-mysqli=mysqlnd

现在:

$./configure --with-mysqli
wget http://us1.php.net/distributions/php-5.5.12.tar.gz #国外VPS用
wget http://cn2.php.net/distributions/php-5.5.12.tar.gz #国内VPS 用
tar zxvf php-5.5.12.tar.gz && cd php-5.5.12
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-cgi \
--enable-fpm \
--enable-pdo \
--enable-opcache \
--with-pdo-sqlite \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--with-mysql-sock \
--with-iconv=/usr/local/libiconv \
--enable-mbstring \
--with-mcrypt \
--with-mhash \
--with-gd \
--enable-gd-native-ttf \
--with-gettext \
--with-freetype-dir=lib \
--with-jpeg-dir=lib \
--with-png-dir=lib \
--with-xpm-dir=lib \
--with-libxml-dir=/usr \
--enable-xml \
--enable-exif \
--with-zlib \
--with-bz2 \
--enable-zip \
--with-curl \
--with-xmlrpc \
--enable-soap \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-shmop \
--enable-json \
--with-pear \
--with-openssl \
--with-libdir=lib \
--enable-bcmath \
--enable-calendar \
--enable-ftp \
--enable-inline-optimization \
--with-kerberos

make ZEND_EXTRA_LIBS='-liconv'
make install

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --level 2345 php-fpm on

#AAA 此处有个懒人包下载,直接复制到您的服务器即可,如果部分PHP插件没安装,您需要根据需要去注释掉php.ini, php-fpm.conf 中的部分插件配置信息, 简单快速的定位到修改处办法 vi 打开相关文件,/extention_dir 就OK
cd /usr/local/php/etc/
wget http://www.qicai.us/host/php.ini
wget http://www.qicai.us/host/php-fpm.conf

#如果有AAA 复制,以下关于php.ini, php-fpm.conf 的配置可以忽略
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp php.ini-production /usr/local/php/etc/php.ini


#配置php.ini 文件
sed -i "s#expose_php = On#expose_php = Off#g" /usr/local/php/etc/php.ini
sed -i "s#max_execution_time = 30#max_execution_time = 300#g" /usr/local/php/etc/php.ini
sed -i "s#max_input_time = 60#max_input_time = 600#g" /usr/local/php/etc/php.ini
sed -i "s#;error_log = php_errors.log#error_log = /usr/local/php/var/log/php_errors.log#g" /usr/local/php/etc/php.ini
sed -i "s#post_max_size = 8M#post_max_size = 100M#g" /usr/local/php/etc/php.ini
sed -i "s#;cgi.fix_pathinfo=1#cgi.fix_pathinfo=0#g" /usr/local/php/etc/php.ini
sed -i "s#;upload_tmp_dir =#upload_tmp_dir = /tmp#g" /usr/local/php/etc/php.ini
sed -i "s#;date.timezone =#date.timezone = Europe/London#g" /usr/local/php/etc/php.ini
#sed -i "s#;date.timezone =#date.timezone = Asia/Tokyo#g" /usr/local/php/etc/php.ini
 
#添加PHP-FPM的配置文件
cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf

#说明: pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers 
#这几个参数的值可以根据服务器内存的大小来调整,内存大的,设置的值就大
#公式 pm.start_servers = min_spare_servers + (max_spare_servers - min_spare_servers) / 2

#配置php-fpm.conf
sed -i "s#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;error_log#error_log#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;log_level = notice#log_level = warning#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#; process.max = 128#process.max = 128#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;slowlog#slowlog#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#; events.mechanism = epoll#events.mechanism = epoll#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#pm.max_children = 5#pm.max_children = 10#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#pm.start_servers = 2#pm.start_servers = 4#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#pm.min_spare_servers = 1#pm.min_spare_servers = 2#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#pm.max_spare_servers = 3#pm.max_spare_servers = 5#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;pm.max_requests = 500#pm.max_requests = 500#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;listen.allowed_clients#listen.allowed_clients#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#user = nobody#user = www#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#group = nobody#group = www#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#listen = 127.0.0.1:9000#;listen = 127.0.0.1:9000\nlisten = /tmp/php-cgi.sock#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;listen.owner = nobody#listen.owner = www#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;listen.group = nobody#listen.group = www#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;listen.mode = 0666#listen.mode = 0666#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;slowlog#slowlog#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;access.format#access.format#g" /usr/local/php/etc/php-fpm.conf
sed -i "s#;access.log = log/$pool.access.log#access.log = /usr/local/php/var/log/$pool.access.log#g" /usr/local/php/etc/php-fpm.conf
 

将php.ini中expose_php = On 改为 expose_php = Off

sed -i 's@expose_php = On@expose_php = Off@g' /usr/local/php/etc/php.ini
sed -i 's@#sendmail_path.*@#sendmail_path = /usr/sbin/sendmail -t@g' /usr/local/php/etc/php.ini

隐藏页面头部的PHP版本信息

错误1: error: mcrypt.h not found. Please reinstall libmcrypt.
今天在编译php的时候,出现如下错误php安装出错:configure: error: mcrypt.h not found. Please reinstall libmcrypt.,意思是,没有查找到mcrytp.h,需要安装libcrytp。

解决办法:
下面的地址下载libmarypt:

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz

安装:

tar -zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
mkdir -p /usr/local/libmcrytp
./configure prefix=/usr/local/libmcrytp/
make
make install

然后再安装PHP

错误2:/usr/bin/ld: cannot find -liconv
collect2: ld returned 1 exit status
make: *** [sapi/cli/php] 错误 1

解决办法:
1、下载libiconv函数库

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz

2、解压缩

tar -zxvf libiconv-1.14.tar.gz

3、安装libiconv

./configure --prefix=/usr/local/iconv 
       make 
       make install

4、重新编译php 增加编译参数–with-iconv-dir=/usr/local/iconv

错误3: make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1

PHP5.3以上版本编译出错:
make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1

这是由于内存小于1G所导致.
在./configure加上选项:
–disable-fileinfo
重新编译即可。

2,Zend Opcache (ex. Zend Optimizer+) 介绍及独立安装方法

Summary:The Zend OPcache provides faster PHP execution through opcode caching and optimization.

注意: zendopcache-7.0.3 对PHP 5.2.17 支持不好,会导致莫名其妙的 Nginx 502 bad gateway 错误,因此 PHP 5.2.x 版本的用户请不要安装,今天(2014-7-15)在给 赵伟 配置服务器的时候就遇到这个问题,PHP页面第一次打开正常,刷新就502,刷新N次后又正常显示PHP,但是再刷新又会遇到 502,如此反复,调试花了很多冤枉时间。2014-7-15 更新

Zen Opcache 适合 PHP 5.2 或更新的版本 如 5.3,5.4,5.5,未来会取消对5.2的支持,但是,PHP5.2-5.4需要独立安装。
Zen Opcache 自 PHP 5.5.0 被PHP 原生支持,Zend Opcache 已经集成在了PHP 5.5里面,编译安装PHP5.5的时候加上–enable-opcache就行了, 不需要独立安装

背景资料:
大家知道目前PHP的缓存插件一般有三个:APC、eAccelerator、XCache,但未来它们可能都会消失,因为PHP 5.5已经集成Zend Opcache,功能和前三者相似但又有少许不同,缓存速度据说比它们更快(注意:只是据说,我没测试过)。

这几个PHP加速插件的主要原理都相同,就是把PHP执行后的数据缓冲到内存中从而避免重复的编译过程,能够直接使用缓冲区已编译的代码从而提高速度,降低服务器负载,它们的效率是显而易见的,像drupal这种庞大的CMS,每次打开一个页面要调用数十个PHP文件,执行数万行代码,效率可想而知,在安装APC等加速器后打开页面的速度明显加快。

Zen Opcache 有国内PHP大牛 Laruence 参与维护.

http://en.wikipedia.org/wiki/List_of_PHP_accelerators#Zend_Opcache_.28ex._Zend_Optimizer.2B.29

下面是我在PHP 5.4下的独立安装方法:
2.1,Zend Opcache 下载地址:http://pecl.php.net/package/ZendOpcache

2.2 Zend Opcache 在PHP 5.4下的独立编译安装

wget http://pecl.php.net/get/zendopcache-7.0.3.tgz
tar zxvf zendopcache-7.0.3.tgz && cd zendopcache-7.0.3
phpize #如果执行出错,请使用完整路径 /usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

打开 php.ini 文件

vi /usr/local/php/etc/php.ini
#查找extension_dir
/extension_dir
#在; extension_dir = "ext" 这句下添加
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/"

2.3 Zend Opcache 配置

# zend_extension= 此处参数请看说明
zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/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

注意将 zend_extension = 后面的 /full/path/to/opcache.so 替换为你的 opcache.so 所在路径
比如我的服务器是 /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/opcache.so
128意思是给它分配128M内存,然后重启php,用phpinfo查看是否生效,显示下面的信息就说明生效了.

一般来说,按照以往的经验,如果加在ZendGuardLoader之前会稳定多。

保存 php.ini ,重启php

service php-fpm restart

查看Zend Opcache是否生效
可以通过phpinfo查看是否生效,下图是我的配置PHP扩展:

3, 安装 APCu 替代APC

注意:Zend Opcache 与 APCu 只需要安装其中一个即可。

APCu下载地址: http://pecl.php.net/package/APCu

wget http://pecl.php.net/get/apcu-4.0.4.tgz
tar zxvf apcu-4.0.4.tgz && cd apcu-4.0.4
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
export TEST_PHP_ARGS='-n'
make test
make install

APCu 推荐配置
Suggested Configuration (in your php.ini file)
———————————————-
在php.ini 文件中添加如下配置

extension = apcu.so
apc.enabled=1
apc.shm_size=32M
apc.ttl=7200
apc.enable_cli=1

4 安装igbinary

http://pecl.php.net/package/igbinary
igbinary 介绍
Description Igbinary is a drop in replacement for the standard php serializer. Instead of
time and space consuming textual representation, igbinary stores php data
structures in a compact binary form. Savings are significant when using
memcached or similar memory based storages for serialized data.

步骤 5 redis 启用一个参数–enable-redis-igbinary时需要
主要方便 redis, memcache 或者相似的基于内存存储序列化数据的加速插件使用的。

wget http://pecl.php.net/get/igbinary-1.1.1.tgz
tar zxvf igbinary-1.1.1.tgz && cd igbinary-1.1.1
phpize
./configure CFLAGS="-O2 -g" --enable-igbinary --with-php-config=/usr/local/php/bin/php-config
make && make install

#在php.ini 中加载插件
extension = igbinary.so

5 安装Redis的PHP扩展

http://pecl.php.net/package/redis

wget http://pecl.php.net/get/redis-2.2.4.tgz
tar zxvf redis-2.2.4.tgz && cd redis-2.2.4
phpize
#./configure [--enable-redis-igbinary] 
# 如果在步骤1.4中安装了 igbinary 则使用如下参数编译
./configure --enable-redis-igbinary --with-php-config=/usr/local/php/bin/php-config

#步骤4 插件 igbinary 未安装时执行这个
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

#在php.ini 中加载插件
extension = redis.so

5 安装 lzf

http://pecl.php.net/package/lzf

wget http://pecl.php.net/get/LZF-1.6.3.tgz
tar zxvf LZF-1.6.3.tgz && cd LZF-1.6.3

phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

#在php.ini 中加载插件
extension = lzf.so

6 安装 suhosin (PHP安全漏洞保护程序)

https://github.com/stefanesser/suhosin/
http://www.hardened-php.net/suhosin/download.html
注意:如果PHP版本高于 5.4 建议不要使用,编译安装不了。如果你有探索精神,爱折腾,可以试试这个
https://github.com/NewEraCracker/suhosin-patches/blob/master/patches/suhosin-0.9.33.php55.diff

wget http://download.suhosin.org/suhosin-0.9.33.tgz
tar zxvf suhosin-0.9.33.tgz && cd suhosion-0.9.33
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

#在php.ini 中加载插件
extension = suhosin.so

好测试下我们刚才加载的插件配置是否成功

/usr/local/php/sbin/php-fpm -t

如果配置文件没有问题,测试后就将输出这句
NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

successful 表示已经成功了
启动或重启php

#启动
service php-fpm start
#重启
service php-fpm restart

解决perl: warning: Setting locale failed错误

在给CentOS linux 上的php 编译插件时遇到如下问题
/usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = “c”
are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = “c”
are supported and installed on your system.
perl: warning: Falling back to the standard locale (“C”).

Annoying “perl: warning: Setting locale failed”

解决办法

1. Edit your user bash_profile

vi .bash_profile

2. Insert the following:

export LANGUAGE=en_US.UTF-8

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

3. Rerun your Bash Profile

. ~/.bash_profile