memcached服務器IP地址: 192.168.218.5
系統: CentOS7
源碼包下載鏈接:鏈接:https://pan.baidu.com/s/17ZJvfKeX67zwKRYmYoFxjA
提取碼:jfgr
安裝環境依賴
yum -y install gcc gcc-c++ make
安裝libevent
[root@localhost ~]# tar xf libevent-2.1.8-stable.tar.gz
[root@localhost ~]# cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]#
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@localhost libevent-2.1.8-stable]# make && make install
安裝memcached
[root@localhost libevent-2.1.8-stable]# cd ..
[root@localhost ~]# tar -xf memcached-1.5.6.tar.gz
[root@localhost ~]# cd memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@localhost memcached-1.5.6]# make && make install
創建軟鏈接&&開啓memcached
[root@localhost ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
[root@localhost ~]# memcached -d -m 32m -p 11211 -u root
11211是memcached的默認端口,故下面檢查11211端口是否開啓
[root@localhost ~]# netstat -antp| grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 10040/memcached
tcp6 0 0 :::11211 :::* LISTEN 10040/memcached
連接本地memcached進行測試
[root@localhost ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
出現上面的輸出,說明連接成功,使用quit退出
安裝memcached客戶端,客戶端使用LAMP架構通過php訪問memcached
故需先搭建LAMP環境,可參考我的博客:https://blog.csdn.net/weixin_43515220/article/details/103786092
安裝autoconf
yum -y install autoconf
解壓memcache客戶端包
[root@localhost ~]# tar xf memcache-2.2.7.tgz
[root@localhost ~]# cd memcache-2.2.7/
進入目錄,可以發現並每有configure文件,我們通過php來生成configure文件
[root@localhost memcache-2.2.7]# /usr/local/php5/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
phpize的路徑需要根據實際php安裝的目錄來指定,如果存在軟鏈接或者環境變量能找到該命令,則不需要指定命令的絕對路徑
下面編譯安裝
[root@localhost memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config
[root@localhost memcache-2.2.7]# make && make install
查找memcache.so,查看是否存在/usr/local/php5/lib/php/extensions/no-debug-zts-20131226
[root@localhost ~]# cd /usr/local/php5/lib/php/extensions/no-debug-zts-20131226/
[root@localhost no-debug-zts-20131226]# ls
memcache.so opcache.so
若存在,則修改php.ini文件,添加memcache擴展,php.ini通常在php的安裝目錄下
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20131226"
extension = memcache.so
添加測試php頁面,將下面頁面放到httpd的文件夾下,這裏將LAMP中測試php頁面index.php中的內容直接替換
<?php
$memcache = new Memcache();
$memcache->connect('192.168.218.5',11211);
$memcache->set('key','Memcache test successful!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
注意,將php頁面中的memcached服務器的IP地址根據實際情況進行替換
重啓httpd服務
systemctl restart httpd
使用瀏覽器訪問客戶端
http://192.168.218.4/index.php
測試成功!