为 WordPress安装配置Memcached

作者: 麻辣阁 分类: wordpress 发布时间: 2018-11-21 16:58

安装 memcached

安装,并设置开机启动:

yum install memcached
systemctl start memcached && systemctl enable memcached
Created symlink from /etc/systemd/system/multi-user.target.wants/memcached.service to /usr/lib/systemd/system/memcached.service.

安装 php扩展

编辑 /etc/php.ini, 添加

;启用 memcached
extension=memcached.so

安装扩展并重启:

yum install php72w-pecl-memcached
service nginx restart
service php-fpm restart

安装缓存插件到 wordpress

参考 https://wordpress.org/plugins/memcached/#installation,

  • wget https://downloads.wordpress.org/plugin/memcached.3.0.1.zip, 下载的可能是 memcache 的,在 memcached 下使用会出现 HTTP ERROR 500 错误
  • memcached 的 object-cache.php 可以在 GitHub 获取到如: https://github.com/tollmanz/wordpress-pecl-memcached-object-cache 这里下载, 不能直接 wget https://github.com/tollmanz/wordpress-pecl-memcached-object-cache/blob/master/object-cache.php 到wp-content 目录, 需要wget 对应的 zip文件.
  • Copy object-cache.php to wp-content,
  • chown nginx:nginx object-cache.php
  • 设置 wordpress 的 wp-config.php, 参考 wordpress-pecl-memcached-object-cache 添加如下内容
/** mecached 设置 */
global $memcached_servers;
$memcached_servers = array(
    array(
        '127.0.0.1', 
        11211        
    )
);

查看 Memcached 缓存情况

$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats
STAT pid 9593
STAT uptime 5592
STAT time 1542789426
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 0.108247
STAT rusage_system 0.126697
STAT curr_connections 10
STAT total_connections 26
STAT connection_structures 13
STAT reserved_fds 20
STAT cmd_get 446
STAT cmd_set 188
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 256
STAT get_misses 190
STAT delete_misses 4
STAT delete_hits 1
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 305043
STAT bytes_written 236717
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 250462
STAT curr_items 109
STAT total_items 122
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
END
  • uptime:memcached运行的秒数
  • curr_items:现在在缓存中的键值对个数
  • STAT cmd_get 446 总共获取数据的次数(等于 get_hits + get_misses )
  • STAT cmd_set 188 总共设置数据的次数
  • STAT get_hits 256 命中了多少次数据,也就是从 Memcached 缓存中成功获取数据的次数
  • STAT get_misses 190 没有命中的次数
  • STAT delete_misses 4
  • STAT delete_hits 1

如果发现页面可以打开,但是里面没有Hits数据,说明WordPress并没有成功连接到memcached.

更简单的输出统计信息的命令:

 memcached-tool 127.0.0.1 stats

查看php的 memcached 扩展信息

建立php文件写入类似如下内容,然后访问网页地址, 查找memcached:

<?php phpinfo(); ?>

参考:

配置Memcached禁止公网访问

当前服务器Memcached端口未授权漏洞,端口默认的11211允许任何地址可以访问,应该设置只允许本地访问才行。

$ netstat -anptu | grep memcached
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      9593/memcached
tcp6       0      0 :::11211                :::*                    LISTEN      9593/memcached
udp        0      0 0.0.0.0:11211           0.0.0.0:*                           9593/memcached
udp6       0      0 :::11211                :::*                                9593/memcached

配置Memcached禁止公网访问, vi /etc/sysconfig/memcached, 添加OPTIONS的-l 127.0.0.1参数:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1"

检查:用本地电脑或其他服务器

telnet x.x.x.x 11211

提示11211端口连接失败,则表示配置成功,Server禁止远程访问memcached。

参考


发表评论

电子邮件地址不会被公开。 必填项已用*标注