Skip to main content

Fixing slow Apache on localhost under Windows 7

A couple days ago, I documented my recent experience with my wireless network and how I got hacked. I briefly mentioned that I'm installing Windows 7 this time around.

Yesterday, I ran into an issue with 32-bit Apache 2.2.15 running on Windows 7 Ultimate 64-bit. This issue appears to only affect 64-bit Windows 7 and 64-bit Windows Vista web developers attempting to run 32-bit Apache on the system.

NOTE: I didn't test 64-bit Apache because that is experimental and 64-bit PHP is even more experimental. However, I doubt the results would be too different.

The issue is slow response times (anywhere from 1 to 3 seconds per request) when connecting to 'http://localhost/'. Connecting to 'http://127.0.0.1/' and 'http://[NetBIOScomputernamegoeshere]/' have fast response times. I've seen various fixes around that seem to boil down to these three:

- Disable the Windows Firewall.
- Disable IPv6 support.
- Edit the 'hosts' file.

It looks like there is a bug in IPv6 DNS mapping for localhost under Windows that causes the slowdown when used in conjunction with the Windows Firewall. If Microsoft employees are reading this - THIS IS A BUG...FIX IT! Apache doesn't seem to be the issue but it could be that 'localhost' via the firewall first tries an IPv6 address first, fails after a second or two, and then falls back to IPv4 at which point a connection to 32-bit Apache is established. Apache isn't the issue here - the Windows Firewall combined with IPv6 is. Anything 'localhost' only listening on IPv4 will have slow responses.

What worked for me was to use the IPv4 version of 'localhost' in my hosts file.  The 'hosts' file is usually located in the 'C:\Windows\System32\drivers\etc' directory on most systems. Editing the 'hosts' file was a pain in the neck. You can't simply right click or double-click to open the file (BUG! ALSO NEEDS TO BE FIXED!). You first have to fire up Notepad or your favorite editor using "Run As Administrator" (right-click) and then hunt for the file using the "File -> Open" dialog. Windows prevents editing that file normally (a good thing) but then get in the way of actually editing it (a bad thing).

Anyway, under Windows 7, the following two lines are commented out:

# 127.0.0.1 localhost
# ::1 localhost

Uncomment the first line but leave the second commented:

127.0.0.1 localhost
# ::1 localhost

Save the file. Try loading pages in Apache again. Should be back to normal speed.

Why this works?

The 'hosts' file supersedes all DNS mappings. So this "fix" merely forces the IPv4 mapping to occur when 'localhost' is requested. Uncomment the second line and Apache will slow down again.

Disabling the Windows Firewall supposedly fixes the issue but I'm pretty sure no one wants to run without a firewall. Disabling IPv6 also apparently fixes the issue but IPv6 is supposedly the future - IPv4 is what the OS falls back on without IPv6. The 'hosts' file fix is really the most desirable until Microsoft can figure out why the Windows Firewall is broken.

Hopefully this helps someone else and helps provide insight into the actual problem.

Side note: If you want to simplify future installations of Apache and you also use MySQL and PHP, you might want to try my portable version of Apache + MySQL + PHP for Windows.

Comments

  1. Thanks! I was looking for this! Fixed my problem.

    ReplyDelete
  2. Thank you very much! I was updating from xammp 1.7.3 to 1.7.4 and suddenly I had a dns lookup of 1second. (firebug was very helpful here)

    You fixed my problem. :-)

    ReplyDelete
  3. Brilliant, thank you. Occurring for me under Vista 64. Response time (as measured with LORI) went from 2-3 seconds down to less than 1/10 of a second.

    ReplyDelete
  4. hollllyyy crap, thank oyu!!!!

    ReplyDelete
  5. After wasting time thinking this was just a Drupal 7 problem, I found this post. Thanks a million!

    ReplyDelete
  6. Didn't help for me. That's what DID help in my case: installed Microsoft Visual C++ 2008 Redistributable Package (x64).

    ReplyDelete
  7. hi you should add place of hosts file because basic search cant find it.

    HOSTS FILE is in directory
    c:\Windows\System32\drivers\etc\

    ReplyDelete
  8. @WhiteNoise - I'm not sure why that would help fix a slow Apache install. Might fix one that was popping up a "Could not load 'some.dll'" issue.

    @Patrick - I can't believe I forgot something so critical. I forgot that there are a lot of people just getting started in web development who don't know where the 'hosts' file location is on the box. Article updated.

    ReplyDelete
  9. worked great for me. We where seeing load times of 20 - 22 seconds that have now droped to 1 - 2 seconds which is acceptable

    ReplyDelete
  10. Awesome! Solved My problem. Thank you!

    ReplyDelete
  11. I had PHP scripts connecting to a MySQL server hosted on... localhost. So even though I used 127.0.0.1 in the URL to access my scripts faster, it was slow as hell until I realized PDO (PHP Data Object) still used "localhost" to connect to the server. Thanks a lot for this!

    ReplyDelete
  12. Had the same issue here on Apache 2.2.22 / php 5.3.13 under Windows 2008 R2 and it also fixed the problem.

    Thank you

    ReplyDelete
  13. I run win 7 32 bit and I confirm the same issue occurs and the fix resolves the isse. Thanks very much.

    ReplyDelete
  14. I absolutely love you. You fixed my problem. THANKS!!!!

    ReplyDelete
  15. It worked for me just doing:
    #127.0.0.1 localhost
    to
    127.0.0.1 localhost

    ReplyDelete
  16. If that doesn't work, try disabling your antivirus, or adding an exception for your httpd.exe.

    ReplyDelete
  17. Thank you so much for writing this article. This helped my loads!

    ReplyDelete
  18. Thanks, thanks a lot, It solved the problem.
    Two seconds of wait for ever data connection with the server in localhost.
    Now it runs like light! THANK YOU!!!

    ReplyDelete
  19. Thanks a lot, it works like a charm !

    ReplyDelete
  20. That worked brilliantly. Thanks for taking the time to share this.

    ReplyDelete
  21. works like wonder..thanks!

    ReplyDelete
  22. Thanx very much, you've saved me!

    However, try :
    1) with commented ::1 localhost it will ping 127.0.0.1 and ping takes <1ms
    2) with UNcommented ::1 localhost it will ping ::1 and ping takes <1ms

    Also i tried to disable FW, and it doesnt help.

    => It looks, like, its more of Apache and IPv6 problem.

    ReplyDelete
  23. The commenting out of ::1 did the trick for me.
    Thanks a lot!

    ReplyDelete
    Replies
    1. Same here.
      Commenting out ::1 did the trick.

      My Hosts file looks like this now:
      # 127.0.0.1 localhost
      ::1 localhost

      When I comment out 127.0.0.1 it is still slow.
      Running Wamp on Windows 8 64bit

      Delete
  24. Worked for me on Windows Server 2008 R2. I had a yii appllication, the CGridView filter was taking 3 to 5 seconds to return the filtered records and some ajax dependent dropdowns was slow to return the data from db. This did the trick, Thanks.

    ReplyDelete
  25. MySQL remains slow with modified hosts file.
    MySQL access directly via 127.0.0.1 speeds up the queries. I think that it requires a valid MySQL user/password on %127.0.0.1 but not sure.

    ReplyDelete
  26. You're my hero! Looking for the program so long. Only had issues with my WordPress site on my server but this fixed it. Great!

    ReplyDelete
  27. It is crazy how often this article has helped people over the past few years. Microsoft REALLY needs to fix this bug in Windows.

    ReplyDelete
  28. jeez, saved my life. Thanks

    ReplyDelete
  29. THANK YOU!

    Wordpress on local WAMP insulation was taking 21 seconds to load simple page.

    Used your fix response on same page3 is less than a second.

    I had been firing up freecell and playing while waiting for a wordpress page to load. I could make several moves before the page loaded - I am serious - response was actually that long.

    Now I don't have time to even switch to the window running Freecell.

    Bob

    ReplyDelete
  30. Thanks helped me out a ton! Spent over a day on this.

    ReplyDelete
  31. Thank you so much.
    Disabling IPv6 has fixed the problem for me.
    I was searching from long time how to fix ajax speed problem on localhost and you answer my prayers !!!!

    ReplyDelete
  32. Thanks! You solved my problem. And yeah, there's no way I'm turning off the firewall.

    ReplyDelete
  33. I Can't believe it 0_0 it went from 5s to 1s Wow I Love you kind sir.

    ReplyDelete

Post a Comment