PHP Memcache 扩展组件的安装

| 选择喜欢的代码风格  

memcache官网 http://memcached.org


目前最新版为:v1.6.13 (2022-1-12),memcache 源码编译安装,可能需要 libevent 支持。

# memcache 官网下载 memcache 源码包
wget http://memcached.org/latest

#解压
tar -zxvf memcached-1.x.x.tar.gz

cd memcached-1.x.x


#编译 memcache 参数
./configure --prefix=/Data/apps/memcache --with-libevent=/usr

...

make && make test && sudo make install

#运行memcached
/usr/local/memcached/bin/memcached -d -m 128 -l localhost -p 11211 -u root

 -d 以守护程序(daemon)方式运行 memcached
 -m 设置 memcached 可以使用的内存大小,单位为 M
 -l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数
 -p 设置监听的端口,默认为 11211,所以也可以不设置此参数
 -u 指定用户

PHP 7.x 安装 Memcache 扩展


PECL 官网下载 memcache 扩展(目前最新支持 PHP 8):https://pecl.php.net/package/memcache4.0.5.2 为 PHP 7 支持的 Memcache 扩展:

wget https://pecl.php.net/get/memcache-4.0.5.2.tgz

tar -xvf memcache-4.0.5.2.tgz
cd memcache-4.0.5.2/

$ phpize

Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.


$ ./configure --with-php-config=/Data/apps/php7.3.33/bin/php-config  # 之前 PHP 7.3.X 的自定义安装路径

make && make install

# 最后系统提示路径:
Installing shared extensions:     
/Data/apps/php7.3.33/lib/php/extensions/no-debug-non-zts-20180731/

# 编辑 php.ini 文件
[memcache]
extension_dir = "/Data/apps/php7.3.33/lib/php/extensions/no-debug-non-zts-20180731/"
extension = memcache.so

# 测试是否生效
$ /etc/init.d/php-fpm configtest

[26-Jan-2022 12:34:27] NOTICE: configuration file /Data/apps/php7.3.33/etc/php-fpm.conf test is successful


# 然后重启 php-fpm
$ /etc/init.d/php-fpm restart

#查看 phpinfo 后可以看到 memcache 就证明已经安装成功了
#或者 php -m 命令,查看 PHP 7.X 加载的扩展也可以

PHP查看扩展的方法


除了在php文件里,调用 phpinfo() 之外,用命令行执行 php -m

[root@Dev_Test ~]$php -m
[PHP Modules]
apc
bcmath
calendar
Core
ctype
curl
date
dom
ereg
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
mcrypt
memcache
memcached
mhash
mysql
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
protocolbuffers
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
ssh2
standard
sysvsem
tokenizer
xml
xmlreader
xmlrpc
xmlwriter
zip
zlib

[Zend Modules]

Linux 下执行命令 php --ri memcache 查看组件详细配置信息

$php --ri memcache

memcache

memcache support => enabled
Version => 4.0.5.2
Revision => $Revision$

Directive => Local Value => Master Value
memcache.allow_failover => 1 => 1
memcache.max_failover_attempts => 20 => 20
memcache.default_port => 11211 => 11211
memcache.chunk_size => 32768 => 32768
memcache.protocol => ascii => ascii
memcache.hash_strategy => consistent => consistent
memcache.hash_function => crc32 => crc32
memcache.redundancy => 1 => 1
memcache.session_redundancy => 2 => 2
memcache.compress_threshold => 20000 => 20000
memcache.lock_timeout => 15 => 15
memcache.session_prefix_host_key => 0 => 0
memcache.session_prefix_host_key_remove_www => 1 => 1
memcache.session_prefix_host_key_remove_subdomain => 0 => 0
memcache.session_prefix_static_key => no value => no value
memcache.session_save_path => no value => no value
memcache.prefix_host_key => 0 => 0
memcache.prefix_host_key_remove_www => 1 => 1
memcache.prefix_host_key_remove_subdomain => 0 => 0

memcache 扩展安装的坑 (1) - libevent:


如果是 PHP 7.x,可能需要 libevent ,则需要到 libevent 官网:​http://libevent.org​​ 编译安装,目前最新版是 libevent-2.1.12:

# 编译安装 libevent
./configure --prefix=/usr --disable-static && make

如果你的系统还未编译 libmemcached,则下载编译它:https://launchpad.net/libmemcached/+download

memcache 扩展安装的坑 (2) - autoconf:


在使用 phpize 的时候,提示:Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script,在 CentOS 下执行:

yum install autoconf      # CentOS

# 其他:
zypper install autoconf   # openSuSE
apt-get install autoconf  # Ubuntu
dnf install autoconf      # fedora 24-27

最终加入 extension=memcached.so 即可,再执行 php -m 查看是否生效。



发表评论