實驗環境:RHEL7.0 server1.example.com 172.25.254.1
server2.example.com 172.25.254.2
實驗前提:已經編譯安裝完成php與nginx,具體内容可參考部落格前邊内容:。
實驗内容:1.memcache安裝
2.編譯安裝memcache使其支援php
3.修改php配置檔案并配置memcache的測試頁
4.測試
安裝包:memcache-2.2.5.tgz
1.memcache安裝(memcache的端口号是11211)
[root@server1 ~]# yum install memcached -y
[root@server1 ~]# /etc/init.d/httpd start
[root@server1 ~]# /etc/init.d/memcached start
[root@server1 ~]# curl -I localhost
HTTP/1.1 200 OK
Date: Sun, 11 Sep 2016 16:31:52 GMT
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sun, 11 Sep 2016 16:15:02 GMT
ETag: "1fca9-8-53c3dae8ae0cf"
Accept-Ranges: bytes
Content-Length: 8
Connection: close
Content-Type: text/html; charset=UTF-8
[root@server1 ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
STAT pid 2759
STAT uptime 14
STAT time 1473611553
STAT version 1.4.4
........................
STAT total_items 0
STAT evictions 0
END
2.編譯安裝memcache使其支援php
[root@server1 mnt]# php -m |grep memcache ### 可以看到php不支援memcached
[root@server1 ~]# cd /mnt/
[root@server1 mnt]# ls
memcache-2.2.5.tgz
[root@server1 mnt]# tar -zxf memcache-2.2.5.tgz ###解壓tar zxf memcache-2.2.5.tgz
[root@server1 mnt]# cd memcache-2.2.5 ###進入解壓目錄
[root@server1 memcache-2.2.5]# ls
[root@server1 memcache-2.2.5]# phpize ##會自動生成configure
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@server1 memcache-2.2.5]# yum install libtool -y
[root@server1 memcache-2.2.5]# ./configure \
> --prefix=/usr/local/lnmp/php/modules \
> --enable-memcache
[root@server1 memcache-2.2.5]# make ##編譯
[root@server1 memcache-2.2.5]# make install ###安裝
Installing shared extensions: /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
[root@server1 no-debug-non-zts-20131226]# ls
memcache.so opcache.a opcache.so
[root@server1 no-debug-non-zts-20131226]# /etc/init.d/memcached restart
3.編譯PHP配置檔案并配置memcache的測試頁
[root@server1 ~]# vim /usr/local/lnmp/php/etc/php.ini
863 extension=memcache.so
[root@server1 ~]# /etc/init.d/fpm restart
[root@server1 ~]# php -m |grep mem ###檢視php是否支援memcached
memcache
[root@server1 ~]# cd /mnt/memcache-2.2.5
[root@server1 memcache-2.2.5]# cp example.php memcache.php /usr/local/lnmp/nginx/html/
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim example.php ##分析資訊不做修改
22 define('ADMIN_USERNAME','memcache'); // Admin Username
23 define('ADMIN_PASSWORD','willis'); // Admin Password ##willis為密碼
28 $MEMCACHE_SERVERS[] = '127.0.0.1:11211'; // add more as an array
29 #$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
[root@server1 html]# nginx -s reload
4.測試
http://172.25.6.10/example.php##通路網站并多次重新整理
http://172.25.6.10/memcache.php##檢視命中率
<a href="http://s5.51cto.com/wyfs02/M02/87/5B/wKiom1feHw_T1_49AABJ6N0Rd3k727.png" target="_blank"></a>
<a href="http://s1.51cto.com/wyfs02/M02/87/58/wKioL1feHxCxiodGAAA2_iwLyFE937.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M00/87/5B/wKiom1feHxGx3krkAAERJFXkOfM028.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M02/87/58/wKioL1feHxGx7X2AAAA3Zv1FvqQ300.png" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M00/87/58/wKioL1feHxLQLtRAAAC_Ei-MgW4823.png" target="_blank"></a>
<a href="http://s2.51cto.com/wyfs02/M01/87/5B/wKiom1feHxOyaL-jAAA2uxe4c80661.png" target="_blank"></a>
本文轉自willis_sun 51CTO部落格,原文連結:http://blog.51cto.com/willis/1853588,如需轉載請自行聯系原作者