為WordPress安裝Memcached對象緩存

為WordPress安裝Memcached對象緩存

WordPress的對象緩存是一種緩存由插件和主題設置的瞬態的方法。默認的WordPress對象緩存使用了MySQL數據庫,開發人員一直尋找替代方法來將這些瞬變存儲在其他位置,以加快WordPress性能。RedisMemcached都是用於將瞬態存儲在RAM中的對象緩存守護程序。

本教程將展示如何安裝Memcached服務器和PHP 7擴展,以便WordPress可以與Memcached服務器交互。

安裝Memcached服務器

從存儲庫安裝最新的Memcached守護程序和依賴項。

Ubuntu:

sudo apt install libmemcached* memcached libanyevent-perl libyaml-perl libterm-readkey-perl libevent-dev libsasl2-2 sasl2-bin libsasl2-dev php7.0-dev php7.0-json php-igbinary php-msgpack pkg-config libtool-bin build-essential git -y

Centos:

yum install memcached
yum install php-memcached

為WordPress配置Memcached

備份原始的Memcached配置文件:

sudo mv /etc/memcached.conf /etc/memcached.conf.bak

創建一個新的Memcached配置文件:

sudo nano /etc/memcached.conf

粘貼此memcached配置:

# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d
# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log
# Be verbose
# -v
# Be even more verbose (print client commands as well)
# -vv
# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 50
# Default connection port is 11211
-p 11211
# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache
# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 127.0.0.1
# Set unix socket which we put in the folder /var/run/memcached and made memcache user the owner
#-s /var/run/memcached/memcached.sock
# set permissions for the memcached socket
#-a 775
# Limit the number of simultaneous incoming connections. The daemon default is 1024
# -c 1024
# Lock down all paged memory. Consult with the README and homepage before you do this
# -k
# Activate optimal options
-o modern

Ctrl + X,Y並按Enter保存並退出

打開Memcached init.d配置文件:

sudo nano /etc/default/memcached

確定有下面這一行,以便Memcached啟動。

# Set this to no to disable memcached.
ENABLE_MEMCACHED=yes

重新啟動Memcached:

sudo service memcached restart

安裝Memcached PHP 7 PECL擴展

編譯Memcached PHP 7 PECL擴展:

sudo apt install php7.0-dev libtool pkg-config php-igbinary php-json php-msgpack -y
cd /tmp
git clone https://github.com/php-memcached-dev/php-memcached -b php7
cd php-memcached
libtoolize
phpize
./configure --prefix=/usr --enable-memcached-igbinary --enable-memcached-json --enable-memcached-msgpack
sudo make
sudo make install

為nginx或Apache啟用PHP 7擴展

添加memcached擴展:

echo "extension = memcached.so" | sudo tee -a /etc/php/7.0/mods-available/memcached.ini

重新啟動php7.0-fpm

sudo service php7.0-fpm restart

重新啟動Apache2或nginx

sudo service apache2 restart

安裝Memcached WordPress插件(兼容PHP 7.x)

MemcacheD Is Your Friend插件已經六年未更新了,所以此處站長幫提供的是在此插件的基礎上優化升級的版本,且兼容PHP 7.x。MemcacheD Is Your Friend升級版下載鏈接: https://pan.baidu.com/s/1PKae47m6m17g4O7X5T-2aw 提取碼: 3ici

安裝並啟用插件后,在WordPress後台 “工具 > Memcached”中可以看到以下信息:

MemcacheD Is Your Friend升級版
MemcacheD Is Your Friend升級版

為WordPress安裝Memcached對象緩存

0

Line

電話

粉絲頁

導航