Advertisement

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.

1
2
3
4
5
6
7
8
9
10
<?php
$argyle = fsockopen( 'example.com', 80, $errno, $errstr, 0.01 );
if ( $argyle ) {
fputs( $argyle, "GET /wp-cron.php HTTP/1.0\r\n"
. "Host: example.com\r\n\r\n" );
echo "Success sending the GET.\n";
} else {
echo "Error: $errstr ($errno)\n";
}
?>

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.

1
2
3
4
5
6
7
8
9
10
11
<?php
$host_ip = $_SERVER[SERVER_ADDR];
$argyle = fsockopen( $host_ip, 80, $errno, $errstr, 0.01 );
if ( $argyle ) {
fputs( $argyle, "GET /wp-cron.php HTTP/1.0\r\n"
. "Host: example.com\r\n\r\n" );
echo "Success sending the GET.\n";
} else {
echo "Error: $errstr ($errno)\n";
}
?>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function spawn_cron() {
	$crons = _get_cron_array();
 
	if ( !is_array($crons) )
		return;
 
	$keys = array_keys( $crons );
	if ( array_shift( $keys ) &gt; 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function spawn_cron() {
	$crons = _get_cron_array();
 
 
	if ( !is_array($crons) )
		return;
 
	$keys = array_keys( $crons );
	if ( array_shift( $keys ) &gt; 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

Share and Enjoy:
  • Digg
  • del.icio.us
  • Ma.gnolia
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
Tags: DNS, fsockopen, solution, WordPress
22 Responses to “WordPress being slow, a DNS problem”
  1. Austin says:

    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.

  2. Peter says:

    @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

  3. Jon says:

    the first 2 code blocks appear empty for me…

  4. Peter says:

    @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

  5. Istoselidon says:

    Great article and informativ. I have this bookmarked. Thanks

  6. ananyah says:

    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!

  7. ananyah says:

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

  8. musadsdsd says:

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

  9. donnie says:

    4 second? its not bad

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

  10. best price says:

    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.

  11. oyunox.com says:

    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'];

  12. FerhatAKIN says:

    Thanks For Author ;)

  13. Webitec says:

    Hello! Wery cool!!!

  14. domain says:

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

  15. endianx says:

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

  16. Aleks says:

    Thanks For Author

  17. Zoran says:

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

    Thanks
    Zoran

  18. 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.

  19. ben says:

    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.

    • Peter says:

      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.

  20.  
Pingbacks and Trackbacks
  1.  
Leave a Reply