Mac下安装Memcache

我是使用 brew 来安装的,让我们再回顾一下整个过程吧。如果你没有装 brew ,先看步骤一,否则直接看步骤二。
步骤一:安装 Homebrew
先看看是否满足下面条件:

Intel 的 CPU
OS X 10.5 或者更高
安装了XCode 或者 XCode命令行工具

满足了,就可以安装 Homebrew,命令如下:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

步骤二:安装 memcached
安装前,可以先查找一下,看看有没有:

$ brew search memcache

返回结果:

libmemcached    memcache-top    memcached   memcacheq

说明和关键字memcache相关的有上面这四个,这样就确认了,有我们需要的东西,第一个是客户端,第三个是服务器。

那么安装吧!

先装服务器:

$ brew install memcached

安装日志:

==> Installing memcached dependency: libevent
==> Downloading https://github.com/downloads/libevent/libevent/libevent-2.0.21-s
######################################################################## 100.0%
==> ./configure --disable-debug-mode --prefix=/usr/local/Cellar/libevent/2.0.21
==> make
==> make install
/usr/local/Cellar/libevent/2.0.21: 48 files, 1.8M, built in 84 seconds
==> Installing memcached
==> Downloading http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/memcached/1.4.15 --disable-coverage
==> make install
==> Caveats
To have launchd start memcached at login:
    ln -sfv /usr/local/opt/memcached/*.plist ~/Library/LaunchAgents
Then to load memcached now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
Or, if you don't want/need launchctl, you can just run:
    /usr/local/opt/memcached/bin/memcached
==> Summary
/usr/local/Cellar/memcached/1.4.15: 10 files, 176K, built in 8 seconds

从上面安装日志,可以看出:

1.安装 memcached 前,先安装了其所依赖的 libevent 库 memcache的依赖:openssl和libevent会自动下载并安装。
2.下载的libevent和memcached,被安装到/usr/local/Cellar下面,但是又自动在/usr/local/bin下面建立了软连接,方便使用。

安装后可以查看安装的结果:

$ which memcached
/usr/local/bin/memcached

$ memcached -h
memcached 1.4.15
...

步骤二:安装客户端
使用php操作memcache前,需要安装php的扩展,
php扩展有两种,一个是memcache,一个是memcached,下载地址为:
memcache:http://pecl.php.net/package/memcache
memcached:http://pecl.php.net/package/memcached

方案一、安装memcache。从http://pecl.php.net/package/memcache选择一个版本下载源码压缩包,
解压
进入到源码目录后执行:(我选择的是2.2.7稳定版)

$ sudo phpize

phpize是帮助用来在已编译好的php外,编译php扩展用的脚本,用来生成configure、make等文件。
有时执行这个命令会报错:

Cannot find autoconf. Please check your autoconf installation and the  
$PHP_AUTOCONF environment variable. Then, rerun this script.  

缺少依赖,那就安装咯,还是使用brew:

$ sudo brew install autoconf  

phpize完成后,依次实行如下命令实现编译和安装:

$ sudo ./configure  
$ sudo make  
$ sudo make install  

编译好的memcache.so一般被安装到如下目录:

Installing shared extensions: /usr/local/Cellar/php56/5.6.32_8/lib/php/extensions/no-debug-non-zts-xxxxxx/
这样就可以在php.ini中配置这个扩展了:
找到php.ini

extension=/usr/lib/php/extensions/no-debug-non-zts-xxxxxx/memcache.so

20160722114523849.jpg
打开phpinfo()页面,查看memcache是否已经加载成功:
20160722114538745.jpg

方案二、安装memcached客户端库:
安装Libmemcached、php-memcached扩展
1、去Libmemcached官网下载最新安装包。如:libmemcached-1.0.18.tar.gz
2、去pecl官网下载最新php-memcached扩展包。如:memcached-2.2.0.tgz
3、解压并安装libmemcached-1.0.18.tar.gz

tar  xzvf  libmemcached-1.0.18.tar.gz
cd  libmemcached-1.0.18
./configure --prefix=/usr/lib/libmemcached --with-memcached
make
sudo make install

4、解压并安装memcached-2.2.0.tgz

tar  xzvf  memcached-2.2.0.tgz
cd  memcached-2.2.0
phpize
./configure
make
sudo make install

5、修改php.ini
6、重启apache,完成。

标签:memcache