Linux下安裝Memcache服務器端
服務器端主要是安裝memcache服務器端,目前的最新版本是memcached-1.3.0 。
下載:http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
另外,Memcache用到了libevent這個庫用于Socket的處理,所以還需要安裝libevent,libevent的最新版本是libevent-1.3。
用wget指令直接下載這兩個東西.下載回源文件后。
1.先安裝libevent。這個東西在配置時需要指定一個安裝路徑,即./configure –prefix=/usr;然后make;然后make install;
2.再安裝memcached,只是需要在配置時需要指定libevent的安裝路徑即./configure –with-libevent=/usr;然后make;然后make install;
這樣就完成了Linux下Memcache服務器端的安裝。詳細的方法如下:
1.分別把memcached和libevent下載回來,放到 /tmp 目錄下:
# cd /tmp
# wget http://www.monkey.org/~provos/libevent-1.3.tar.gz
# wget http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
2.先安裝libevent:
# tar zxvf libevent-1.3.tar.gz
# cd libevent-1.3
# ./configure -prefix=/usr
# make
# make install
3.測試libevent是否安裝成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 Jan 24 01:38 libevent-1.1a.so.1 ->libevent-1.1a.so.1.0.2
-rwxr-xr-x 1 rootroot 31596 Jan 7 2007libevent-1.1a.so.1.0.2
lrwxrwxrwx 1 rootroot 21 Jan 24 2013 libevent-1.3.so.1 ->libevent-1.3.so.1.0.3
-rwxr-xr-x 1 rootroot 260076 Jan 24 2013 libevent-1.3.so.1.0.3
-rw-r--r-- 1 rootroot 331144 Jan 24 2013 libevent.a
-rwxr-xr-x 1 rootroot 805 Jan 24 2013 libevent.la
lrwxrwxrwx 1 rootroot 21 Jan 24 2013 libevent.so -> libevent-1.3.so.1.0.3
還不錯,都安裝上了。
4.安裝memcached,同時需要安裝中指定libevent的安裝位置:
# cd /tmp
# tar zxvf memcached-1.2.5.tar.gz
# cd memcached-1.2.5
# ./configure -with-libevent=/usr
# make
# make install
如果中間出現報錯,請仔細檢查錯誤信息,按照錯誤信息來配置或者增加相應的庫或者路徑。
安裝完成后會把memcached放到/usr/local/bin/memcached ,
5.測試是否成功安裝memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 124281 Jan 24 2013 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 132762 Jan 24 2013 /usr/local/bin/memcached-debug
安裝Memcache的PHP擴展
1.在http://pecl.php.net/package/memcache選擇相應想要下載的memcache版本。
2.安裝PHP的memcache擴展
tar vxzf memcache-2.2.1.tgz
cd memcache-2.2.1
/usr/bin/phpize
./configure -enable-memcache -with-php-config=/usr/bin/php-config-with-zlib-dir
make
make install
3.上述安裝完后會有類似這樣的提示:
Installing shared extensions: /usr/lib/php/modules/
4.把php.ini中的extension_dir = “./”修改為
extension_dir = "/usr/lib/php/modules"
5.添加一行來載入memcache擴展:extension=memcache.so
memcached的基本設置:
1.啟動Memcache的服務器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 11211 -c 256 -P/tmp/memcached.pid
-d選項是啟動一個守護進程,
-m是分配給Memcache使用的內存數量,單位是MB,我這里是10MB,
-u是運行Memcache的用戶,我這里是root,
-l是監聽的服務器IP地址,如果有多個地址的話,我這里指定了服務器的IP地址192.168.0.200,
-p是設置Memcache監聽的端口,我這里設置了12000,最好是1024以上的端口,
-c選項是最大運行的并發連接數,默認是1024,我這里設置了256,按照你服務器的負載量來設定,
-P是設置保存Memcache的pid文件,我這里是保存在 /tmp/memcached.pid,
2.如果要結束Memcache進程,執行:
# kill `cat /tmp/memcached.pid`
也可以啟動多個守護進程,不過端口不能重復。
3.重啟apache,servicehttpd restart
Memcache環境測試:
運行下面的php文件,如果有輸出This is a test!,就表示環境搭建成功。開始領略Memcache的魅力把!
< ?php
$mem = new Memcache;
$mem->connect(”127.0.0.1″, 11211);
$mem->set(’key’, ‘This is a test!’, 0, 60);
$val = $mem->get(’key’);
echo $val;
?>
-第二種安裝方法----------------------------
用yum來裝
在CentOS5下為PHP安裝memcache支持
--------------------------------------------------------------------------------
裝了CentOS5之后,由于有了yum這個非常有用的包管理器,我們基本不用親自手動一步步去configure,make,make install了。
只要簡單的運行yum命令就可以搞定,如果你還是需要自己手動編譯,請參閱文末所附參考文檔。
1、檢查安裝第三方軟件倉庫
標準的CentOS5軟件倉庫里面是沒有memcache相應的包的,不信你自己運行
yum search memcache
看看結果,是不是提示如下?
Loading “installonlyn” plugin
Setting up Install Process
Setting up repositories
Reading repository metadata in from local files
Parsing package install arguments
Nothing to do
所以,我們的第一步就是導入第三方軟件倉庫,這里推薦的是 Dag Wieers 庫(現在叫 RPMForge 了),安裝方法如下:
到安裝庫的網頁http://dag.wieers.com/rpm/packages/rpmforge-release/,找到合適的軟件包,事實上很簡單,我們是centos5.0,由于是和紅帽子企業版5.0同樣的源編譯出來的,那么要的就是rhel5.0的就可以,備選的就只有以下兩個
rpmforge-release-0.3.6-1.el5.rf.i386.rpm Fri 09 Mar 2007 16 kB Red Hat EL 5 -i386
rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm Fri 09 Mar 200716 kB Red Hat EL 5 - x86_64
我的是32位的系統,我就選擇上面那個地址下載,如果是64位的,選下面那個,呵呵。
[root@localhost src]# wget http://dag.wieers.com/rpm/packages/r...l5.rf.i386.rpm
[root@localhost src]# rpm -ivhrpmforge-release-0.3.6-1.el5.rf.i386.rpm
后面沒有其他提示,那差不多好了。我們就可以安裝我們的memcache相關的模塊了。
2、查找相關軟件包
在查找的時候會自動更新我們的版本庫索引的,這點從下面可以看出來。我們可以發現有兩個包要裝,一個是 memcache ,一個是php-pecl-memecache。還有個新發現,python 版本的 memcache 客戶端也有了,爽啊。
[root@localhost src]# yum search memcache
Loading “installonlyn” plugin
Setting up repositories
update 100% |=========================| 951 B 00:00
rpmforge 100% |=========================| 1.1 kB 00:00
base 100% |=========================| 1.1 kB 00:00
addons 100% |=========================| 951 B 00:00
extras 100% |=========================| 1.1 kB 00:00
Reading repository metadata in from local files
primary.xml.gz 100% |=========================| 1.1 MB02:07
rpmforge :################################################## 3907/3907
Added 3907 new packages, deleted 0 old in 13.20 seconds
primary.xml.gz 100% |=========================| 37 kB 00:00
extras : ##################################################145/145
Added 17 new packages, deleted 0 old in 0.16 seconds
php-pecl-memcache.x86_64 2.0.4-1.el5.rf rpmforge
Matched from:
php-pecl-memcache
PECL package to use the memcached distributed cachingsystem
Memcached is a caching daemon designed especially fordynamic web applications
to decrease database load by storing objects in memory.This extension allows
you to work with memcached through handy OO and proceduralinterfaces.
http://pecl.php.net/package/memcache
php-pecl-memcache.x86_64 2.1.2-1.el5.rf rpmforge
Matched from:
php-pecl-memcache
PECL package to use the memcached distributed cachingsystem
Memcached is a caching daemon designed especially fordynamic web applications
to decrease database load by storing objects in memory.This extension allows
you to work with memcached through handy OO and proceduralinterfaces.
http://pecl.php.net/package/memcache
python-memcached.noarch 1.31-1.el5.rf rpmforge
Matched from:
python-memcached
Python interface to the memcached memory cache daemon
Python interface to the memcached memory cache daemon.
ftp://ftp.tummy.com/pub/python-memcached/
memcached.x86_64 1.1.13-4.el5.rf rpmforge
Matched from:
memcached
memcached is a high-performance, distributed memory objectcaching system,
generic in nature, but intended for use in speeding updynamic web
applications by alleviating database load.
http://www.danga.com/memcached/
memcached.x86_64 1.2.1-3.el5.rf rpmforge
Matched from:
memcached
memcached is a high-performance, distributed memory objectcaching system,
generic in nature, but intended for use in speeding updynamic web
applications by alleviating database load.
http://www.danga.com/memcached/
memcached.x86_64 1.1.13-5.el5.rf rpmforge
Matched from:
memcached
memcached is a high-performance, distributed memory objectcaching system,
generic in nature, but intended for use in speeding updynamic web
applications by alleviating database load.
http://www.danga.com/memcached/
memcached.x86_64 1.2.1-4.el5.rf rpmforge
Matched from:
memcached
memcached is a high-performance, distributed memory objectcaching system,
generic in nature, but intended for use in speeding updynamic web
applications by alleviating database load.
http://www.danga.com/memcached/
memcached.x86_64 1.2.2-1.el5.rf rpmforge
Matched from:
memcached
memcached is a high-performance, distributed memory objectcaching system,
generic in nature, but intended for use in speeding updynamic web
applications by alleviating database load.
http://www.danga.com/memcached/
python-openid.noarch 1.2.0-1.el5.rf rpmforge
Matched from:
The OpenID library with batteries included.
The library features:
refined and easy-to-use API,
extensive documentation,
many storage implemetations including file-based, SQL, andmemcached,
simple examples to help you get started and
licensed under the LGPL.
3、安裝并驗證
[root@localhost src]# yum install –enablerepo=rpmforge memcachedphp-pecl-memcache
[root@localhost src]# memcached -h
memcached 1.2.2
[root@localhost src]# php -m|grep memcache
memcache