mac下php7.1安装memcached

准备工作

  1. 安装 libevent
    brew install libevent
  2. 安装 libmemcached 客户端库
    brew install libmemcached
  3. 下载 php memached 源码
    wget http://pecl.php.net/get/memcached-3.1.3.tgz

安装模块

解压后,进入memcached-3.1.3 目录下,执行以下命令:

phpize
./configure --prefix=/usr/bin/php-config
make
make install

修改PHP配置 php.ini

通过命令php -i | grep ini 可以找到php.ini的文件位置,
然后开启memcache模块。
extension=memcached.so

测试

  1. 通过命令php -m | grep mem 就可以看到是否安装成功了。

重启

php-fpm restart 重启后, web才能真正用到memcache. 你可以搞个测试文件,phpinfo() 看看是否生效。

by the way.
我本地测试时,有时候刷缓存就是重启memcache

#!/usr/bin/env bash
ps aux | grep mem | awk '{print $2}' | xargs kill -9
memcached -d

说明

我一开始使用系统自带的php7.1, 在执行phpize时报错了。

grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:        
Zend Module Api No:     
Zend Extension Api No:  

解决方法是让我另一个软链接。但是又需要操作/usr目录,又引发了rootless权限的问题。

参考 https://www.cnblogs.com/imayanlong/p/6071805.html
http://www.mayanlong.com/archives/2016/348.html

以上版本安装的memcached是新版本的。老版本的还是memcache, 如果你发现你代码报错了。
Fatal error: Uncaught Error: Class 'Memcache' not found
请安装老版本的memcache。
和上面步骤其实就是一样的。
1. 下载wget https://github.com/websupport-sk/pecl-memcache/archive/php7.zip
2. 安装同上
3. 配置同上 extension=memcache.so
4. 重启php-fpm同上。