跳至正文

WordPress Memcached

为 WordPress 服务器添加 Memcached 支持

转载自广联在线微信公众号。请扫描本页底部二维码关注和查看原文。 在 WordPress 中开启 Memcached 缓存支持,可以加快网站访问速度。具体原理和配置请参考 WordPress 文档。本文着重介绍下在 Docker 方式部署的 WordPress 5.7 服务器上安装 Memcached 的方法步骤。 Docker 容器的 Linux 环境中包含一套 dpkg 和 apt 的包管理工具,所以如果在运维 Docker 部署的业务时临时需要用到什么软件,一般情况可以跟 Ubuntu 一样办法,使用 apt install 命令来安排常用软件。 先准备好一个 Docker 方式部署的 WordPress 网站实例。 进入 docker 容器的运行环境。 [root@ip-172-31-35-228 centos]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4ce6a0bf360d wordpress:latest "docker-entrypoint.s…" 5 days ago Up 5 days 0.0.0.0:8010->80/tcp, :::8010->80/tcp ubuylogi_wordpress_1 b6bd40497f6f mysql:5.7 "docker-entrypoint.s…" 5 days ago Up 6 minutes 3306/tcp, 33060/tcp ubuylogi_ubuy_db_1 [root@ip-172-31-35-228 centos]# docker exec -it 4ce6a0bf360d /bin/bash root@4ce6a0bf360d:/var/www/html# cd root@4ce6a0bf360d:~# vim bash: vim: command not found         可以看到,vim 等一些常用的工具并不存在 直接用 apt-get update 更新源。 root@4ce6a0bf360d:~# apt update Get:1 http://security.debian.org/debian-security bullseye-security InRelease [44.1 kB] Get:2 http://deb.debian.org/debian bullseye InRelease [113 kB] Get:3 http://deb.debian.org/debian bullseye-updates InRelease [36.8 kB] Get:4 http://security.debian.org/debian-security bullseye-security/main amd64 Packages [29.4 kB] Get:5 http://deb.debian.org/debian bullseye/main amd64 Packages [8178 kB] Fetched 8401 kB in 2s (5057 kB/s) Reading package lists... Done Building dependency tree... Done Reading state information... Done 1 package can be upgraded. Run 'apt list --upgradable' to see it. root@4ce6a0bf360d:~# 先安装等下要用到的一些工具软件。apt install -y vim wget root@4ce6a0bf360d:~# apt install -y vim wget Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libgpm2 vim-common vim-runtime xxd …… 同样方法安装 zlib1g-dev 和 memcached root@4ce6a0bf360d:~# apt install -y zlib1g-dev memcached Reading package lists... Done Building dependency tree... Done Reading state information... Done …… 安装 memcached 后别忘了启动服务。 root@4ce6a0bf360d:~# /etc/init.d/memcached start Starting memcached: memcached. root@4ce6a0bf360d:~# 下载 libmemcached 并编译和安装(以下省略各命令的输出)。 root@4ce6a0bf360d:~# cd root@4ce6a0bf360d:~# wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz root@4ce6a0bf360d:~# tar -zxvf libmemcached-1.0.18.tar.gz root@4ce6a0bf360d:~# cd libmemcached-1.0.18 root@4ce6a0bf360d:~/libmemcached-1.0.18# ./configure --prefix=/usr/local/libmemcached --with-memcached root@4ce6a0bf360d:~/libmemcached-1.0.18# make && make install ……         这一版 libmemcached 在我的环境中编译会出错,如下: CXX clients/memflush.o clients/memflush.cc: In function 'int main(int, char**)': clients/memflush.cc:42:19: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] 42 | if (opt_servers == false) | ~~~~~~~~~~~~^~~~~~~~ clients/memflush.cc:51:21: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] 51 | if (opt_servers == false) | ~~~~~~~~~~~~^~~~~~~~ make[1]: *** [Makefile:5832: clients/memflush.o] Error 1 make[1]: Leaving directory '/root/libmemcached-1.0.18' make: *** [Makefile:3700: all] Error 2 root@4ce6a0bf360d:~/libmemcached-1.0.18#         原因是 clients/memflush.cc 源文件中有两处指针类型变量与布尔型的对比操作,用vim打开源文件把false改为 NULL 再重新 make 即可。 接下来为 php 安装 memcached 插件 root@4ce6a0bf360d:~/libmemcached-1.0.18# pecl install memcached pecl/memcached can optionally use PHP extension "igbinary" (version >= 2.0) pecl/memcached can optionally use PHP extension "msgpack" (version >= 2.0) downloading memcached-3.1.5.tgz ... Starting to download memcached-3.1.5.tgz (83,015 bytes) ....................done: 83,015 bytes 15 source files, building running: phpize Configuring for: PHP Api Version: 20190902 Zend Module Api No: 20190902 Zend Extension Api No: 320190902 libmemcached directory [no] : /usr/local/libmemcached zlib directory [no] : use system fastlz [no] : enable igbinary serializer [no] : enable msgpack serializer [no] : enable json serializer [no] : enable server protocol [no] : enable sasl [yes] : no enable sessions [yes] : building in /tmp/pear/temp/pear-build-defaultuser2i8dVo/memcached-3.1.5 …… Build process completed successfully Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20190902/memcached.so' install ok: channel://pecl.php.net/memcached-3.1.5 configuration option "php_ini" is not set to php.ini location You should add "extension=memcached.so" to php.ini root@4ce6a0bf360d:~/libmemcached-1.0.18#     安装过程中,libmemcached 头文件路径填入上面 configure libmemcached 时指定的路径,zlib 头文件路径保持 no (pecl 自动检测),下面除了 sasl 改为 no 之外,其它都按默认。 libmemcached 安装完成后,需要从上一步输出的最后几行找到memcached so库的安装路径,填入 php 配置文件中。 root@4ce6a0bf360d:~# vim /usr/local/etc/php/php.ini [Memcached] extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20190902/" extension = memcached.so 接下来可以确认一下 memcached 模块已经被 php 加载。(可能需要重启 php 服务) root@bd570ad1c1a5:~/libmemcached-1.0.18# php -m | grep mem memcached 最后重启 Docker 容器验证。如果以上步骤都执行成功,WordPress 的 object-cache.php 和 wp-config.php 文件也做了相应正确的设定,就可以在网站后台看到 Memcached 的缓存运行情况——包括命中率等数据的显示了。

点我,点我
QQ
  • 售前咨询 点击这里给我发消息
邮箱
  • Email
关注微信
  • 扫码关注公众号