Home  >  Article  >  CMS Tutorial  >  How to optimize wordpress in nginx

How to optimize wordpress in nginx

尚
Original
2019-07-16 10:45:253229browse

How to optimize wordpress in nginx

Solution to the slow running of WordPress under Nginx:

Methods to optimize the slow running of WordPress

1. Reduce the number of plug-ins Use

This is the first point. Most people often do not have enough coding knowledge, and it is impossible to code every plug-in, so most people are using a large number of plug-ins. Most plug-ins will perform data query and generation, which will take time during the page generation process. If your host has strict restrictions (to balance resource usage), this time may even become longer. Reducing plug-ins is one of the very necessary optimization methods

2. Reduce HTTP requests

Every access to JS, CSS and other files is an HTTP request. If the plug-in is used less, the number of HTTP requests will naturally be smaller. The speed is naturally faster

WP will determine the usage of the plug-in every time the page is accessed. Letting WP do fewer judgments means doing fewer data queries. Release its burden.

3. Choose themes carefully or optimize manually

Although many themes are beautiful, they are not very good at optimization. Various JS, various CSS, various background images, various Various blank lines and various redundancies will affect the loading speed. However, this does not mean that you cannot choose a gorgeous theme, but the theme should basically meet the following requirements, or you can manually modify it to meet the following requirements:

Merge CSS and JS: Merge multiple CSS into 1.

Merge background images: Use CSS Sprites to merge most background images into one file, which also reduces HTTP requests, reduces file size, and speeds up rendering time.

Reduce data queries: Don’t make data queries where HTML can be directly output.

Reasonable JS and CSS placement: Reasonably place some JS and CSS at the bottom of the web page.

Optimize the background image of the theme

4. Reduce the use of external resources

External resources are resources that are not on your own server. Using too many external resources will encounter the following problems:

The time to resolve DNS becomes longer

Every domain name requires DNS resolution to work properly. If the content on the website needs to load the resources of a.com, b.com, c.com, and d.com at the same time, it will take more time to resolve the DNS of each domain name separately and establish a connection. Similarly, if your website is a.com, if you load similar domain names such as 1.a.com and 2.a.com at the same time, the DNS resolution time will also be lengthened. It is impossible to completely avoid external resources, but they can be used as little as possible.

The loading speed of external resources is uncontrollable

This is mainly for the content of some foreign sites, such as fickr, twitter, etc. Because of well-known reasons, loading their resources will slow down significantly. Loading speed. Domestic sites are relatively fast.

5. Reduce database queries, or use database caching plug-ins

Many plug-ins require database queries. The above section on plug-ins and themes also mentioned optimizing the code to minimize the number of database queries. If you don’t understand, you can consider using database cache, which can cache the query content of the database. It should be noted that the database cache plug-in should not be used together with the static cache plug-in. Because both cache content and reduce queries, using them together means duplication of work.

6, Use the page static cache plug-in

The principles of the static cache plug-in and the database cache plug-in are similar. They both organize the content in advance and generate static files. When needed, directly Take it out without having to search or generate it, which is a waste of time. A cached static file can basically be equivalent to a static image without performing too much complicated content.

There are also some other optimization methods, such as using GZIP to compress web pages, optimizing blog post images, using CDN distributed network to accelerate static files, etc.

Install wordpress plug-in and check the reason for slowness

After this site has been running for a period of time, it takes up to 20 seconds to access the homepage every time. It also takes 20 seconds to access a single article page. It took 10 seconds, which was simply unacceptable. I almost collapsed. I finally made up my mind to solve it completely.

By consulting a large amount of information, the system was optimized by referring to the method in the previous section.

1. First block all plug-ins and find that the system problem still exists. The speed of opening the homepage is still about 10 seconds.

2. Check the http request status through httpwatch and find that when accessing the homepage, get php The page time is 9 seconds, and the time to open images, js, css, etc. is about 2 to 3 seconds, so it is not caused by too many http requests.

3. Open the slow query of the mysql database and check the slow query status of the database. It is found that most queries are relatively fast. I have tested some queries and the speed is very fast. Therefore, database factors can also be eliminated.

There is really no other way. Finally, I considered using a wordpress plug-in to study what happened on the displayed home page, and then see where it takes the longest time, and then slowly optimize it accordingly, so I had no choice but to do this. .

When I arrived at the wordpress website, I searched for the debug plug-in. I searched a lot and tried many, but none of them met my requirements. Finally, I found a plug-in "Debug Queries". I downloaded and installed it according to the old habits of wp. Finally, as shown in the picture below, click the enable button to enable the plug-in.

How to optimize wordpress in nginx

After installing the plug-in, open the homepage of the website and check the effect and find that there is no change

Finally open an article page and find Jiujiu at the bottom of the article page What you are looking for, the picture below:

How to optimize wordpress in nginx

The picture has been marked with different colored areas

Red area: time: 0.000xxxx Query required Time

Green area: Query: query sql statement

Call from: require, xxx calling sequence, etc.

Then the following are additional calls. The page shows which functions and calling processes are called during this page display process, and the time required (the query time of the database, based on this time and the total time we calculate the time spent by PHP)

The last page is down Figure:

How to optimize wordpress in nginx

The blue part: is the summary of the data query time for this visit. You can see that the query took a total of 0.24 seconds, and 83 queries occurred (this also shows that wp The plug-in brings performance problems, but the amount of data in the system is currently small, which does not affect the performance of the system)

The green part: is a summary of the overall time during this user visit. The statistics here should be Includes the overall time for php.

The red part: is the total time of this visit

The purple part: the percentage spent in the php program during this visit. 97.5%

Yellow part: It is the percentage of time spent on the MySQL database during this visit 2.5%

We can see from the above data that in a 10-second user visit, Mysql only takes up 2.5% of the time, so it is certain that the 10-second access problem is caused by links other than mysql, and it should mainly be a problem with PHP.

Restart the php process to solve the slow problem

From the analysis of the above department, we can already understand that the problem is likely to be in php. It is not me to troubleshoot the php problem. expertise, so I decided to restart the php process to see the effect.

1. Find the php process

Enter the su – command, switch to the root account

Enter: ps -ef | grep php command to view the php process, as follows

How to optimize wordpress in nginx

From the picture we can understand that the php process id is 26765 and the process name is php-fpm (this is the php program required by nginx).

2. Find the startup path of the php process

Enter the following command lsof -p 8584 (8584 here is the ID of the php process when solving the problem, the above 26765 is still the search for the recent article) The id of the process)

How to optimize wordpress in nginx

3. Close the process

kill -9 8584 Close the old php process

Sometimes php opens There are many processes that are difficult to close. You can use the following method to do it

killall php-fpm (This site uses this method)

4. Restart the php process

Enter the following command

/usr/sbin/php-fpm Restart the process

For more wordpress related technical articles, please visit the wordpress tutorial column to learn!

The above is the detailed content of How to optimize wordpress in nginx. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:Is WordPress theme paid?Next article:Is WordPress theme paid?