標籤:memcache php
# 我們開始裝memcache
tar -zxvf tar -zxvf libevent-1.4.13-stable.tar.gzcd libevent-1.4.13-stable ./configure --prefix=/usr make && make install # 查看 libevent 是否安裝完成 ls -al /usr/lib | grep libevent tar -zxvf memcached-1.4.17.tar.gz cd memcached-1.4.17 ./configure --with-libevent=/usr make && make install # 查看memcache是否安裝完成 ls -al /usr/local/bin/mem* # 啟動memcache的服務端 #啟動Memcache的伺服器端: /usr/local/bin/memcached -d -m 200 -u www -p 11211 -c 256 -P /tmp/memcached.pid #(不指定ip)
#參數說明:
#-d選項是啟動一個守護進程,
#-m是分配給Memcache使用的記憶體數量,單位是MB,我這裡是10MB,
#-u是運行Memcache的使用者,我這裡是root,
#-l是監聽的伺服器IP地址,如果有多個地址的話,我這裡指定了伺服器的IP地址202.207.177.177,
#-p是設定Memcache監聽的連接埠,我這裡設定了11211,最好是1024以上的連接埠,
#-c選項是最大啟動並執行並發串連數,預設是1024,我這裡設定了256,按照你伺服器的負載量來設定,
#-P是設定儲存Memcache的pid檔案,我這裡是儲存在 /tmp/memcached.pid,
#2.如果要結束Memcache進程,執行:kill `cat /tmp/memcached.pid`
# 添加防火牆規則
# -I 在前面添加規則
# drop目標連接埠11211連接埠的所有資料包iptables -I INPUT -p tcp --dport 11211 -j DROP# 單獨接受特定的ip的當前連接埠的資料包iptables -I INPUT -s 192.168.1.112 -p tcp --dport 11211 -j ACCEPT
# 查看啟動的連接埠號碼
netstat -tanp # 查看所有使用者開啟的連接埠
netstat -tunp # 查看目前使用者開啟的連接埠
# 安裝php-memcache的擴充
tar -zxvf memcache-2.2.4.tgz
cd memcache-2.2.4
phpize # 如果沒有找到phpize命令則 yum -y install php-devel
./configure --with-php-config=/usr/bin/php-config --enable-memcache # error: memcache support requires ZLIB. Use --with-zlib-dir=<DIR> to specify prefix where ZLIB include and library are located 這個錯誤就要執行:yum -y install zlib-devel
make
make test
## 查看是否可以在擴充目錄下找到 memcache.so
ls /usr/lib64/php/modules/ # 如果找不到則直接複製一個過去 cp modules/memcache.so /usr/lib64/php/modules/
### 如果可以找到則重新載入php設定檔進行測試
service httpd reload
### 測試memcache的功能是否正常
# 建立index.php檔案測試看php是否支援memcache
<?phpini_set(‘display_errors‘,1);error_reporting(7);$mem = new Memcache;$mem->connect(‘192.168.1.112‘,11211);$mem->set(‘test‘,‘Hello world!‘,0,12);$val = $mem->get(‘test‘);echo $val;var_dump($val);
# 如果頁面中輸出Hello world!string(12) "Hello world!" 則說明此時php已經支援memcache模組
## /etc/rc.d/rc.local 中加入memcache的啟動命令確保memcache在開機時自動啟動vi /etc/rc.d/rc.local # memcache 啟動 /usr/local/bin/memcached -d -m 200 -u www -p 11211 -c 256 -P /tmp/memcached.pid
memcached(源碼包)安裝,php(rpm包)-memcache的擴充安裝