Fine Tuning Your WordPress Install

From DreamHost
(Redirected from WordPress Optimization)
Jump to: navigation, search

Overview

Over time, you may notice that your WordPress site runs slower or doesn't run as well as it should. There are several reasons this could occur, and this wiki walks you through some of the most common issues and provides solutions to help you resolve them.

Automatic upgrades

Staying on top of both plugins and core WordPress upgrades is the single best thing you can do. Not only will it keep your site less prone to hack attempts, but almost every new version of the software has tweaks that offer speed increases.

You can use either the One-Click Installs page in the panel or log into your WordPress panel to set up automatic upgrades.

To use our One-Click Installs feature in the panel to request upgrade notifications:

  1. Open your panel at (Panel > ‘Goodies’ > ‘One-Click Installs’)
  2. Choose your domain.
  3. Click the edit link under the ‘Upgrade Action’ column.
    Wp finetune1.fw.png
  4. Select the Upgrade automatically option.
    Note2 icon.png Note: Please note that all One-Click Installs require that the domain they are being installed onto is set to Fully Hosted.


  • When you request automatic upgrades, DreamHost updates the packages in the installer whenever they become available and notify you via email.
  • You can also update your site by logging into your WordPress dashboard. From there, you can update your core WordPress install, plugins, and themes.
  • As of WordPress 3.7, minor revision updates are done automatically by the WordPress application. DreamHost highly recommends that you allow these minor updates to automatically occur since they are typically bug fixes and/or security updates.

Caching

If spam isn't your problem, then the server is probably hitting your MySQL database more than it should. Since WordPress depends a lot on the database, it can make quite a few requests while trying to get the information needed to render your pages. This can be really inefficient and cause astronomical loads when sites like Reddit, Digg, Slashdot, and BoingBoing link to you.

Web Caching Plugins

There are several caching plugins available which promise to improve your loading times and decrease server load, allowing your site to handle large spikes in traffic like the Slashdot effect. Please note however that since your site is being cached the user may not see any immediate updates.

There are plenty of plugins that cache MySQL requests by building static content to load instead of hitting the database for every request. Below are some that work the best on our servers:

WP Super Cache
  • Picking up where WP-Cache left off, this plugin has included advanced features as well as a plugin and hooks system. It should be included with all new deluxe one-click installs of WordPress done by DreamHost. An installation guide can be found here: Installing WP Super Cache
Hyper Cache
  • This is probably one of the most user-friendly caching plugins out there. If you take one look at WP Super Cache and can't make heads or tails of it, HyperCache is an outstanding alternative for most end users.
W3 Total Cache
  • This plugin is also excellent, and gaining a lot of traction in the community, in part due to its inclusion of CDN features.
WP Widget Cache
  • Works together with Web Caching plugins above to further speed up your site – in particular if you're using many widgets.

Database

You should routinely optimize your MySQL database to avoid a "fragmentation" effect. The easiest way to do this is to use the WP-DBManager plugin which includes a function to optimize your database as well as the capability to schedule this optimization to run at an appropriate timeframe (recommended once per month at least).

FastCGI

You can activate Dreamhost's PHP FastCGI options which should improve your php code execution. If you are on a private server, you also have the option to activate XCache support which will further improve your PHP execution.

Important icon.png Warning: Some sites that have a lot of spam/bot/spider traffic could see a large increase in memory saturation when switching to FastCGI. If memory spikes are a problem, try switching to CGI and use a good caching plugin (see above).


Image Size

The size of your images can also contribute to a slow loading site. The larger your images, the longer it will take for it to download on the user's end. So you will want to make sure that your images are as small as possible. Please read over these instructions on how to optimize your images

JavaScript

JavaScript can add a lot of interactivity and interesting remote content to a site — However, if used liberally, it can also cause pages to load slower. There are a few things you can do to speed up your JavaScript commands.

Remote JavaScript

Instead of inserting remote JavaScript towards the top of your page — as plenty of instructions for insertion suggest — it is completely acceptable to add it right before the </body> tag in your theme. Due to the way page load order is handled in the browser, this will allow your content to start rendering and display before having to wait for remote files (which would load first if were they in the header).

If you'd like to save yourself from tweaking your themes manually, the JavaScript to Footer plugin may help quite a bit.

Local JavaScript

Pushing local JavaScript to the bottom of the page will also give the appearance of speeding up load time, so you might want to throw a little edit on your theme to migrate any local JavaScript from header.php to footer.php.

Also, while mod_deflate should compress JavaScript, you can cut a little bit of overhead off by pre-caching the compression. All you need to do is save gzipped copies of your theme's JavaScript in the same directory as your current files. Again, you'll need to SSH in to your web server to do this.

The location of JavaScript varies from theme to theme, but the theme itself can always be found in the "wp-content/themes/" directory. Once you have found the JavaScript for your site, simply run:

gzip javascript.js

So long as you replace "javascript.js" with the actual filename you wish to compress and repeat the process for all of your files, a compressed version of your .js files will be created. The benefit to this is that once it's done, you don't have to do anything else.

Note2 icon.png Note: Some themes attempt to compress JavaScript and CSS by adding a ".php" to the end of the file name. This actually processes the file through PHP unnecessarily and can cause a significant amount of overhead. If you notice your theme doing this, please modify your theme and use the gzip method discussed above.


Missing Files

Sometimes, due to upgrades or some poorly coded pre-packaged themes, your WordPress install might have some missing files. Missing files make the server expend a little extra effort and can drive up load considerably on a popular site. One of the most common missing files is the favicon (favicon.ico). If you've recently used the one-click installer, a favicon should be generated for you — but it never hurts to make sure one exists in the main directory for your site.

Adding a favicon if one doesn't already exist is easy. It does require SSH access however. In the root directory of your site (i.e. /home/username/example.com/), type:

touch favicon.ico

This command creates a blank favicon.ico file. But a blank icon is far better than one that is missing.

To check for any other missing files, you can run your site through Pingdom Tools. A single pass through their site will point out any missing files that could be causing your site to load slowly. Just look for filenames in red and then either replace them or remove references to them in your themes or posts.

Page Speed Optimization

Unless you're using nginx, you can use Google PageSpeed on your site to help optimize it further. Just by turning it on, it minifies your webpage. That means it takes your formatted source code and gets rid of the extra spaces you don’t use. This is called by using the PageSpeed filter “collapse_whitespace”.

There are many filters you can add to your site by editing your .htaccess file. You simply need to include a section like this:

<IfModule pagespeed_module>
   ModPagespeed on
   ModPagespeedEnableFilters remove_comments,rewrite_javascript,rewrite_css,rewrite_images
   ModPagespeedEnableFilters elide_attributes,defer_javascript,move_css_to_head
   ModPagespeedJpegRecompressionQuality -1
</IfModule>

You'll want to experiment with what works best for your site. View the Page Speed Optimization article for further details.

Plugins

Stats Plugins

While having a plugin that handles your stats (and displays them in your admin interface) is nice, they can actually inflate the database almost as quickly as unchecked spam. This means you should be cautious of using them, and pay special attention to your database if you do so.

Optimally, you'd use something that doesn't depend on your server environment like Google Analytics — but if you prefer a server side solution, Piwik is available in our one-click installer and works rather nicely.

Checking your Plugins

Firstly, it's extremely important to make sure your plugins are compatible with the version of WordPress you're using.

This is something you can verify at WordPress' Plugin Directory.

If you are experiencing site slowness, and you want to see if it's resource-usage related (and not something on DreamHost`s end), simply disable all your plugins, and switch to the default theme.

If your WordPress installation is suddenly running faster, then it’s recommended to go through your plugins and enable them one-by-one, to see if any of them is using more resources than the others.

For a more scientific approach, you can see how many queries and how much CPU time a page took to load.

To determine the number the queries and how much CPU is being used:

  1. Log into the server via FTP. You can find more info about how to use FTP here:
  2. Find your "footer.php" file which is found on the webserver.
  3. Navigate to your /wp-content/themes directory.
  4. In this directory, open the folder for the theme you’re using. For example, if you’re using ‘twentyfourteen’ you’d find the footer.php file at /wp-content/themes/twentyfourteen/footer.php.
  5. In the file, add the following content at the very bottom:
     <?php echo get_num_queries(); ?> 
     queries in <?php timer_stop(1); ?> seconds
    

By reloading this page each time you activate a plugin, you can check which one increases your queries and CPU time considerably.

On the bottom of your Wordpress site you’ll see:

# queries in # seconds


You will need to have any caching plugins disabled for this to work.

The Jetpack plugin is a bit special. You might try disabling individual modules in the plugin to improve performance. For each module, click "Learn More" and then "Deactivate".

The P3 Plugin Profiler plugin is a great tool to see what plugins may be slowing your site down. It gives you a simple graphical output to quickly see which plugins are using the most resources.

Revisions

Post Revisions is a WordPress feature that has proven to be helpful. While these are very nice to have, some databases can balloon if you're prone to making a lot of changes or spending a lot of time creating a post. To help keep your database size down — which keeps your install speedy — you might want to fine tune the amount of revisions your site saves.

Running a MySQL Query

If you don't want to add another plugin into your WordPress installation, then you can run a simple SQL query to remove revisions.

To set up a SQL query:

  1. Open your panel and navigate to the (Panel > ‘Goodies‘ > ‘MySQL Databases’) page.
  2. Log into phpMyAdmin.
    You will see your tables listed on the left.
  3. Click your database name.
  4. Make a note of the table prefix.
    Note2 icon.png Note: The table prefix may appear as "wp_" but it’s very possible to have a random string of letters such as "wp_qch9en_".


    In the upper left of the phpMyAdmin page are a series of icons:
    Wp finetune2.fw.png
  5. Click the SQL button to open the SQL command window.
    The SQL command page opens:
    Wp finetune3.fw.png
  6. Paste the following SQL command in the SQL window:
    DELETE FROM wp_posts WHERE post_type = "revision";
    
    

This will remove all revisions currently stored in the database. Just make sure to enter the correct table prefix into the query. In the example above, the prefix is just wp_.

Disabling Revisions

To prevent revisions from being created and stored in the first place, add this line to your wp-config.php file after the database info:

 define('WP_POST_REVISIONS', false);

You can find the wp-config.php file in your FTP account. It will be in the primary folder for your domain.

If you still want to save some revisions, you can use the following code in place of the one above:

 define('AUTOSAVE_INTERVAL', 120 ); // Default value is 60 seconds.
 define('WP_POST_REVISIONS', 3); // Number of revisions to save.

That limits the number of revisions to 3 and changes the auto-save value to every 2 minutes. You can change those values to something higher (or lower).

You can read more about revisions via the WordPress codex here:

Spam

The first thing to do while checking out an overloaded blog is to see if it's being spammed. It is one of the top reason why blogs go crazy on CPU usage. Check the "Comments" link on your WordPress install. If you're seeing the same sort of comments as the mail you see in your mail account's Inbox from spammers, you'll need to install a plugin to help deal with the incoming flow of spam.

Anti-Spam Plugins

There are a lot of anti-spam plugins that are available for WordPress, but we're only going to list a few that work the best on our servers:

Akismet
  • Pros: Spam checking happens off server, low impact on server overhead, works with most feedback forms.
  • Cons: Needs an API Key in order to run.
Cookies for Comments
  • Pros: Easy to install via WordPress, blocks users who don't have 'cookies' (which should only be bots), works with Akismet
  • Cons: Requires editing your .htaccess
reCAPTCHA
  • Pros: CAPTCHAs keep bots from submitting comments.
  • Cons: Spam isn't always done by bots, meaning manual spam will still get through. Not always readable/accessible.

Using a combination of these plugins is recommended, but keep in mind that the CAPTCHAs plugin is not always user friendly.

Themes

Using a good theme can be challenging. While the ones on wordpress.org are free and safe to use, some are going to be better than others. A fast way to see if your theme is the problem is to change to the default theme (currently TwentyFifteen) and see if that helps your site. When shopping for a theme, do some Google searches to see if people have complained about site speed with that theme. You'd be surprised what you find out.

Tools

Here are a few tools to help you troubleshoot performance issues with your Wordpress site:

  • Firebug: A very handy tool that displays what is loading and how long it takes.
  • YSlow: A companion to Firebug that might give you some pointers on what to fix and how.
  • Web Inspector: Built into Chrome and Safari, it gives you much of the same information as Firebug.

Only use the following if you are using a VPS or Dedicated server:

  • Load Impact: A load testing service that hits your server with many simultaneous users. A free version does a 50 user test. Be sure that you have caching enabled before running. During this process, you can watch the CPU load with 'top', and RAM levels with 'free -m' - or both with 'vmstat 1' (Ctrl-c to stop).

Upgrading to DreamPress or a Private Server

If you've tried the plugins and suggestions but are still seeing a high load on the server, then it could be that the traffic to your site has outgrown shared hosting. At this point you may wish to consider adding a Private Server or DreamPress to your hosting package.

If you have any questions about adding DreamPress or a Private Server to your account, please contact DreamHost support for more info.

Widgets

Be suspicious of all widgets (or sidebar content) that will force the user to poll other DNS servers. This may delay the loading of the site.

Also, check if your theme is using custom widgets to draw recent comments of posts. If this is not handled correctly, it will poll the database every time which increases your site's load considerably.

If you're using a lot of widgets, a good option is to use the WP Widget Cache plugin (in addition to standard caching) to reduce the processing time for their output.

XML-RPC

WordPress uses the xmlrpc.php file for remote administration purposes such as posting via third party applications. However, this file can be a target for attackers.

To protect your site, DreamHost has implemented rate-limiting on this file. You can disable xmlrpc.php although some plugins which depend on it (like Jetpack) may no longer work if you do.

See Also