LNMP环境配置 + gpertools, Redis, phpredis, memcached, Zend Guard Loader, suhosin, MariaDB,ngx_pagespeed

操作系统环境: CentOS 6.4 32bit
LNMP + gpertools, Redis, phpredis, memcached, Zend Guard Loader, suhosin, MariaDB,ngx_pagespeed

首先安装软件编辑所需要的系统环境

sudo -s
LANG=C
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libXpm-devel perl-devel 

一、安装gpertools

判断操作系统为32位或者64位,执行命令

getconf LONG_BIT

如果屏幕出现 64 则说明服务器或VPS上的操作系统是64位的。
64位的操作系统安装gpertools之前需要先安装 libunwind

1,安装gpertools在64位操作系统中的支持库文件libunwind
提示:libunwind 一般情况下不必指定安装路径,用默认的参数安装就好,否则很可能导致gpertools无法找到路径的问题。

rm -rf /usr/local/libunwind
wget -c http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
tar zxf libunwind-1.1.tar.gz && cd libunwind-1.1/
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install

2,安装Google gperftools 内存性能优化软件 https://code.google.com/p/gperftools/downloads/list

wget --no-check-certificate https://gperftools.googlecode.com/files/gperftools-2.1.tar.gz
tar zxf gperftools-2.1.tar.gz && cd gperftools-2.1/
./configure && make && make install && cd ../
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
mkdir -p /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc

二、安装Redis 数据库

1, 下载Redis http://redis.io/download

wget http://download.redis.io/releases/redis-2.8.7.tar.gz -O -|tar xz && cd redis-2.8.7

uname -m #确定下CPU类型:i686 或 i386
export CFLAGS=-march=x86_64 #根据上一条来确定x86_64这个值

make distclean
make
make USE_TCMALLOC=yes FORCE_LIBC_MALLOC=yes PREFIX=/usr/local/redis install
cp -pf redis.conf /usr/local/redis/6379.conf
touch /usr/local/redis/6379.log
useradd -M -r --home-dir /usr/local/redis redis

#修改6379.conf配置文件
sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/6379.conf
sed -i 's/redis.pid/redis_6379.pid/g' /usr/local/redis/6379.conf
sed -i "s/^.*bind 127.0.0.1.*/bind 127.0.0.1/g" /usr/local/redis/6379.conf
sed -i 's/timeout 0/timeout 300/g' /usr/local/redis/6379.conf
sed -i 's/logfile stdout/logfile \/usr\/local\/redis\/6379.log/g' /usr/local/redis/6379.conf
sed -i 's/dir .\//dir \/usr\/local\/redis/g' /usr/local/redis/6379.conf
sed -i "s/^.*maxclients 100.*/maxclients 10000/g" /usr/local/redis/6379.conf
sed -i "s/^.*maxmemory .*/maxmemory 256m/g" /usr/local/redis/6379.conf

#添加redis启动脚本
wget -c https://fzrxefesh.googlecode.com/files/redis_init_script_centos
mv redis_init_script_centos /etc/init.d/redis

#修改redis 启动脚本中的路径为当前配置的路径
执行如下两句脚本

sed -i 's/local\/bin/local\/redis\/bin/g' /etc/init.d/redis
sed -i 's/etc\/redis/usr\/local\/redis/g' /etc/init.d/redis

或者手动修改

vi /etc/init.d/redis

(1), 修改EXEC=/usr/local/bin/redis-server
为 EXEC=/usr/local/redis/bin/redis-server
(2), 修改 CLIEXEC=/usr/local/bin/redis-cli
为 CLIEXEC=/usr/local/redis/bin/redis-cli
(3), 修改 CONF=”/etc/redis/${REDISPORT}.conf”
为 CONF=”/usr/local/redis/${REDISPORT}.conf”

添加redis服务随系统启动

chmod +x /etc/init.d/redis
chkconfig redis on

修复redis 日志中的警告信息
# WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.

vi /etc/sysctl.conf
#添加一句脚本到文件最后
vm.overcommit_memory = 1
#保存改动
:wq
#让脚本生效
sysctl vm.overcommit_memory=1

#启动redis 服务
#可用命令 service redis {start|stop|restart}
service redis start

至此,redis 服务器端安装完成,请在安装完 PHP之后查看 4.1 PHPREDIS安装,让PHP支持redis

三、编译安装PHP 5.3.28

这步可以考虑用 PHP 5.5.11这个最新版本代替,教程见这里 PHP5.5使用原生的Zend Opcache加速

先添加用户及用户组

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

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

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

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

3, 下载 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/

4,下载 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 ../

5,下载 PHP-5.3.28 并安装 (PHP 5.3版本最近的更新日期12 Dec 2013)

#wget http://us1.php.net/distributions/php-5.3.28.tar.gz -O -|tar zxf && cd php-5.3.28
wget http://us1.php.net/distributions/php-5.3.28.tar.gz
#PHP 5.4.28 版本下载
#国外VPS用
wget http://us1.php.net/distributions/php-5.4.28.tar.gz
#国内VPS用
wget http://cn2.php.net/distributions/php-5.4.28.tar.gz

tar zxvf php-5.3.28.tar.gz && cd php-5.3.28
#特别说明,此处利用PHP的MySQL原生库mysqlnd安装,因此安装PHP不需要先安装MySQL
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-cgi \
--enable-fpm \
--with-iconv=/usr/local/libiconv \
--with-mcrypt \
--with-mhash \
--with-zlib \
--with-gettext \
--enable-exif \
--enable-zip \
--with-bz2 \
--enable-soap \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-shmop \
--with-pear \
--enable-mbstring \
--with-openssl \
--with-libdir=lib \
--with-mysql-sock \
--with-curl \
--with-gd \
--with-xmlrpc \
--enable-bcmath \
--enable-calendar \
--enable-ftp \
--enable-gd-native-ttf \
--with-freetype-dir=lib \
--with-jpeg-dir=lib \
--with-png-dir=lib \
--with-xpm-dir=lib \
--enable-pdo \
--with-pdo-sqlite \
--enable-inline-optimization \
--with-kerberos

make ZEND_EXTRA_LIBS='-liconv'
make install

#添加php.ini
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

#配置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-FPM到系统服务
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on

启动PHP

service php-fpm start

#其他可用命令
service php-fpm {start|stop|force-quit|restart|reload}
#或
/etc/init.d/php-fpm {start|stop|force-quit|restart|reload}

错误error: libXpm.(a|so) not found
解决办法:
首先确认系统是否已经安装过libXpm-devel

updatedb
locate libXpm

我的服务器环境如下:

/usr/lib64/libXpm.so
/usr/lib64/libXpm.so.4
/usr/lib64/libXpm.so.4.11.0
/usr/share/doc/libXpm-3.5.10
/usr/share/doc/libXpm-3.5.10/AUTHORS
/usr/share/doc/libXpm-3.5.10/COPYING
/usr/share/doc/libXpm-3.5.10/ChangeLog

如上结果说明libXpm 安装完好,只需要做个系统软连接即可,执行如下命令:

ln -sv /usr/lib64/libXpm* /usr/lib/
/sbin/ldconfig

如果没有,则如下处理
a, redhat、centos、fedora解决方法:

yum install libXpm-devel

b, debian、ubuntu解决方法:

apt-get install libxpm-dev

将PHP安装目录添加到系统环境PATH:
方法:修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码

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

最后:执行 命令使其生效

source /etc/profile
#检验刚才添加的路径是否生效:
echo $PATH

四,编译安装PHP扩展

4.1 phpredis

phpredis https://github.com/nicolasff/phpredis/

下载phpredis

git clone https://github.com/nicolasff/phpredis.git

安装 phpredis

/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config [--enable-redis-igbinary]
make && make install

打开 /usr/local/php/etc/php.ini
查找 extension_dir 在其后添加

extension = "redis.so"

PHP使用Redis实现Session储存,PHP session handler Redis
注意:redis 存储session因为很多程序自己定义了session目录,会导致很多奇奇怪怪的问题,因此非必要请勿尝试。
查找 session.save_handler和session.save_path 将这两行替换成

session.save_handler = redis
session.save_path = "tcp://localhost:6379/"

4.2, 安装 APCu 替代APC

APCu – APC User Cache
注意: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.3 编译安装APC (注意:如果执行安装了APCu,则不需要重复安装APC)

4.3.1 编译安装APC:Alternative PHP Cache

APC下载地址: http://pecl.php.net/package/APC/
beat 版 http://pecl.php.net/get/APC-3.1.13.tgz
stable 稳定版 http://pecl.php.net/get/APC-3.1.9.tgz

wget http://pecl.php.net/get/APC-3.1.9.tgz
tar xzf APC-3.1.9.tgz &&cd APC-3.1.9
/usr/local/php/bin/phpize  #增加扩展模块
./configure --enable-apc --enable-apc-mmap --with-php-config=/usr/local/php/bin/php-config && make && make install

安装完后会生成一个apc.so在/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/里面

复制生成的apc.so 到 extensions 目录

cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/apc.so /usr/local/php/lib/php/extensions/

4.3.2, 配置php.ini 加载APC

接下来修改php.ini
默认的php.ini在/usr/local/php/etc/php.ini 具体路径请根据实际服务器环境:
查找
extension_dir = “./”

然后在php.ini 文档的extension_dir 下面添加如下正确配置。

extension = "apc.so"
apc.enabled=1
apc.shm_segments=1
apc.shm_size=64M
apc.optimization=1
apc.num_files_hint=1024
apc.ttl=7200
apc.user_ttl=7200
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1
apc.cache_by_default=on
apc.max_file_size = 10M
apc.stat = 0   # 1 for dev, 0 for production, whether the source file is checked for mod date
apc.include_once_override = 1  # Use PHP5.3+ for include_once optimization

保存并退出 php.ini
重启php
注意:apc.enable_cli=1 这句是针对不在 apache 下安装PHP的cli模式下才需要的, 如 lnmp (Linux + Nginx + MySQL + PHP) 环境
具体模式请运行 php -v 来检查。如下则为 cli
PHP 5.3.10 (cli) (built: Mar 8 2012 00:05:22)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2010, by Zend Technologies

4.3.3, 检查APC是否正确配置并加载

方法一: 执行

/usr/local/php/bin/php -r "phpinfo();" |grep apc

出现类似如下的结果,则安装成功

apc
MMAP File Mask => /tmp/apc.XXXXXX
apc.cache_by_default => On => On
apc.canonicalize => On => On
apc.coredump_unmap => Off => Off
apc.enable_cli => Off => Off
apc.enabled => On => On
apc.file_md5 => Off => Off
apc.file_update_protection => 2 => 2
apc.filters => no value => no value
apc.gc_ttl => 3600 => 3600
apc.include_once_override => On => On
apc.lazy_classes => Off => Off
apc.lazy_functions => Off => Off
apc.max_file_size => 10M => 10M
apc.mmap_file_mask => /tmp/apc.XXXXXX => /tmp/apc.XXXXXX
apc.num_files_hint => 1024 => 1024
apc.preload_path => no value => no value
apc.report_autofilter => Off => Off
apc.rfc1867 => Off => Off
apc.rfc1867_freq => 0 => 0
apc.rfc1867_name => APC_UPLOAD_PROGRESS => APC_UPLOAD_PROGRESS
apc.rfc1867_prefix => upload_ => upload_
apc.rfc1867_ttl => 3600 => 3600
apc.serializer => default => default
apc.shm_segments => 1 => 1
apc.shm_size => 64M => 64M
apc.slam_defense => On => On
apc.stat => Off => Off
apc.stat_ctime => Off => Off
apc.ttl => 7200 => 7200
apc.use_request_time => On => On
apc.user_entries_hint => 4096 => 4096
apc.user_ttl => 7200 => 7200
apc.write_lock => On => On

方法二:

<?php
echo phpinfo();
?>

Ctrl + F 搜索 APC
如果找到,则APC安装成功,具体配置参数会有显示

4.4 安装memcache与memcached

先安装autoconf
BTW:像这种系统级的编译软件,最好别自定–prefix=安装路径,其他程序编译的时候有默认的调用路径,如果你没有制定路径,容易出错。

wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar zxvf autoconf-latest.tar.gz && cd autoconf-2.69/
./configure
make && make install && cd ../

4.4.1 下载编译安装服务器端memcached http://memcached.org/
首先需要安装libevent http://libevent.org/
如果是centos可以yum安装

yum install libevent-devel

如果不支持yum的系统,可以这样装libevent http://libevent.org/
http://www.monkey.org/~provos

# cd /usr/local/src
# wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
# tar vxf libevent-1.4.12-stable.tar.gz
# cd libevent-1.4.12
# ./configure --prefix=/usr/local/libevent
# make && make install 

安装memcached

wget http://www.memcached.org/files/memcached-1.4.17.tar.gz
tar zxf memcached-1.4.17.tar.gz && cd memcached-1.4.17
./configure --prefix=/usr/local/memcached --with-php-config=/usr/local/php/bin/php-config --with-libevent=/usr/local/libevent
make && make 

新建启动和关闭脚本并赋执行权限

cp scripts/memcached.sysv /etc/init.d/memcached
chmod 755 /etc/init.d/memcached
#添加系统服务
chkconfig memcached on
#检查是否添加成功
chkconfig --list memcached

出现如下这行,则说明添加成功
memcached 0:off 1:off 2:on 3:on 4:on 5:on 6:off

修改配置:

vi /etc/init.d/memcached

查找:daemon memcached -d -p

/daemon memcached -d -p

回车

daemon memcached -d -p
修改为 daemon /usr/local/memcached/bin/memcached -d -p
将chown $USER /var/run/memcached
修改为
chown $USER /usr/local/memcached/bin/memcached
保存退出编辑/etc/init.d/memcached
启动memcached

service memcached start

停止memcached

service memcached stop

遇见locate, updatedb 命令无法使用,bash: locate: command not found 这个错误的解决办法

yum -y install mlocate

问题解决了.

4.4.2 下载安装PHP插件Memcache http://pecl.php.net/package/memcache

wget http://pecl.php.net/get/memcache-3.0.8.tgz
tar zxvf memcache-3.0.8.tgz
cd memcache-3.0.8/
export PHP_AUTOCONF="/usr/local/bin/autoconf"
export PHP_AUTOHEADER="/usr/local/bin/autoheader"
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install && cd ../

打开 /usr/local/php/etc/php.ini 查找 ; extension_dir = “ext”
  在其后增加一行:

extension = memcache.so

4.5 安装Zend Guard Loader在PHP5.4环境下,选择合适的版本

Zend Guard Loader下载地址
Zend Guard Loader for Linux
PHP 5.4
Zend Guard Loader (Runtime for PHP 5.4) 6.0.0 (32 bit)
Linux 32位
http://downloads.zend.com/guard/6.0.0/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz

Zend Guard Loader (Runtime for PHP 5.4) 6.0.0 (64 bit)
Linux 64位
http://downloads.zend.com/guard/6.0.0/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz

PHP 5.3
Zend Guard Loader (Runtime for PHP 5.3) 5.5.0 (32 bit)
http://downloads.zend.com/guard/5.5.0/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz

Zend Guard Loader (Runtime for PHP 5.3) 5.5.0 (64 bit)
http://downloads.zend.com/guard/5.5.0/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz

PHP 5.2.x
ZendOptimizer-3.3.9(可选)
ZendOptimizer的作用是分析,优化由Zend 编译器加密产生的代码的程序,对不是Zend编译器加密过的程序,没有必要安装这个。
64位:http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
32位:http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz

Windows 32位平台
http://downloads.zend.com/guard/6.0.0/ZendGuardLoader-70429-PHP-5.4-Windows-x86.zip

本文以Linux 32位系统下PHP 5.4的Zend Guard Loader为例

先创建Zend Guard Loader保存目录

mkdir -p /usr/local/zend/
#下载Zend Guard Loader
wget http://downloads.zend.com/guard/6.0.0/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz
tar zxvf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz
cd ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386
cp php-5.4.x/ZendGuardLoader.so /usr/local/zend/

将Zend Guard Loader添加到PHP配置文件php.ini中去
最后执行

cat >>/usr/local/php/etc/php.ini<<EOF

[Zend Guard Loader] 
zend_extension="/usr/local/zend/ZendGuardLoader.so"
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path=

EOF

4.6 PHP保护程序suhosin 的安装

suhosin 官网 http://www.hardened-php.net/suhosin/download.html
https://github.com/stefanesser/suhosin

git clone https://github.com/stefanesser/suhosin.git
#或者 suhosin-0.9.33.tgz 这个文件在PHP 5.4 32位版本中编译安装的时候会出错,还是用 git 比较好
#wget http://download.suhosin.org/suhosin-0.9.33.tgz
#tar xvfz suhosin-0.9.33.tgz
cd suhosin
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

将 suhosin 添加到 php.ini中

vi /usr/local/php/etc/php.ini

#查找 extension_dir 后 添加
;suhosin - config
extension=suhosin.so
[suhosin]
suhosin.request.max_vars = 2048
suhosin.post.max_vars = 2048
suhosin.request.max_array_index_length = 256
suhosin.post.max_array_index_length = 256
suhosin.request.max_totalname_length = 8192
suhosin.get.max_value_length = 5120
suhosin.sql.bailout_on_error = no

检查suhosin 是否正确安装并生效,执行命令
方法一:

/usr/local/php/bin/php -r "phpinfo();" |grep suhosin

输出了很多 suhosin.开头的参数则说明成功
方法二:

/usr/local/php/bin/php -v

出现 with Suhosin v0.9.34-dev 这句就说明成功

PHP Suhosin 模块是什么
上面说了一堆 这个 Suhosin 模块, 哪么 Suhosin 究竟是个什么东西呢,
Suhosin 是 朝鲜语 保护神的英文音译, 他是一个PHP程序的保护拓展. 它的设计初衷是为了保护服务器和用户抵御哪些基于PHP运行的程序中已知或者未知的缺陷, Suhosin有两个独立的部分, 使用时可以分开使用或者联合使用. 第一部分是一个用于PHP核心的补丁, 它能抵御缓冲区溢出或者格式化串的弱点; 第二部分是一个强大的PHP扩展, 包含其他所有的保护措施. 可以把他看成是一个PHP的安全拓展或重要安全补丁.

五,编译安装MariaDB,MySQL的变种

首先:更新操作系统软件,安装必要软件包

yum update #防止GCC 版本过低,导致编译无法通过
yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make cmake

其次,添加用户及用户组

#groupadd -g 10001 mysql
# useradd -g 10001 -u 10001 -s /sbin/nologin  mysql

1, 先下载安装MariaDB 的依赖库ncurses http://ftp.gnu.org/pub/gnu/ncurses/
wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz
tar zxvf ncurses-5.9.tar.gz
cd ncurses-5.9
./configure
make && make install

2, 下载安装cmake http://www.cmake.org/cmake/resources/software.html
wget http://www.cmake.org/files/v2.8/cmake-2.8.11.2.tar.gz

3, 下载安装autoconf http://www.gnu.org/software/autoconf/
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
或 wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz

MariaDB 下载与安装 https://downloads.mariadb.org/

wget https://downloads.mariadb.org/f/mariadb-5.5.32/kvm-tarbake-jaunty-x86/mariadb-5.5.32.tar.gz/from/http:/mirror.jmu.edu/pub/mariadb

mkdir /data/db/innodb_data/ -p
mkdir /data/db/mysql_logs/binary_log -p
mkdir /data/db/mysql_logs/innodb_log -p
mkdir /data/db/mysql_logs/query_log -p
mkdir /data/db/mysql_logs/slow_query_log -p
mkdir /data/db/mysql_logs/error_log -p
mkdir -p /data/db/mysql_data
mkdir -p /data/db/tmp
mkdir -p /etc/mysql
groupadd -g 10001 mysql
useradd -g 10001 -u 10001 -s /sbin/nologin  mysql
wget https://downloads.mariadb.org/f/mariadb-5.5.34/kvm-tarbake-jaunty-x86/mariadb-5.5.34.tar.gz/from/http:/mirror.jmu.edu/pub/mariadb
tar zxf mariadb-5.5.34.tar.gz && cd mariadb-5.5.34

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \
-DMYSQL_DATADIR=/data/db/mysql_data -DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all \
-DWITH_SSL=bundled -DWITH_ZLIB=bundled -DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 \
-DMYSQL_TCP_PORT=7454  -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1

make && make install

# vi /etc/mysql/my.cnf

[mysqld] 
port            = 7454
socket          = /tmp/mysqld.sock
character_set_server=utf8
back_log = 600
log-error=/data/db/mysql_logs/error_log/server.err
max_connections = 1024
max_connect_errors = 100
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 32M
max_heap_table_size = 64M
read_buffer_size = 4M
read_rnd_buffer_size = 16M
join_buffer_size = 20M
thread_cache_size = 9
thread_concurrency = 10
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
#default-storage-engine = MYISAM
default-storage-engine = innodb
thread_stack = 256K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=/data/db/mysql_logs/binary_log/db-bin
expire_logs_days=10
binlog_format=STATEMENT
#general_log=1
#general_log_file=/data/db/mysql_logs/query_log/query.log
slow_query_log=1
long_query_time = 5
slow_query_log_file=/data/db/mysql_logs/slow_query_log/slow_query.log
server-id = 1
key_buffer_size = 32M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 300M
innodb_data_file_path = ibdata1:100M;ibdata2:100M;ibdata3:100M;ibdata4:100M:autoextend
innodb_data_home_dir=/data/db/innodb_data/
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 8M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_log_group_home_dir=/data/db/mysql_logs/innodb_log
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table=1
#tmpdir=/data/db/tmp
#slave_load_tmpdir=/data/db/tmp

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
pager=less -SFX
prompt=\\h/\\u:[\\d]>\\_
no-auto-rehash

[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 10M
write_buffer = 10M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
open-files-limit = 8192

设置启动服务

cp support-files/mysql.server /etc/rc.d/init.d/mysql
chmod +x /etc/rc.d/init.d/mysql
chkconfig --add mysql
chkconfig mysql on

chown -R mysql.mysql /data/db

chmod +x scripts/mysql_install_db
#设置MariaDB初始化环境
scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/db/mysql_data --defaults-file=/etc/mysql/my.cnf

#删除默认的my.cnf 文件,防止mysql启动不了
rm -rf /etc/my.cnf

#创建软链接
ln -s /etc/my.cnf /etc/mysql/my.cnf

#启动MariaDB
service mysql start
#MySQL安全设置,包括设置root 密码,删除匿名访问,禁止管理员远程访问,删除test数据库等必要措施
/usr/local/mysql/bin/mysql_secure_installation

为MySQL添加TCMalloc库的安装步骤(Linux环境)
修改MySQL启动脚本(mysqld_safe的路径根据你的MySQL安装位置而定):

vi /usr/local/mysql/bin/mysqld_safe

在# executing mysqld_safe的下一行,加上:

export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

保存后退出,然后重启MySQL服务器。

service mysql restart

检查是否加载成功

/usr/sbin/lsof -n | grep tcmalloc

mysqld 734 mysql mem REG 253,0 1853290 4907 /usr/local/lib/libtcmalloc.so.4.1.2

出现上面这句则说明加载成功!

六、编译安装Nginx

6.1 准备必要软件:

6.1.1, zlib http://www.zlib.net/

wget http://zlib.net/zlib-1.2.8.tar.gz

解压后的文件夹名 zlib-1.2.8
6.1.2, openssl-1.0.1e http://www.openssl.org/source/

wget http://www.openssl.org/source/openssl-1.0.1e.tar.gz

解压后的文件夹名 openssl-1.0.1e
6.1.3, pcre-8.34 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz

解压后的文件夹名 pcre-8.34

6.1.4, ngx_cache_purge https://github.com/FRiCKLE/ngx_cache_purge

wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.1.tar.gz -O ngx_cache_purge_2.1.tar.gz

解压后的文件夹名 ngx_cache_purge-2.1

6.1.5, srcache-nginx-module https://github.com/agentzh/srcache-nginx-module

wget https://github.com/agentzh/srcache-nginx-module/archive/v0.24.tar.gz -O srcache-nginx-module_v0.24.tar.gz

解压后的文件夹名 srcache-nginx-module-0.24

6.1.6, redis2-nginx-module https://github.com/agentzh/redis2-nginx-module

wget https://github.com/agentzh/redis2-nginx-module/archive/v0.10.tar.gz -O redis2-nginx-module_v0.10.tar.gz

解压后的文件夹名 redis2-nginx-module-0.10

6.1.7, echo-nginx-module https://github.com/agentzh/echo-nginx-module

wget https://github.com/agentzh/echo-nginx-module/archive/v0.46.tar.gz -O echo-nginx-module_v0.46.tar.gz

解压后的文件夹名 echo-nginx-module-0.46

6.1.8, ngx_devel_kit https://github.com/simpl/ngx_devel_kit

wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz -O ngx_devel_kit_v0.2.18.tar.gz

解压后的文件夹名 ngx_devel_kit-0.2.18

6.1.9, set-misc-nginx-module https://github.com/agentzh/set-misc-nginx-module

wget https://github.com/agentzh/set-misc-nginx-module/archive/v0.23.tar.gz -O set-misc-nginx-module_v0.23.tar.gz

解压后的文件夹名 set-misc-nginx-module-0.23

6.1.10, ngx_http_redis https://github.com/splitice/ngx_http_redis

wget -c http://people.freebsd.org/~osa/ngx_http_redis-0.3.6.tar.gz -O -|tar xz

git clone https://github.com/splitice/ngx_http_redis.git

解压后的文件夹名 ngx_http_redis

6.1.11, ngx_pagespeed Google网页前端优化插件 https://github.com/pagespeed/ngx_pagespeed

wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.7.30.1-beta.zip -O ngx_pagespeed-1.7.30.1-beta.zip
unzip ngx_pagespeed-1.7.30.1-beta.zip # or unzip ngx_pagespeed-1.7.30.1-beta
cd ngx_pagespeed-release-1.7.30.1-beta/
wget https://dl.google.com/dl/page-speed/psol/1.7.30.1.tar.gz
tar -xzvf 1.7.30.1.tar.gz # expands to psol/

6.2 基础软件安装

sudo yum install gcc-c++ pcre-dev pcre-devel zlib-devel make

6.3 编译安装Nginx

./configure \
--user=www --group=www \
--prefix=/usr/local/nginx \
--with-pcre-jit \
--with-file-aio \
--with-http_addition_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-google_perftools_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_spdy_module \
--with-http_sub_module \
--with-http_realip_module \
--with-zlib=../zlib-1.2.8 \
--with-pcre=../pcre-8.34 \
--with-openssl=../openssl-1.0.1e \
--with-openssl-opt=no-krb5 \
--add-module=../ngx_http_redis \
--add-module=../redis2-nginx-module-0.10 \
--add-module=../echo-nginx-module-0.46 \
--add-module=../ngx_devel_kit-0.2.18 \
--add-module=../set-misc-nginx-module-0.22rc8 \
--add-module=../srcache-nginx-module-0.22 \
--add-module=../ngx_pagespeed-1.7.30.1-beta \
--add-module=../ngx_cache_purge-2.1 \
--with-cc-opt=' -O3'
#如编译时使用 --with-cc-opt='-O3' 增加GCC的优化;去掉Nginx的debug模式编译(debug模式会插入很多跟踪和ASSERT之类),这样编译后文件大小降低几倍之多;

make && make install

6.4 Nginx 启动脚本

打开 http://wiki.nginx.org/RedHatNginxInitScript 页面
复制文件,稍微修改下 Nginx安装路径及 nginx.conf 路径,修改后的如下

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:     /usr/local/nginx/conf/nginx.conf
# config:     /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

6.5 设置Nginx 随系统启动

vi /etc/init.d/nginx
i #进入编辑模式
#将上面启动脚本文件复制到nginx文件里
#按下键盘左上角 Esc 键,退出编辑模式
:wq #保存退出
chmod +x /etc/init.d/nginx #赋予启动脚本运行权限
chkconfig nginx on #添加到系统服务,本句等同于 chkconfig --level 2345 nginx on
service nginx start #启动Nginx 服务

6.6 修改优化nginx.conf 文件

创建ngx_pagespeed 模块的缓存文件

mkdir /var/ngx_pagespeed_cache
chown -R www.www /var/ngx_pagespeed_cache
chmod 0755 /var/ngx_pagespeed_cache

进入目录 /usr/local/nginx/conf
添加文件 ngx_pagespeed.conf 内容如下:

pagespeed On;

# needs to exist and be writable by nginx
pagespeed FileCachePath              "/var/ngx_pagespeed_cache/";
pagespeed FileCacheSizeKb            102400;
pagespeed FileCacheCleanIntervalMs   3600000;
pagespeed FileCacheInodeLimit        500000;

pagespeed LRUCacheKbPerProcess     8192;
pagespeed LRUCacheByteLimit        16384;

# let's speed up PageSpeed by storing it in the super duper fast memcached
#pagespeed MemcachedServers "host1:port1,host2:port2,host3:port3";

pagespeed MemcachedServers "localhost:11211";
pagespeed MemcachedTimeoutUs 100000;

pagespeed UseNativeFetcher on;
resolver 8.8.8.8;
pagespeed FetcherTimeoutMs  300;
pagespeed RewriteDeadlinePerFlushMs 300;
pagespeed ImageMaxRewritesAtOnce 6;
#pagespeed NumRewriteThreads 4;
#pagespeed NumExpensiveRewriteThreads

#pagespeed FetchWithGzip on;
#pagespeed CustomFetchHeader Accept-Encoding gzip;

pagespeed RespectVary on;

更多设置参考 https://github.com/pagespeed/ngx_pagespeed

修改后的nginx.conf文件如下

user  www www;
worker_processes  2;

error_log  logs/error.log crit;
pid        logs/nginx.pid;

#gperftools
google_perftools_profiles /tmp/tcmalloc/;

worker_rlimit_nofile 51200;

events {
        use epoll;
        worker_connections  25600;
        multi_accept on;
}

http {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile        on;
        open_file_cache max=200000 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 2;
        open_file_cache_errors on;
        open_log_file_cache    max=1000 inactive=20s min_uses=2 valid=1m;

        tcp_nopush     on;
        keepalive_timeout  60;
        keepalive_requests 100000;
        reset_timedout_connection on;
        client_body_timeout 10;
        send_timeout 2;

        server_tokens off;
        tcp_nodelay on;

#tomcat proxy add start
#       client_body_buffer_size 512k;
#       proxy_connect_timeout 300;
#       proxy_read_timeout 300;
#       proxy_send_timeout 300;
#       proxy_buffer_size 16k;
#       proxy_buffers 4 64k;
#       proxy_busy_buffers_size 128k;
#       proxy_temp_file_write_size 128k;
#       upstream tomcat_server {
#               server 127.0.0.1:8080;
#       }
#tomcat proxy add end

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;

        gzip  on;
        gzip_vary on;
        gzip_min_length  1k;
        gzip_proxied expired no-cache no-store private auth;
        gzip_buffers     4 16k;
        gzip_comp_level  3;
        gzip_types       text/plain application/x-javascript text/css application/xml
                         application/ecmascript application/javascript application/json
                         application/postscript image/svg+xml text/csv text/javascript
                         text/xml;

        gzip_http_version 1.0;

        limit_rate_after 3m;
        limit_rate 512k;

#       proxy_temp_path   /tmp/proxy_temp_dir;
#       proxy_cache_path  /tmp/proxy_cache_dir  levels=1:2   keys_zone=cache_one:50m inactive=1d max_size=1g;

        log_format access '$remote_addr - $remote_user [$time_local] $request '
                          '"$status" $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log logs/access.log access buffer=32k;

        upstream memcache {
            server localhost:11211;
            keepalive 512;
        }

    upstream redis {
        server 127.0.0.1:6379;
        keepalive 1024;
    }

    include ngx_pagespeed.conf;

    server {
            listen  80;
            server_name    _;
            access_log     /dev/null;
            root /home/wwwroot;
            index index.html;

        ## redis-nginx-module
        location = /redis {
            internal;
            set $redis_key $args;
            redis_pass redis;
        }
        ## redis2-nginx-module
        location = /redis2 {
            internal;
            set_unescape_uri $exptime $arg_exptime;
            set_unescape_uri $key $arg_key;
            redis2_query set $key $echo_request_body;
            redis2_query expire $key $exptime;
            redis2_pass redis;
        }

        ## file disable redis
        location ~ .*(gk13|3ds|ds|x|402p)\.(php|php5)?$ {
            root          /home/wwwroot;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include       fastcgi.conf;
        }

    location ~ \.php(|/.*)$ {
          try_files $uri =404;  
	  #Resolve the problem: No input file specified, when PHP file does not exist it does not show 404 error.

          charset        utf-8;
          default_type   text/html;

          ## srcache-nginx-module
          gzip on;
          redis_gzip_flag 1;
          set $key $uri;
          set_escape_uri $escaped_key $key;
          srcache_fetch GET /redis $key;
          srcache_store PUT /redis2 key=$escaped_key&exptime=300;

          fastcgi_pass       unix:/tmp/php-cgi.sock;
          #fastcgi_pass       127.0.0.1:9000;
          fastcgi_index      index.php;
          include            fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    }

        location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
            expires      30d;
        }

        location ~ .*\.(js|css)?$ {
            expires      1h;
        }
    }

    include vhost/*.conf;
}