memcached 是以 LiveJournal 旗下 Danga Interactive 公司的 Brad Fitzpatric 为首开发的一款软件。现在已成为 mixi、hatena、Facebook、Vox、LiveJournal 等众多服务中提高 Web 应用扩展性的重要因素。许多 Web 应用都将数据保存到 RDBMS 中,应用服务器从中
读取数据并在浏览器中显示。但随着数据量的增大、访问的集中,就会出现 RDBMS 的负担加重、数据库响应恶化、网站显示延迟等重大影响。这时就该 memcached 大显身手了。memcached 是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态 Web 应用的速度、提高可扩展性。 Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。
这篇接上一篇博客 lnmp架构
给php增添缓存
[root@server1 ~]# ls # 先获取安装包
memcache-2.2.5.tgz
[root@server1 ~]# tar zxf memcache-2.2.5.tgz
[root@server1 ~]# vim /root/.bash_profile # 添加环境变量
10 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin
[root@server1 ~]# source /root/.bash_profile # 刷新
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# phpize # 初始化
[root@server1 memcache-2.2.5]# ls
[root@server1 memcache-2.2.5]# ./configure
[root@server1 memcache-2.2.5]# make && make install
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/etc/
[root@server1 etc]# ls
php-fpm.conf php-fpm.conf.default php.ini
[root@server1 etc]# vim php.ini # 给php添加扩展模块memcache
873 extension=memcache.so
[root@server1 etc]# /etc/init.d/php-fpm reload
[root@server1 etc]# php -m # 查看所有模块
[root@server1 etc]# cd
[root@server1 ~]# cd memcache-2.2.5
[root@server1 memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim memcache.php
23 define('ADMIN_PASSWORD','westos'); // Admin Password # 修改密码
28 $MEMCACHE_SERVERS[] = '172.25.78.1:11211'; // add more as an array
29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array # 注释掉这一行
外压测试缓存速率
[root@foundation78 Desktop]# ab -c 10 -n 5000 http://172.25.78.1/index.php # 10各个并发,5000各请求,从服务器里请求
[root@foundation78 Desktop]# ab -c 10 -n 5000 http://172.25.78.1/example.php # 直接从memcache里请求
给nginx添加缓存
[root@server1 ~] ls 获取软件包
openresty-1.13.6.1.tar.gz
[root@server1 ~]# nginx -s stop # 因为openresty里自带nginx
[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz
[root@server1 ~]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ./configure
[root@server1 openresty-1.13.6.1]# gmake && gmake install
[root@server1 openresty-1.13.6.1]# cd /usr/local/openresty/
[root@server1 openresty]# cd nginx/
这两个文件用来做外压测试
[root@server1 nginx]# cp /usr/local/lnmp/nginx/html/index.php /usr/local/openresty/nginx/html
[root@server1 nginx]# cp /usr/local/lnmp/nginx/html/example.php /usr/local/openresty/nginx/html
[root@server1 nginx]# cd conf/
[root@server1 conf]# vim nginx.conf
17 http {
18 upstream memcache{
19 server localhost:11211;
20 keepalive 512; # 最大保持512个不立即关闭的连接用于提升性能
21 }
54 location /memc {
55 internal; # 只接受内部访问,不接收外部http请求
56 memc_connect_timeout 100ms;
57 memc_send_timeout 100ms;
58 memc_read_timeout 100ms;
59 set $memc_key $query_string; # 使用Nginx内置的$query_string来作为key
60 set $memc_exptime 300; # 缓存失效时间
61 memc_pass memcache;
62 }
81 location ~ \.php$ {
82 set $key $uri$args;
83 srcache_fetch GET /memc $key;
84 srcache_store PUT /memc $key;
85 root html;
86 fastcgi_pass 127.0.0.1:9000;
87 fastcgi_index index.php;
88 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
89 include fastcgi.conf;
90 }
root@server1 conf]# cd ../sbin/
[root@server1 sbin]# ./nginx -t # 检测语法
[root@server1 sbin]# ./nginx # 启动nginx
[root@server1 sbin]# netstat -antlp
外压测试:
[root@foundation78 Desktop]# ab -c 10 -n 5000 http://172.25.78.1/index.php
[root@foundation78 Desktop]# ab -c 10 -n 5000 http://172.25.78.1/example.php