一、下载文件
1、memcach
下载:http://memcached.org/files/memcached-1.6.9.tar.gz
2、libevent
下载:https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
二、安装libevent
# tar -zxvf libevent-2.1.12-stable.tar.gz
# cd libevent-2.1.12-stable
# ./configure --prefix=/usr/local/libevent
# make
# make install
三、安装memcache
# tar -zxvf memcached-1.6.9.tar.gz
# cd memcached-1.6.9
# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
# make
# make install
四、启动服务
# ps -ef | grep memcached
root 25099 14654 0 15:47 pts/2 00:00:00 grep --color=auto memcached
# /usr/local/memcached/bin/memcached -u root -d
# ps -ef | grep memcached
root 25101 1 0 15:47 ? 00:00:00 /usr/local/memcached/bin/memcached -u root -d
root 25112 14654 0 15:47 pts/2 00:00:00 grep --color=auto memcached
五、停止服务
首先需要查一下memcached进程的pid,查询命令如下:
# pgrep memcached
25101
然后再根据pid来kill这个进程即可(-9表示强制杀死),命令如下:
# kill -9 25101
# ps -ef | grep memcached
root 25147 14654 0 15:49 pts/2 00:00:00 grep --color=auto memcached
六、配置开机自启动
在rc.local增加启动代码:
# vi /etc/rc.d/rc.local
底部增加一行:
/usr/local/memcached/bin/memcached -u daemon -d -m 2048 -p 11211 -P /tmp/memcached.pid
![](https://img.haomeiwen.com/i2781902/1f9d8d6fc0634c72.png)
设置可执行权限:
# chmod755/etc/rc.local
八、配置PHP扩展
1、下载
# wget http://pecl.php.net/get/memcache-8.0.tgz
2、安装m4
# yum install m4 autoconf
3、安装memcache
# tar -zxvf memcache-8.0.tgz
# cd memcache-8.0
# /usr/local/php7_2/bin/phpize
# ./configure --with-php-config=/usr/local/php7_2/bin/php-config
# make
# make install
4、配置PHP扩展
# cd /usr/local/php7_2/lib
# vi php.ini
添加extension路径与扩展:
extension_dir = "/usr/local/php7_2/lib/php/extensions/no-debug-non-zts-20170718/"
extension=memcache.so
5、重启nginx
# /usr/local/nginx/sbin/nginx -s reload
九、检测
<?php
ini_set('display_errors', 'on');
$mem = new Memcache();
$mem->addServer("localhost", 11211);
$mem->set('key', 'This is a test2211!', 0, 60);
$val = $mem->get('key');
echo $val;
设置为服务
# cp /usr/softwares/memcached-1.6.9/scripts/memcached.sysv /etc/init.d/memcached
# chmod 755 /etc/init.d/memcached
# chkconfig --add memcached
# chkconfig memcached on
网友评论