centos 7安裝Memcache及其php拓展


Linux下配置使用memcache功能,主要包括:安裝libevent函數庫、安裝memcached、配置PHP擴展。

一、安裝libevent函數庫

libevent各版本地址:http://libevent.org/old-releases.html

在此我安裝當前最穩定版本:libevent-2.0.21

1.下載並安裝

[root@centos-7 ~]# wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
[root@centos-7 ~]# tar -zxvf libevent-2.0.21-stable.tar.gz
[root@centos-7 ~]# cd libevent-2.0.21-stable
[root@centos-7 libevent-2.0.21-stable]# ./configure --prefix=/usr/local/libevent
[root@centos-7 libevent-2.0.21-stable]# make && make install

(我安裝到了/usr/local/libevent下)

2.檢查是否安裝成功:

# ls -al /usr/local/libevent/lib | grep libevent

 

二、安裝memcached

memcached各版本下載地址:https://github.com/memcached/memcached/wiki/ReleaseNotes

在此我安裝當前最穩定版本: memcache-1.5.0

1.下載安裝:

# wget http://www.memcached.org/files/memcached-1.5.0.tar.gz
# tar -zxvf memcached-1.5.0.tar.gz
# cd memcached-1.5.0
# ./configure --prefix=/usr/local/memcache --with-libevent=/usr/local/libevent/
# make && make install

2.啟動服務器:

# /usr/local/memcache/bin/memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid

查看是否正常運行:

# netstat -tunpl | grep 11211
tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      10247/memcached
udp        0      0 127.0.0.1:11211         0.0.0.0:*                           10247/memcached

 

php的memcache和memcached拓展區別

memcache的文檔在:http://pecl.php.net/package/memcache

memcached的文檔在:http://pecl.php.net/package/memcached

首先看下時間,memcache最早是在2004年2月開發的,最后更新是在2013年4月,而memcached最早是在2009年1月開發的,最后更新是在2014年1月更新的。所以memcache的歷史比memcached早。

在安裝memcache擴展的時候並不要求安裝其他東東,但是在安裝memcached的時候會要求你安裝libmemcached,問題來了,libmemcached是memcache的C客戶端,它具有的優點是低內存,線程安全等特點。比如新浪微博之前就全面將PHP的memcache替換成php的memcached,在高並發下,穩定性果斷提高。

memcache的方法特別少,比如getMulti,setMulti都是沒有的,基本就剩下最簡單的get和set了。所以說“memcached比memcache支持更多的memcache協議”。

然后memcached直接配置了session支持,只要稍微修改下配置文件就可以把session存儲在memcache中了。

為什么要裝memcached擴展

memcached的1.2.4及以上增加了CAS(Check and Set)協議,對於同一key的多進行程的並發處理問題。這種情況其實根數據庫很像,如果同時有幾個進程對同一個表的同一數據進行更新的話,那會不會打架呢,哈哈。數據庫里面可以鎖定整張表,也可以鎖定表里面一 行的功能,其實memcached加入的CAS根這個差不多。

php的擴展memcache,不支持cas,memcached擴展是基於libmemcached,所以要先安裝libmemcached

 

三、添加php-memcache拓展

各版本下載地址:https://pecl.php.net/package/memcache

在此我用最穩定版本: memcache-2.2.7.tgz

1.步驟如下:

# wget https://pecl.php.net/get/memcache-2.2.7.tgz
# tar -zxvf memcache-2.2.7.tgz
# cd memcache-2.2.7
# /usr/bin/phpize
# ./configure --with-php-config=/usr/bin/php-config

如果出現: error: memcache support requires ZLIB. 因為缺少zlib的支持,安裝一下zlib重新啟動就好了:

# yum install -y zlib.x86_64 zlib-devel.x86_64

2.編譯安裝:

# make && make install

 

四、安裝libmemcached庫及php-memcached擴展包

 各版本libmemcached下載地址:https://launchpad.net/libmemcached/+download

1.步驟如下:

wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz 
tar -zxvf libmemcached-1.0.16.tar.gz 
cd libmemcached-1.0.16 
./configure -prefix=/usr/local/libmemcached -with-memcached 
make && make install

2.下載memcached擴展包並安裝

各版本memcached拓展包下載地址:https://pecl.php.net/package/memcached

wget http://pecl.php.net/get/memcached-2.2.0.tgz
tar -zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
/usr/bin/phpize --with-php-config=/usr/bin/php-config
./configure --with-php-config=/usr/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
make && make install

安裝成功:

修改/etc/php.ini,添加下面的擴展

extension=memcached.so

通過yum安裝的php則:

echo 'extension=memcached.so' > /etc/php.d/memcached.ini

重啟服務器

service httpd restart 
//centos 7
systemctl restart httpd.service

檢查是否成功加載php-memcached拓展:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM