Memcached 是一个高性能分布式的内存对象缓存系统, 通常被用于降低数据库加载压力以提高动态 web 应用的响应速度。
为Wordpress网站加速,这个是重要的优化手段。
1. 安装zlib
1.1介绍
源码所在网站:https://packages.debian.org/source/bullseye/zlib
1.2安装
wget http://deb.debian.org/debian/pool/main/z/zlib/zlib_1.2.11.dfsg.orig.tar.gz
mkdir /usr/share/zlib
tar xzvf zlib_1.2.11.dfsg.orig.tar.gz
cd zlib-1.2.11.dfsg/
./configure --prefix=/usr/share/zlib
make && make install
1.3添加系统变量
echo 'export PATH="$PATH:/usr/share/zlib"' >>/etc/profile
echo 'export PATH="$PATH:/usr/share/zlib/bin"' >>/etc/profile
source /etc/profile
2. 安装Memcached
2.1概述
2.2安装服务端
根据系统使用yum或apt:
yum -y install memcached
apt -y install memcached
启动memcached:
systemctl start memcached
设置开机启动:
systemctl enable memcached
2.3安装客户端
2.3.1安装libmemcached库
由于memcached是基于libmemcached开发的因此要先安装libmemcached。
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar xzvf libmemcached-1.0.18
cd libmemcached-1.0.18
./configure --with-memcached=/usr/local/memcached --prefix=/usr/local/libmemcached
make && make install
2.3.2安装php-fpm的memcached扩展
官网:http://pecl.php.net/package/memcached
wget http://pecl.php.net/get/memcached-3.3.0.tgz
tar zxvf memcached-3.3.0.tgz
cd memcached-3.3.0
phpize
./configure --with-libmemcached-dir=/usr/local/libmemcached --with-zlib-dir=/usr/share/zlib --disable-memcached-sasl
make && make install
完成后显示类似字样:Installing shared extensions: /usr/lib/php/20220829/
编辑PHP的配置文件,例如/etc/php/8.2/fpm/php.ini,添加以下内容:
extension_dir = "/usr/lib/php/20220829/"
extension = "memcached.so"
最后重启nginx和php-fpm
kill -USR2 `cat /run/php/php8.2-fpm.pid`
systemctl restart nginx
查看是否已经加载memcached模块: