WordPress being slow, a DNS problem

A friend of mine runs a WordPress site and recently moved to a different provider. The transition went smoothly and the site ran fine for three weeks. One day he made a post like he normally does and the site become very slow. He deleted the post but with no result. His site was still extremely slow, I mean it took almost a minute to load the index page. Installation of a caching plugin, deactivating all plugins, selecting the default theme did not help at all.

Using Firefox with Firebug I saw that the problem seem to be with php files, both the index.php and wpsf-js.php, a part of WP-Spamfree, took 20 seconds to load.
The thing is though that he also runs a phpBB forum and there were no problems with the forum, also uploading a new php file, to show phpinfo(), run perfectly. It had to do with WordPress.

After some searching on the net it turns out to be a problem with the provider. The server on which the site is running, unable to resolve it’s own address.

Why is this a problem?

WordPress uses a program wp-cron.php which is called when a certain amount of time has passed.
Every time your blog is accessed by a user WordPress checks if the time is there to call wp-cron.php by comparing the time wp-cron.php last ran and the current time and when it determines it needs to run wp-cron.php it does so.
The way wp-cron.php is called is by the PHP fsockopen and doing a HTTP call. One of the parameters is your site’s URL. The fsockopen function needs to resolve that name to an IP address, that’s just the basics of the internet.
I guess you see the problem now, the function runs on the server and tries to resolve it’s own name. Which in this case doesn’t work.

Determine if this is the case

Before you go yelling at your provider they need to fix this, you better check if this is really the case.

If your provider gives you cPanel you can quickly check it by going to the Network Tools and try to resolve your website. If it comes back with an error, you can yell at your provider.

If you don’t have cPanel or you just don’t have the ability to do a DNS check you can use the method:
Create a file called cron-test.php and insert the following code.


Replace example.com on lines 2 and 5 with your site’s address and upload this in the root of your blog. If your blog resides in a directory upload the file there and change the GET /wp-cron.php to GET /your_directory/wp-cron.php

Now point your browser to http://example.com/cron-test.php and see if it succeeds or returns an error.
If the error has failure in name resolution in it’s message you have the DNS problem.

Solution.

The best way to solve this problem is to contact your provider, tell them the server can’t resolve its own IP address. If they know what they are doing they should be able to fix that. If they can’t or they just plainly tell you they won’t there is another solution.
You’ll have to add a line in a core file of WordPress. This comes with a problem though, when you upgrade WordPress you’ll have to change that file again.But I guess it’s better as having a slow website.

The hack

If you want to go ahead with hacking the core file, lets make sure the hack actually works.
We’ll apply the hack first to the above file.


Point your browser at the file again and it should return Success… if not let me know what error message you get and I’m sorry for now I don’t know how to solve it.

If it does work lets go ahead and starting applying the hack to the core file wp-includes/cron.php.
Open it in you favorite editor and search for:

function spawn_cron() {
	$crons = _get_cron_array();

	if ( !is_array($crons) )
		return;

	$keys = array_keys( $crons );
	if ( array_shift( $keys ) > time() )
		return;

	$cron_url = get_option( 'siteurl' ) . '/wp-cron.php';
	$parts = parse_url( $cron_url );

	if ($parts['scheme'] == 'https') {

We’ll add a line after line 12 so the part looks like this

function spawn_cron() {
	$crons = _get_cron_array();


	if ( !is_array($crons) )
		return;

	$keys = array_keys( $crons );
	if ( array_shift( $keys ) > time() )
		return;

	$cron_url = get_option( 'siteurl' ) . '/wp-cron.php';
	$parts = parse_url( $cron_url );
	$parts['host'] = $_SERVER['SERVER_ADDR'];

	if ($parts['scheme'] == 'https') {

And that’s it, save the file and your site should be running smoothly again.
Update on Dec 6, 2008, I forgot to put SERVER_ADDR in quotes, sorry for that

This article is filed under the category WordPress and has the following tags associated with it: , , , .
  • Sometimes this happens because the server has been chrooted, and the chroot doesn’t have DNS tools. It might help a clueless host to suggest that they try to install the necessary libraries within the chroot so that one can ping a site as the chrooted server user—if a ping works, usually that means domain resolution will too.

  • Peter

    @Austin:
    Good point, unfortunately in this case the server can do successful DNS queries to any other site not on the server, like cnn.com

  • Jon

    the first 2 code blocks appear empty for me…

  • Peter

    @Jon

    I’m sorry Jon, this sometimes happens when I edit a post, the plugin I use to show syntax doesn’t do well in the WordPress editor. It’s fixed now

  • Great article and informativ. I have this bookmarked. Thanks

  • I’m having this current problem, my forum (php) works no problem but accessing my blog takes FOREVER to load. I did the cron-test and it came back sucessful therefore it’s probably not a DNS problem.

    I thought it may be a problem with my theme as I could access the dashboard no problem (which has now decided to load slow as well). But I deactivated the plugins, changed the theme to the default, unpublished all the posts too, and still loading slow.

    My hosting provider is having internet problems right now due to a cable breaking in the sea, so he can’t do anything right now to help me!

  • update: it seems to be working now, 20 mins after I posted my comment!

    • Yeah I just checked and around 4 secs to load teh page isn’t that bad 🙂

  • musadsdsd

    Good point, unfortunately in this case the server can do successful DNS queries to any other site not on the server, like cnn.com

  • 4 second? its not bad

    Yours sincerely,
    donnie
    http://donnieproperties.blogspot.com

  • best price

    I never get any issue but I found something slow after install wp 2.7 because it’s update automatically.

    But that’s ok for wordpress performance.

  • bende oyunox.com da kullanmak istiyorum ama bu değişkeni çalıştıramadım.

    $cron_url = get_option( ‘siteurl’ ) . ‘/wp-cron.php’;
    $parts = parse_url( $cron_url );
    $parts[‘host’] = $_SERVER[‘SERVER_ADDR’];

  • Thanks For Author 😉

  • Hello! Wery cool!!!

  • Thanks for providing the above solution. This seem not to work for me though, eventually my wordpress stopped runing

  • endianx

    Wow! You nailed it. Put an entry in my hosts file and everything is fine. Thank you so much.

  • Thanks For Author

  • Hello,
    Can i take a one small pic from your blog?

    Thanks
    Zoran

  • Thank you for this post. It was helpful in determining whether it was a DNS issue of not and it is not. I just cannot figure out why my personal blog is loading so slowly.

    • What is your personal blog, I can see if I can be of assistance.

  • ben

    Hi,

    My site loads pretty slow and I think it’s because it’s quite graphic intensive (needs to be for a fashion blog). I have checked my dns and it is not a problem with that (i tried the cron-test) so wondered if you could point me in the direction of what to change on my blog?

    I have installed firebug but it hasn’t really broken down exactly what is causing the lag (around 8 seconds to load the homepage), and I do not tend to use many plugins on there. Any help would be great on the url that came with this comment.

    • The time it takes from start to finish for WordPress to build your page, this is just running the PHP code is about 6 secs.
      I think this is rather long. It could be several factors:
      – The CPU of the host is overloaded.
      – One of your plugins is acting up.
      – You ran an awfull lot of plugins.

      Shoot me an email [my name]@avirtualhome.com if you want to discuss this in more detail. Replace [my name] with peter.

  • Hey Peter,

    Fantastic guide you’ve written! You’re very good at communicating this rather complicated issue (I think;)

    I’s administering a small server where I also have my blog. I have the symptoms that you’re describing, however I don’t get excactly the same error that you mention:
    http://munk-nielsen.dk/wp-cron.php

    It says “unable to connect to munk-nielsen.dk:80”. This is strange since I’m able to access non-wordpress websites on the server from the server itself (using linux terminal http client: elinks). Therefore, it must be able to resolve its own address, but something still fails.

    We recently changed the IP adress of the server and I suspect that I’ve forgotten to update something somewhere… It’s running debian sarge.

    Any help would be appreciated!

  • GioMac

    this is a dump topic!!!

    $host_ip = $_SERVER[SERVER_ADDR];
    $argyle = fsockopen( $host_ip, 80, $errno, $errstr, 0.01 );

    this access server via IP, not via hostname, it will not work on virtualhost-based servers!!!

download