Introduction#
WordPress, as a giant that occupies 40% of the website building market, can indeed be called a product of an era. It is precisely for this reason that many individual webmasters and businesses choose to use WordPress to build their own sites. In this article, I will briefly describe how to improve the access speed of WordPress sites using limited server and network resources.
About WordPress#
This section will elaborate on relevant knowledge about WordPress.
What is WordPress#
WordPress is a free and open-source blogging software and content management system based on PHP and MySQL. WordPress has a plugin architecture and a template system. As of April 2018, over 30.6% of the top 10 million websites use WordPress. WordPress is the most popular website content management system. Approximately 40% of websites globally (750 million) are built using WordPress. WordPress is currently the most popular blogging system on the Internet. It stands out in the most famous web publishing phase. Today, it is used on over 70 million sites.
Material cited from Wikipedia https://en.wikipedia.org/wiki/WordPress
For more information about WordPress, please visit the official WordPress website: https://wp.org.
Why Use WordPress#
WordPress has a very good ecosystem globally, and you can easily find information about WordPress on platforms like Google. The developer of WordPress, AutoMattic, has also established wordpress.org and wordpress.com to strengthen its ecosystem.
Compared to many domestic CMS systems like Typecho, WordPress's ecosystem is overwhelmingly superior.
Although the WordPress ecosystem in China can be considered a complete mess, it is undeniable that it still stands out among many CMS (content management systems).
WordPress has many advantages such as high scalability, convenience, ease of development, and ease of learning. Its function hook mechanism greatly enhances the convenience of development.
Declaration#
- Since the WordPress framework is still being updated, many themes and plugins are also being updated, we will not use a large number of optimization schemes that modify core algorithms in an incomplete production environment of the examples in this article.
- This article is for reference only and is primarily applicable to the construction of personal blogs, which have less interaction, fewer article modifications, and less UGC. The optimization methods mentioned in this article are mostly not applicable to large SNS, BBS, etc., and may even slow down performance. If you have optimization needs in this area, you can contact AHdark for paid support.
- Due to some readers taking advantage of the articles, starting services or products that conflict with my original services after learning technology from my articles, which affects my personal services and products, or taking material from my blog posts without citation or promotion, or even violating the CC-BY-NC-SA 3.0 agreement at the time of publishing my blog posts, I feel disheartened and angry. Therefore, this article will only discuss concepts rather than implementations, and the personal plugins mentioned will not be provided.
- This article does not apply to SaaS WordPress services.
Optimizing WordPress#
Why Optimize#
First, we all understand that WordPress is a server-side rendering (SSR) framework that integrates both front-end and back-end and is built using PHP.
Since FastCGI used by PHP is mostly compiled in real-time, it cannot achieve good concurrency effects. That is, in the same server configuration, the concurrency capacity of Java or Go web programs is far higher than that of PHP.
Therefore, we need to compensate for the deficiencies caused by the low efficiency of the PHP language as much as possible, after all, it is difficult to find a better and more powerful CMS system than WordPress.
How to Optimize#
Typically, for web pages, we will use the following methods:
- Web architecture
- Separation of static page files
- Front-end and back-end separation
- Network architecture
- Static file content distribution network
- Fast return link for dynamic files
- Database
- Database query syntax optimization
- Database index optimization
- Database query caching (Memcached or Redis)
- Database read-write separation
- Site-database separation
- Replace MySQL with PostgreSQL or Oracle
- File system
- File caching
- PHP Opcode caching
- HTML caching for web pages
In addition, there is a more effective optimization method:
Rewrite the website using a faster language.
Clearly, this is not realistic.
Web Architecture Optimization#
This section will mainly explain the optimization schemes for the composition and architecture of web pages.
Separation of Dynamics and Statics#
In the WordPress I manage, by pressing F12, you can see this scenario.
I originally wanted to include a few more, but most of them have been messed up by their original authors. Here I specifically criticize Tkong Blog.
These sites share a commonality in their Page Sources, which is that only page files exist under the accessed domain name.
Separating the static files referenced by the page to object storage or other public services can effectively improve the loading speed of static files and reduce the pressure on the source site, which is an optimization method that can significantly enhance user experience.
For unregistered domains, it is naturally a pity that servers in mainland China cannot be used, and a large number of static files loaded through slower cross-border networks will also reduce the speed of user page rendering. This method is also very effective in such cases.
You can use the source-global-cdn plugin to separate the static files of the WordPress core while accelerating Gravatar.
You can also use the Source Global CDN for WordPress service to accelerate plugins and themes published on WordPress.org.
Front-end and Back-end Separation#
For WordPress, front-end and back-end separation is quite unrealistic. Delegating page rendering to users can reduce server pressure, but naturally places a greater burden on the user side. (However, we usually do not care how slow user rendering is; we just let them switch browsers.)
AlphaPic utilizes program functions to separate static JavaScript/CSS files and data content, fetching data via Ajax and rendering it on the page.
Network Optimization#
The content regarding network optimization will not be elaborated here; a separate article may be written later. (Digging a hole.)
Content Distribution Network#
CDN caches resources to edge nodes and secondary nodes, reducing resource return rates, distributing resources across various nodes, thereby improving access speed and reducing pressure on the source site.
You must have heard about the incident of "Xi'an Yima Tong crashing twice." After unpacking the mini-program, it can be seen that the static files used did not utilize CDN (Content Distribution Network) but were provided directly. Under a traffic volume of 35,000 QPS, a single data center's bandwidth exit is absolutely impossible to sustain.
Using a content distribution network can not only significantly reduce the pressure on the source site but also speed up access. So why not use it?
Here are some recommendations:
- Alibaba Cloud CDN: Good global speed with widespread node distribution, many nodes, requires registration in China.
- Tencent Cloud CDN: Good domestic speed with many low-latency nodes, average quality abroad, requires registration in China.
- CloudFlare: Requires large customers for domestic nodes, high defense for foreign nodes, free, average speed, average latency, occasionally blocked by operators, no registration required.
- CloudFront: AWS's CDN, offers 2TB of traffic, good speed, no nodes in China, good speed, average latency, no registration required.
This is not an exhaustive list of CDNs I have tested; other manufacturers, including some smaller ones, also have high-quality nodes.
In the aforementioned separation of dynamics and statics, using object storage + CDN can also significantly reduce costs, improve speed, and reduce pressure on the source site.
Using Tencent Cloud COS + Tencent Cloud CDN can provide a Tencent registered domain name, which is very friendly for webmasters without a registered domain.
Example: Using Tencent Cloud Object Storage COS for Static Resource CDN Acceleration on WordPress Blog
Dynamic Link Optimization#
Under the crazy destruction of advertisements, you must have heard of "full-site acceleration."
Its essential meaning is to make the return link shorter and faster, with most nodes using internal protocols to improve response speed, accessing the site with the optimal link, thereby reducing the response delay of dynamic content.
I personally recommend Alibaba Cloud's DCDN and Tencent Cloud CDN's dynamic-static acceleration mode, and I am currently using it myself.
Database#
Query Optimization#
When rendering a page on the server, there are usually 10 to 20 database queries.
Using WordFence can increase this to over 30 times while significantly improving security.
Some queries do not need to be updated every time, so we can use memory caches like Redis or Memcached to cache them. Clearly, the speed of database queries is far lower than directly retrieving data from memory.
For Memcached, you can refer to this article: How to Enable Memcached Memory Caching in WordPress to Improve Website Speed
I personally have a more refined method of using Redis and Memcached for caching simultaneously, but it belongs to a paid service, so I will not disclose it.
Site-Database Separation#
Sometimes, when you open your WordPress blog, you find a fatal error: unable to connect to the database. Then, you open the Linux terminal and type service mysqld status
.
Then you find that the MySQL service is down.
The database, as a necessary condition and foundation for continuous service operation, should not be casually shut down. If your database is shut down like this, it is likely due to memory overflow.
MySQL databases can be configured for resource size usage, but if the resources are too small, it will lead to slow responses, and if too large, it will cause memory overflow during high concurrent web page access. So, how can we ensure that the site does not cause database memory overflow when the concurrency is high?
The solution I adopt is site-database separation, which means moving the database out of the web server. I am currently using Alibaba Cloud RDS and Tencent Cloud TDSQL-C databases.
This can effectively reduce the probability of the database shutting down due to memory overflow, which would lead to the site being inaccessible.
PHP Optimization#
The most advanced method of PHP optimization is certainly to modify the PHP core program itself, but this is too difficult, so it will not be written about.
Opcode Caching#
PHP is a scripting language that compiles PHP files into Opcode for execution. Some PHP files do not need to be re-read and recompiled every time, so we can cache the compiled Opcode to eliminate the time consumed in the compilation process.
For example, using PHP extensions: Zend OPCache / APC.
Conclusion#
In summary, optimizing WordPress is a relatively complex process. I have personally researched this for a long time and only have a rough understanding of some content; practical implementation is even more difficult. Therefore, slacking off or finding someone to assist is also a good choice.
My knowledge is not limited to this article; to prevent certain people from plagiarizing my technology, I have not included all my knowledge about WP optimization in this article, nor have I written any actual operational methods. However, spending a little money to have me optimize it, or building a good relationship with me to get my optimization for free (of course, this is relatively difficult), might also be a good choice.