前面已经讲过LNMP编译Memc和Srcache构建高速Memcached缓存,这次换另一种被广泛应用的缓存-Redis
。Redis实际上是一个高性能的key-value
数据库,他可以代替MySQL
;它也没有Memcached
的1 MB
的大小限制,更加自由;同时提供PHP
、Python
、Ruby
等客户端,使用很方便。
本文同样采用agentzh(章亦春)开发的几个Nginx模块:Redis2
、Echo
、Set-Misc
和Srcache
,具体原理可以查看agentzh的Github页面,还有Redis模块。接下来将详细介绍怎样在已经安装LNMP[Linux+Nginx+MySQL+PHP-FPM(FastCGI)]的基础上,升级Nginx和编译安装上述Nginx模块,同时配置Redis
服务器,修改Nginx
配置文件,最后做一个简单的性能测试,可与Memcached
简单比较下。本文安装测试环境为 CentOS 5.8 x86
。
/root/lnmp stop/etc/init.d/nginx stop/etc/init.d/mysql stop/etc/init.d/redis_6379 stop/usr/local/php/sbin/php-fpm stopkillall nginx mysqld redis php-cgi
mkdir -p ~/src &&cd ~/src\cp -rf /usr/local/nginx/conf ~/src/conf.bak\cp -rf /etc/init.d/nginx ~/src/nginx.bak
wget -c http://redis.googlecode.com/files/redis-2.4.17.tar.gz -O -|tar xzcd redis-2.4.17makemake installcd ../mkdir -p /var/redis/6379mkdir -p /var/log/redis/touch /var/log/redis/6379.loguseradd -M -r --home-dir /var/redis redischown -R redis:redis /var/log/redis/ /var/redis/mkdir -p /etc/redis\cp -pf redis-2.4.17/redis.conf /etc/redis/6379.confsed -i 's/daemonize no/daemonize yes/g' /etc/redis/6379.confsed -i 's/redis.pid/redis_6379.pid/g' /etc/redis/6379.confsed -i "s/^.*bind 127.0.0.1.*/bind 127.0.0.1/g" /etc/redis/6379.confsed -i 's/timeout 0/timeout 300/g' /etc/redis/6379.confsed -i 's/loglevel verbose/loglevel notice/g' /etc/redis/6379.confsed -i 's/logfile stdout/logfile \/var\/log\/redis\/6379.log/g' /etc/redis/6379.confsed -i 's/dir .\//dir \/var\/redis\/6379/g' /etc/redis/6379.confsed -i "s/^.*maxclients 128.*/maxclients 0/g" /etc/redis/6379.confsed -i "s/^.*maxmemory <bytes>.*/maxmemory 256m/g" /etc/redis/6379.confwget -c https://fzrxefesh.googlecode.com/files/redis_init_script_centos\cp -pf redis_init_script_centos /etc/init.d/redis_6379chmod +x /etc/init.d/redis_6379chkconfig redis_6379 on
if[`getconf LONG_BIT`='64']; thenrm -rf /usr/local/libunwind wget -c http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz -O -|tar xz cd libunwind-1.1/ ./configure --prefix=/usr/local/libunwind make && make install echo"/usr/local/libunwind/lib" >> /etc/ld.so.conf.d/usr_local_lib.conffiwget -c http://gperftools.googlecode.com/files/gperftools-2.0.tar.gz -O -|tar xzcd gperftools-2.0/./configuremake && make installcd ../echo"/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf/sbin/ldconfigmkdir -p /tmp/tcmallocchmod 0777 /tmp/tcmalloc
wget -c --no-check-certificate https://github.com/agentzh/redis2-nginx-module/tarball/master -O -|tar xz && mv -f agentzh-redis2-nginx-module* redis2-nginx-modulewget -c --no-check-certificate https://github.com/agentzh/echo-nginx-module/tarball/master -O -|tar xz && mv -f agentzh-echo-nginx-module* echo-nginx-modulewget -c --no-check-certificate https://github.com/simpl/ngx_devel_kit/tarball/master -O -|tar xz && mv -f simpl-ngx_devel_kit* ngx_devel_kitwget -c --no-check-certificate https://github.com/agentzh/set-misc-nginx-module/tarball/master -O -|tar xz && mv -f agentzh-set-misc-nginx-module* set-misc-nginx-modulewget -c --no-check-certificate https://github.com/agentzh/srcache-nginx-module/tarball/master -O -|tar xz && mv -f agentzh-srcache-nginx-module* srcache-nginx-modulewget -c http://people.freebsd.org/~osa/ngx_http_redis-0.3.6.tar.gz -O -|tar xzwget -c http://zlib.net/zlib-1.2.7.tar.gz -O -|tar xzwget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.gz -O -|tar xzwget -c http://www.openssl.org/source/openssl-1.0.1c.tar.gz -O -|tar xzwget -c http://nginx.org/download/nginx-1.2.4.tar.gz -O -|tar xzwget -c https://fzrxefesh.googlecode.com/files/nginx-1.2.4-upstream.patch
rm -rf /usr/local/nginxcd nginx-1.2.4/patch -p1 < ../nginx-1.2.4-upstream.patch./configure --prefix=/usr/local/nginx \--user=www --group=www \--with-zlib=/root/src/zlib-1.2.7 \--with-pcre=/root/src/pcre-8.31 \--with-openssl=/root/src/openssl-1.0.1c \--with-http_gzip_static_module \--with-http_stub_status_module \--with-http_realip_module \--with-http_ssl_module \--with-ipv6 \--with-google_perftools_module \--add-module=/root/src/ngx_http_redis-0.3.6 \--add-module=/root/src/redis2-nginx-module \--add-module=/root/src/echo-nginx-module \--add-module=/root/src/ngx_devel_kit \--add-module=/root/src/set-misc-nginx-module \--add-module=/root/src/srcache-nginx-module \--with-cc-opt='-O3'make && make installcd ../chkconfig nginx onrm -rf /usr/local/nginx/conf\cp -rf ~/src/conf.bak /usr/local/nginx/conf\cp -rf /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
Nginx配置文件在LNMP的基础上添加了Redis
、Redis2
和Srcache
等模块的参数,以php
结尾的请求结果都会被缓存。页面再次被请求时,nginx直接从Redis中读取,跳过了php,所以效率大大提高。
nginx.conf
配置如下:
user www www;worker_processes 1;error_log /home/wwwlogs/nginx_error.log crit;pid /usr/local/nginx/logs/nginx.pid;google_perftools_profiles /tmp/tcmalloc;#Specifies the value for maximum file descriptors that can be opened by this process.worker_rlimit_nofile 51200;events { use epoll; worker_connections 51200;}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;#tcp_nopush on; tcp_nodelay on; keepalive_timeout 60; 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 256k; gzip on; gzip_vary on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 5; gzip_types text/plain application/x-javascript text/css application/xml; #limit_zone crawler $binary_remote_addr 10m; upstream redis { server 127.0.0.1:6379; keepalive 1024; }#log format log_format access '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" $http_x_forwarded_for'; server { listen 80; server_name lazyzhu.com; root /home/wwwroot; location / { index index.html index.htm index.php; }## redis-nginx-modulelocation= /redis { internal; set$redis_key$args; redis_pass redis; }## redis2-nginx-modulelocation= /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; }## folder disable redis location ^~ /(wp-admin|admin)/ { root /home/wwwroot; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; }## file disable redis location ~ .*(admin|p)\.(php|php5)?$ { root /home/wwwroot; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } location ~ .*\.(php|php5)?$ {## srcache-nginx-moduleset$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_index index.php; include fcgi.conf; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location /status { stub_status on; access_log off; } access_log /home/wwwlogs/access.log access; }include vhost/*.conf;}
位于vhost
目录的lazyzhu.com.conf
配置如下:
log_format lazyzhu.com '$remote_addr - $remote_user [$time_local] $request ''$status $body_bytes_sent $http_referer ''$http_user_agent $http_x_forwarded_for';server { listen 80; server_name lazyzhu.com; root /home/wwwroot/lazyzhu.com; location / { index index.html index.htm index.php default.html default.htm default.php; } include none.conf; ## redis-nginx-modulelocation= /redis { internal; set$redis_key$args; redis_pass redis; }## redis2-nginx-modulelocation= /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; }## folder disable redis location ^~ /(wp-admin|admin)/ { root /home/wwwroot; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; }## file disable redis location ~ .*(admin|p)\.(php|php5)?$ { root /home/wwwroot; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; } location ~ .*\.(php|php5)?$ {## srcache-nginx-moduleset$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_index index.php; include fcgi.conf; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location /status { stub_status on; access_log off; } access_log /home/wwwlogs/lazyzhu.com.log lazyzhu.com;}
/etc/init.d/nginx start/etc/init.d/mysql start/etc/init.d/php-fpm start/etc/init.d/redis_6379 start
测试环境是一个VMware虚拟机,操作系统为CentOS 5.8,内存256M,双核。Redis最大并发数为1024。
测试分为Nginx一个worker进程
和两个worker进程
,都用Apache
的ab
程序进行压力测试,请求次数为20000
,并发数分别为100
和1000
。
测试程序为phpinfo.php
:
<?phpinfo();?>
测试结果如下:
开启Redis缓存-100并发数-一个worker进程
开启Redis缓存-100并发数-两个worker进程
通过对比可以发现,开启Redis缓存后,两个worker进程
比一个worker进程
优异,但优势不是很明显。
开启Redis缓存-1000并发数-一个worker进程
开启Redis缓存-1000并发数-两个worker进程
开启Redis缓存-1000并发数-三个worker进程
无Redis缓存-1000并发数-一个worker进程
通过对比可以发现,开启Redis缓存后,两个worker进程
比一个worker进程
优异,而且十分明显,而三个worker进程
与两个worker进程
相差不多,这与CPU为2核对应,多个worker未必提升性能,所以建议worker数设置为与CPU核数相同。也可得出高并发下,CPU多核的优势。
由于测试php简单、未涉及数据库、测试环境等因素,这个测试结果对比还不明显,在实际应用中差距还要明显得多。