
Speed and resiliency - one of those factors that invariably affect the popularity of your resource, because even with the best content in the world, a slow working site will irritate readers and sooner or later you will lose them. In this article, we will optimize the most popular blogging engine - Wordpress, running on PHP. And at the same time consider a few general points in the optimization of sites.
1 Test current speed
To find out whether something has changed after our optimization, it doesn’t hurt to measure the current download speed of blog pages to start with, so that it has something to compare with. There are several tools to help do this:
1.1 Pingdom
Pingdom conducts quick and visual testing of the download speed of all elements on the page and presents the results in the form of a convenient chart where you can see which site elements load slower than necessary and other problem areas.
The benchmark of a fairly well-known resource.
1.2 YSlow
YSlow is a plugin for Firefox, which is built into perhaps the best plugin for a web developer,
Firebug . It analyzes more than 20 factors that affect the speed of the site and estimates the overall performance on a 100-point scale, and each individual element is rated from A to F.

')
1.3 The number of requests and the time of their execution
By inserting a small piece of PHP code, you can put into the footer the number of queries to the database and the time spent on their execution.
<? php echo get_num_queries (); ? > queries in <? php timer_stop ( 1 ); ? > seconds.
2 Web Hosting
Believe it or not, web hosting is one of the most important details affecting blog performance. Without going into details, here is a very simple description of the most popular types of hosting, which will help you to roughly estimate the load on the server:
- Shared Hosting - on average about 100 people can be hosted on one server;
- VPS - about 20 people can be hosted on one server;
- Dedicated - the server will be used only by you.
To view the approximate load on the server, log in via ssh and enter the
top command in the console.
This, of course, does not mean that you will not be able to speed up a blog that runs on shared hosting (Shared Hosting), but it is worth remembering that the higher the productivity, the more resources we have at our disposal.
The geographic location of the server also plays a significant role; therefore, you always have to present your target audience approximately. For example, if they are Russian-speaking users, you should not buy an American hosting, etc.
3 Installing and configuring the server
Make sure that the planned load corresponds to the capacity of the server and it can handle it. First of all it will depend on the amount of RAM and processor. As a rule, Wordpress is installed on
Apache , but many successful solutions exist on the basis of other http servers:
nginx ,
lighttpd , etc.
Do not forget to upgrade to the latest version of
PHP and
Apache .
3.1 Disable unused services
You can get more available RAM by disabling unused services and optimizing MySQL and Apache.
- Remove clamd;
- Configure SpamD to use only 1 child process;
- Remove Mailman, of course, if you are not going to start the mail service.
3.2 MYSQL Query Cache
Since the stability and speed of Wordpress is quite dependent on the work of the database, you should make sure that the settings in
my.cnf correspond to the capabilities of the server. The first step is to set the query caching settings by adding the following lines to
my.cnf :
query_cache_type = 1
query_cache_limit = 2M
query_cache_size = 20M
In order for the settings to take effect, you will have to restart the MySQL service.
3.3 Compiler Cache: XCache or Eaccelerator?
The compiler cache increases the performance of compiled scripts on the server by caching them - this will help reduce the time it takes to execute PHP scripts. It is worth trying both solutions, however, according to the results of the experiments, an increase in productivity when using Xcache is 5% higher than with the Eaccelerator.
3.4 Increase the maximum number of connections on Apache
Increasing the maximum number of connections in
httpd.conf will increase performance, because the server will be able to handle more connections at once. However, you should change this parameter carefully, in order not to exhaust the entire amount of RAM and not to slow down the server, so always test the new settings before starting them up. Install for example 150 connections:
max_connections = 150
Do not forget to restart the Apache service to apply the settings.
4 Optimization of code and graphics
So, the server is working and now it’s time to play with the Wordpress code.
4.1 Disable Hotlinks
Every time you use your server to store images, you use its resources substantially more. Quite often, people borrow your images by putting hotlinks on their servers. This not only takes the channel, but also creates a certain load on the server.
Add the following code to the
.htaccess file, replacing
example.com with the name of your domain to disable the use of hotlinks:
< IfModule mod_rewrite . c >
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC]
RewriteRule .*\.(gif|jpg|png|ico)$ - [F,L]
</ ifModule >
4.2 Use external hosting to store images
Hosting images on external servers can significantly reduce server load. In the example below, you can see a decrease in the amount of RAM used on one of the blogs after transferring images to
Amazon S3 service.

4.3 Compress java-script code
Compressing javascript is quite a simple task. Because it runs every time you view a page, you can reduce the size of Javascript by removing all unfilled space. Here is a simple tool to help you do this for you -
JavaScript Compressor .
4.4 Javascript at the top of the page
It often happens that the site starts loading slowly or stops at all, because another resource from which javascript is called (for example Digg badges, Tweetmeme, etc.) is not available or offline. To avoid this, bring the entire javascript code to the bottom of the page, and the fact that for some reason could not be rendered - try to conclude in iFrame.
4.5 Use browser cache
The browser cache itself will certainly not make your blog faster, but it will help reduce the load on the server by caching frequently loaded objects (styles, interface elements, etc.).
Try inserting the following code into the
.htaccess file:
FileETag MTime Size
< ifmodule mod_expires . c >
< filesmatch "\.(jpg|gif|png|css|js)$" >
ExpiresActive on
ExpiresDefault "access plus 1 year"
</ filesmatch >
</ ifmodule >
4.6 Compress Static Data
You can reduce the size of the loaded page by allowing the browser to receive and transmit data in a compressed form. It will also reduce channel loading and the amount of data loaded.
The following code in
.htaccess can help you with this:
AddOutputFilterByType DEFLATE text / html text / plain text / xml application / xml application / xhtml + xml text / javascript text / css application / x-javascript
BrowserMatch ^ Mozilla / 4 gzip-only-text / html
BrowserMatch ^ Mozilla / 4.0 [678] no-gzip
BrowserMatch bMSIE! No-gzip! Gzip-only-text / html
4.7 Use CDN for static files.
If you store all the images on the same domain, the browser will wait for them to download one by one. Suppose you have 12 pieces on their page, if you divide them between three subdomains, they will be loaded simultaneously from three “different” sources instead of being loaded by the browser one by one.
You can try to transfer all css & javascript files to
files.yoursite.com , and images and temporary files to
static.yoursite.com . Or simply use a
CDN (Content Delivery Network) - a large network of servers located around the world, which will not only store your files on different subdomains, and therefore download them in parallel, but also deliver data to the user from the server closest to him. All this will allow to load data much faster.
5 wordpress
In this part of the article, we will look at performance enhancements that can be applied directly to Wordpress.
5.1 Update to the latest version.
Updates to newer versions not only make it possible to fix the detected vulnerabilities, but also improve performance. For example, wordpress 2.8 was significantly optimized to work with the database.
5.2 Disable Post Revisions
In all versions of wordpress, starting from 2.6, the editors of your articles were automatically saved each time they were edited. This slows down the database and increases its size without special need.
To disable post revisions, add the following line to
wp-config.php :
define('WP_POST_REVISIONS', false);
To delete previously saved text revisions, run the following query in PHPmyadmin:
DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision'
5.3 Reduce the number of requests
Remove unnecessary requests to speed up page generation. For example, the following typical code is found in all themes for wordpress:
< meta http-equiv ="Content-Type" content =" < ? php bloginfo ( 'html_type' ); ? > ; charset= <? php bloginfo ( 'charset' ); ? > " />
We can easily rewrite to:
< meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" />
Already two requests less. Pretty simple, isn't it?
6 Wordpress Plugins
And lastly I offer you a few plugins that can improve wordpress performance. Once everything described above is complete, these plugins will help achieve even higher performance.
WP Super CacheThis is probably the best plugin for wordpress. WP Super Cache creates static html versions of each page and loads them every time, thereby doing without queries to the database. This significantly increases the speed of loading pages and reduces the load on the server. Strictly recommended for installation.
PHP Speedy WPThis plugin solves another problem indicated in this article - the removal of empty space in CSS & javascript. However, there are some compatibility issues with this plugin with WP Super Cache, besides, it has not been updated for a long time, so use it at your own peril and risk.
Optimize DBThe plugin allows you to optimize MySQL tables without the help of PHPmyadmin.
Happiness to you and your cozy website,%% username.