天天看點

linux 下安裝memcache

一、(以下指令全部手打,複制會有空格的錯誤)

在根目錄下建立一個memcache目錄

将三個包放在memcache目錄下

Memcached用戶端

wget  http://memcached.org/files/memcached-1.4.20.tar.gz

Memcache擴充

wget  https://pecl.php.net/get/memcache-3.0.6.tgz

Libevent

wget   https://monkey.org/~provos/libevent-2.0.13-stable.tar.gz

二、

(1)安裝libevent (因為linux中memcached需要依賴libevent運作)

   tar -zxvf libevent-2.0.13-stable.tar.gz  

    cd libevent-2.0.13-stable  

    ./configure  --prefix=/usr/local/libevent

    make  

    make install 

(2)安裝Memcached 用戶端

    tar -zxvf memcached-1.4.20.tar.gz  

   cd memcached-1.4.20  

    ./configure --prefix=/usr/local/memcached  

   make  

   make install  

(3)安裝Memcache for PHP Module (安裝memcache擴充)

   tar -zxvf memcache-3.0.6.tgz  

   cd memcache-3.0.6  

   /phpstudy/server/php/bin/phpize  

    ./configure --enable-memcache --with-php-config=/phpstudy/server/php/bin/php-config --with-zlib-dir  #備注:/phpstudy/server/php/bin/php-config 這個路徑是linux中php所在的目錄檔案,不同的環境路徑可能不同

   make  

   make install  

      在運作 "make install" 的後,得到類似下列資訊:

Installing shared extensions:      /phpstudy/server/php/lib/php/extensions/no-debug-non-zts-20121212/   

         安裝完成後的memcache.so子產品 就 在上面 标紅 的路徑中

(4)修改php.ini檔案

找到如下代碼:

extension_dir = "./"  

                    去掉;号,修改為 make install 後得到的目錄。

extension_dir = "/phpstudy/server/php/lib/php/extensions/no-debug-non-zts-20121212/"  

增加 extension 屬性,設定為

extension = memcache.so  

(5)将libevent 檔案拷貝到 /usr/lib 中(不然無法啟動memcached)

cp/usr/local/libevent/lib/libevent-2.0.so.5  /usr/lib

(6)手動開啟memcached

/usr/local/memcached/bin/memcached-d -u root -m 256 -p 11211 -l localhost

-d選項是啟動一個守護程序,

-m是配置設定給Memcache使用的記憶體數量,機關是MB,我這裡是256MB,

-u是運作Memcache的使用者,我這裡是root,

-l是監聽的伺服器IP位址,如果有多個位址的話,我這裡指定了伺服器的IP位址localhost,

-p是設定Memcache監聽的端口,我這裡設定了11211,最好是1024以上的端口,

-c選項是最大運作的并發連接配接數,預設是1024,按照你伺服器的負載量來設定,

-P是設定儲存Memcache的pid檔案,

(7)重新開機伺服器 phpstudy restart (其他環境自行百度如何重新開機)

(8)測試memcache安裝是否成功

1.      輸出phpinfo();檢視memcache擴充是否開啟

2.      輸出如下代碼測試memcache是否可以正常使用:

<?php  

    $mem = new Memcache;  

    $mem->connect("127.0.0.1", 11211);  

    $mem->set('key', 'hello memcache', 0, 60);  

    $val = $mem->get('key');  

    echo $val;  

?>  

(9)浏覽器裡能顯示 hello memcache就說明成功了;;

繼續閱讀