设为首页 收藏本站
查看: 760|回复: 0

[经验分享] LNMP编译安装Redis构建高速Redis缓存

[复制链接]

尚未签到

发表于 2018-11-7 09:14:31 | 显示全部楼层 |阅读模式
LNMP编译安装Redis构建高速Redis缓存
  前面已经讲过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。
1. 停止LNMP服务
/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-cgi2. 备份LNMP配置
mkdir -p ~/src &&cd ~/src\cp -rf /usr/local/nginx/conf ~/src/conf.bak\cp -rf /etc/init.d/nginx ~/src/nginx.bak3. 编译Redis
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 .*/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 on4. 编译gperftools
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/tcmalloc5. 下载Nginx源码包
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.patch6. 编译Nginx
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.bak7. 修改Nginx配置
  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;}8. 启动LNMP服务
/etc/init.d/nginx start/etc/init.d/mysql start/etc/init.d/php-fpm start/etc/init.d/redis_6379 start9. 性能测试比较
  测试环境是一个VMware虚拟机,操作系统为CentOS 5.8,内存256M,双核。Redis最大并发数为1024。
  测试分为Nginx一个worker进程和两个worker进程,都用Apache的ab程序进行压力测试,请求次数为20000,并发数分别为100和1000。
  测试程序为phpinfo.php:
  测试结果如下:
  开启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简单、未涉及数据库、测试环境等因素,这个测试结果对比还不明显,在实际应用中差距还要明显得多。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-631772-1-1.html 上篇帖子: 关于Redis的知识汇总[转] 下篇帖子: Redis 的安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表