search
HomeDatabaseMysql Tutorial[原创]使用memcached为WordPress站点提速

转载请注明出处: http://www.codelast.com/ 本文的前提条件:你对你的WordPress站点服务器有控制权,例如,你使用的是VPS搭建的WordPress站点,而不是虚拟主机。如果不符合条件就不用往下看了,因为后面的很多操作是需要root权限来安装、配置软件的。另外,

转载请注明出处:http://www.codelast.com/

本文的前提条件:你对你的WordPress站点服务器有控制权,例如,你使用的是VPS搭建的WordPress站点,而不是虚拟主机。如果不符合条件就不用往下看了,因为后面的很多操作是需要root权限来安装、配置软件的。另外,我使用的是32位的Linux系统,如果你是64位的系统,可能有些许不一样,或者出现一些文中没有遇到的问题,请注意。

首先说一下使用memcached为WordPress站点提速的意义。由于我不是做前端开发的,所以下面的部分内容说的不一定正确、深刻,您就凑合着看吧。
如果你的WordPress站点访问量很大,那么VPS负载可能就很高,而这其中,可能有很大一部分就是MySQL的负载——它要将大量内容读取出来,并返回给访客。其实这部分工作可以通过缓存来提高性能,memcached就是这样一个软件,可以帮助我们实现这一点。

memcached的官方网站介绍中,我们知道它是:

Free & open source, high-performance, distributed memory object caching system.

说得直白简单一点,它就是把最常访问的那些内容缓存在内存中,有新访客到来的时候,先从内存中找,找到了就直接返回,否则再去查数据库,这样就极大地提高了性能。
有人可能会使用WP Super Cache这个WordPress插件来为WP提速(而且它的效果似乎更好),但此插件会生成大量缓存文件,占用大量磁盘空间,从而影响WordPress备份文件的大小,这对我来说并不是一个好结果,因此,我没有用它。
文章来源:http://www.codelast.com/
那么,在WordPress中,我们怎么使用memcached呢?这不是仅仅安装memcached软件就可以做到的,还要和PHP结合起来,所以涉及到好几个部分,下面分别阐述:
【1】安装memcached
首先我们要从memcached的官方网站下载一个稳定版,例如,我下载的是 1.4.15 版,然后准备安装它。
但是memcached的根基是一个网络开发库 libevent,所以,你要先安装 libevent。我下载的是 2.0.21(稳定版),解压出来,然后编译安装:

./configure --prefix=/usr/local/libevent
make
make install

注意我安装到了 /usr/local/libevent 目录下。
然后编译安装memcached:

./configure --with-libevent=/usr/local/libevent/ --prefix=/usr/local/memcached
make
make install

注意我指定了libevent的安装目录,并且把memcached安装到了 /usr/local/memcached 目录下。
文章来源:http://www.codelast.com/
【2】启动memcached
安装了memcached软件之后,我们就要启动它了:

cd /usr/local/memcached/bin/
./memcached -d -m 256 -u root -t 64 -r

-d 表示以守护进程的方式启动memcached程序,-m 256 表示最大可使用256M的内存,-u root 我不太明白有什么用(网上的某些教程里有这样用的),-t 64 表示使用64个线程,-r 表示最大化core文件的限制。
这样启动之后,memcached就开始在默认端口 11211 上监听了。
现在再看看进程中是否已经有了memcached:

[root@localhost]# ps -ef | grep memcached
root      2180     1  0 22:29 ?        00:00:00 ./memcached -d -m 256 -u root -t 64 -r

最后再把它加入开机启动项,编辑 /etc/rc.local 文件,添加一句:

/usr/local/memcached/bin/memcached -d -m 256 -u root -t 64 -r

文章来源:http://www.codelast.com/
【3】安装PHP的memcached扩展
为了能让WordPress用上我们刚安装的memcached,需要先为PHP安装一个memcached扩展。
这个页面去下载一个稳定版的memcached扩展,例如,我下载的是2.2.7版,然后解压:

wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar zxf memcache-2.2.7.tgz
cd memcache-2.2.7

如果你没这样装过PHP扩展的话,你会很奇怪地发现,解压出来的目录下竟然没有 configure 文件!没有 configure 文件如何能生成Makefile,如何能编译安装呢?
这个时候,我们需要用PHP安装时附带的 phpize 程序来帮助我们生成configure:

cd memcache-2.2.7
/usr/local/php/bin/phpize

注意,我的PHP是安装在 /usr/local/php 目录下的,如果你的目录不同,请作相应的修改。顺利的话,就会发现生成了configure文件;如果遇到了以下错误:

Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script.

文章来源:http://www.codelast.com/
那么,就需要安装以下两个package(如果是Ubuntu,就apt-get install,这里就不废话了):

yum install m4
yum install autoconf

然后再重新执行phpize命令,再编译安装:

make
make install

会提示:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

然后修改你的php.ini配置文件,在最后添加:

[memcache]
extension=memcache.so

最后重启你的web service,如果你用的是nginx,那么需要重启 php-fpm 和 nginx:

/etc/init.d/php-fpm restart
/etc/init.d/nginx restart

文章来源:http://www.codelast.com/
【4】安装、配置WordPress的memcached插件
上面的步骤已经够麻烦了,但是到了这一步,你就快成功了。
在WordPress后台的插件安装页面中,搜索“memcached”,会找到“Memcached Object Cache”插件,安装之,然后这里要注意了,与平常安装插件不一样的是,安装完这个插件之后,不要“启用”它,否则会报错。
我们需要手工把安装插件得到的 object-cache.php 文件拷贝到WordPress的 wp-content 目录下,它就可以开始工作了(有点奇怪吧):

cp wp-content/plugins/memcached/object-cache.php wp-content/

然后memcached就开始为你的WordPress默默地“做奉献”了,尽管你看不到它。如果你不想再管它,那么到这一步就可以收手了,不过,你可能还想知道它工作得怎么样,那么你就要继续看下去。
文章来源:http://www.codelast.com/
【5】安装memcached的管理插件
同样在WordPress的插件安装界面中搜索“WP Memcached Manager”,安装此插件,启用它,即可在WordPress管理后台的左侧菜单中,看到多出了一项“Memcached”:

WordPress memcached menu

点击“Edit servers”,如下图所示的默认server:

WordPress memcached manager add server

然后再回到“Memcached”菜单,就可以看到下拉列表中多出了一项,即我们刚添加的server,点击“Manage Server”,就会看到关于memcached的一些统计信息啦:

WordPress memcached manager manage server

这下踏实了,我们知道memcached确实在工作了,并且hit和miss的数据都知道了。
文章来源:http://www.codelast.com/
【6】进一步优化
有一个叫做Batcache的WordPress插件,能为我们上面的工作锦上添花,建议在WordPress后台安装它(不需要配置)。它的原理是:
在没有Batcache、有memcached的情况下,访问WordPress页面时,需要从内存中获取多个缓存对象,而Batcache可以把整个页面作为一个对象缓存,从而从内存中只需要获取一个缓存对象,这样就提高了速度。

至此,我们整个安装、配置memcached的过程就结束了,在访问量很大的时候,你的WordPress站点的性能一定会得到质的提升。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

What are common causes of poor MySQL query performance?What are common causes of poor MySQL query performance?Apr 12, 2025 am 12:11 AM

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),