Why is the response on localhost so slow?

251,016

Solution 1

The issue was with Apache's main settings file httpd.conf.

I found this:

There are three ways to set up PHP to work with Apache 2.x on Windows. You can run PHP as a handler, as a CGI, or under FastCGI. [Source]

And so I went into the Apache's settings and saw where the problem was: I had it set up as CGI, instead of loading it as a module. This caused php-cgi.exe to start up and shut down every time I made a request. This was slowing my localhost development down.

I changed the settings to load PHP as an Apache MODULE and now it all works perfectly. :)

To load the PHP module for Apache 2.x:

1) insert following lines into httpd.conf

LoadModule php5_module "c:/php/php5apache2.dll"

AddHandler application/x-httpd-php .php

(p.s. change C:/php to your path. Also, change php5apache**.dll to your existing file name)

2) To limit PHP execution only for .php files, add this in httpd.conf:

<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>

3) set path of php.ini in httpd.conf (if after restart you get error, then remove this line again)

PHPIniDir "C:/php"

Thank you all for your efforts.

Solution 2

For me, setting the ServerName property in httpd.conf fixed the delays (they were up to 10 seconds at worst):

# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
ServerName 127.0.0.1:80

Solution 3

I had the very same problem.

Setting localhost redirect to 127.0.0.1 in hosts file did not help. Optimizing MySQL server did not help (InnoDB -> MyISAM, changing many cache related directives in my.ini).

Then I used web webgrind and narrowed down the problem to "new PDO(...)" call. Changing

mysql:host=localhost;dbname=dp-ui;charset=utf8 

to

mysql:host=127.0.0.1;dbname=dp-ui;charset=utf8

in dsn for PDO completely solved the problem! Page loading time went from over 3000 ms to 16ms.

However I am really confused why the "127.0.0.1 localhost" line in hosts file did not help.

Solution 4

Check if /etc/hosts is correct. Like this:

# hostname mobrglnx1 added to /etc/hosts by anaconda

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 *****

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 *******

In the place **** give your hostname.

Solution 5

I had the same problem and finally discover that it was coming from two facts :

  1. I use Mac OS X Mavericks
  2. I accessed my project via the URL http://myproject.local/ because I put a line 127.0.0.1 myproject.local in /etc/hosts

The problem appears because the .local tld is reserved for Bonjour service, and this since Mac OS X Lion (10.7).

Changing the tld for something else fixed the problem.

Share:
251,016

Related videos on Youtube

Peter Perháč
Author by

Peter Perháč

Currently am on a contract, building APIs for the Valuation Office Agency, transforming the way agents interact with the VOA. Coursera certificate - Functional Programming Principles in Scala Coursera certificate - Functional Program Design in Scala Oracle Certified Associate, Java SE 7 Programmer Oracle Certified Professional, Java SE 7 Programmer II Oracle Certified Expert, Java EE6 Web Component Developer github.com/PeterPerhac July 2018 update: Books currently on my desk: (see my goodreads for up-to-date info) Functional and Reactive Domain Modeling Practical Vim, 2nd Edition (Great) books I parked (for now): Reactive Messaging Patterns with the Actor Model: Applications and Integration in Scala and Akka Functional Programming in Scala Learn You a Haskell for Great Good A Practical Guide to Ubuntu Linux Domain-Driven Design: Tackling Complexity in the Heart of Software Plan to look into: Play framework Linux administration Dale Carnegie books (various) Read cover-to-cover: Advanced Scala with Cats Clean Code Release It! - Design and Deploy Production-Ready Software The Phonix Project Spring in Action (3rd edition) Pragmatic Scala - Create Expressive, Concise, and Scalable Applications UML distilled, 2nd edition Pro C# 2010 and the .NET 4 Platform The Art of Unit Testing: With Examples in C# Read selectively: The Well-Grounded Java Developer - Vital Techniques of Java 7 and polyglot programming Switch - how to change things when change is hard Practical Unit Testing with JUnit and Mockito Effective Java, second edition Java Concurrency in Practice Nationality: Slovak

Updated on September 17, 2022

Comments

  • Peter Perháč
    Peter Perháč over 1 year

    I am working on a tiny little PHP project for a friend of mine, and I have a WAMP environment setup for local development. I remember the days when the response from my local Apache 2.2 was immediate. Alas, now that I got back from a long, long holiday, I find the responses from localhost painfully slow.

    It takes around 5 seconds to get a 300B HTML page served out.

    When I look at the task manager, the httpd processes (2) are using up 0% of the CPU and overall my computer is not under load (0-2% CPU usage).

    Why is the latency so high? Is there any Apache setting that I could tweak to perhaps make its thread run with a higher priority or something? It seems like it's simply sleeping before it's serving out the response.

    • Alexis Lê-Quôc
      Alexis Lê-Quôc over 14 years
      Is localhost resolving properly DNS-wise? ping localhost should come back instantaneously with 127.0.0.1.
    • Peter Perháč
      Peter Perháč over 14 years
      that works fine, response received in <1ms
    • Marcus Spiegel
      Marcus Spiegel over 14 years
      Does it have the same behaviour when requesting static content (i.e. when loading http://localhost/index.html)? If not, it might be a PHP issue, not an Apache issue.
    • Alexis Lê-Quôc
      Alexis Lê-Quôc over 14 years
      Can you check your apache logs? Do you see the source address of the requestor, is it 127.0.0.1 or the IP of the machine? If the latter, apache might be a reverse DNS lookup before responding. Worth checking httpd.conf for that.
    • kargig
      kargig over 14 years
      Is there a chance you are trying to reach the server from a local LAN box using http://hostname.localdomain/project/test.php? If so, which name servers do you have set on the box running Apache? Local name servers or the ones provided by the ISP? If you have two name servers at the box and the first one is down, Windows does not switch all the following requests to the second name server, it will always try to connect to the first name server, wait until it times out and then probe the second NS. Make sure that every name server you have specified is up and running. If not, are you using `
    • Peter Perháč
      Peter Perháč over 14 years
      I tried both localhost and 127.0.0.1 but no difference. I am going to check out the HostnameLookups directive
    • Peter Perháč
      Peter Perháč over 14 years
      i found the following in the access log: 127.0.0.1 - - [17/Sep/2009:20:17:16 +0200] "GET /index.html HTTP/1.1" 200 132, so i guess everything is okay and the requestor is 127.0.0.1
    • Bernie
      Bernie over 14 years
      If you are using any type of CMS, make sure that you disable reverse DNS querying in the database.
    • Ladadadada
      Ladadadada over 11 years
      What the many, varied answers to this question show is that there are dozens, if not hundreds of different reasons page requests can be slow. If you have arrived at this question because you too have slow page requests, you will need to dig deeper into the cause of the slowness before you can get a useful answer here. strace and tcpdump are useful tools for this.
    • Admin
      Admin over 10 years
      I don't know if this could help, bu when I added DNS configuration for the NIC on the server, WAMP become so slow on localhost (loads page in ~= 30 sec !). When I removed the DNS config, it becomes fast. Try leaving DNS blank.
    • Synetech
      Synetech over 10 years
      @all, I have a question for everybody that posted solutions that involve adding or replacing text with 127.0.0.1: does it still work if you access 127.0.0.2, 127.1.2.3, and so on or does hard-coding 127.0.0.1 create an unnecessary restriction?
    • Admin
      Admin almost 2 years
      It won't let me post an answer due to low reputation, but in my case it was having PHP XDebug turned on. Turn that off and things will get a lot faster. I narrowed it down to PHP XDebug by checking the PHP error log, which had a bunch of XDebug timeout errors in it. C:\xampp\apache\logs\error.log
  • Peter Perháč
    Peter Perháč over 14 years
    i don't seem to have an apache.conf file, also I searched for HostnameLookups directive in all of the files and I found it in the core.html.en manual file. It said it's Off by default, so I guess it's off
  • hohner
    hohner about 12 years
    This works -- wish I could upvote twice
  • DouglasHeriot
    DouglasHeriot almost 12 years
    This fixes it for me too! Gone from 10s latency to 2ms!
  • AgA
    AgA about 11 years
    My Apache stops on adding the lines as mentioned in the Source. How do I do in Windows?
  • PrivateUser
    PrivateUser almost 11 years
    Can somebody tell me which file I should edit to make it work?
  • michalko
    michalko almost 11 years
    You should edit the line where you are connecting to database (in your PHP script). E.g. change the line: $link = new PDO('mysql:host=localhost;dbname=dp-ui;charset=utf8'); to $link = new PDO('mysql:host=127.0.0.1;dbname=dp-ui;charset=utf8');
  • user184985
    user184985 almost 11 years
    I was working on an EXT-JS application recently and has a huge problems of MYSQL data queries taking too long to respond. MYSQL was basically too slow. Thank goodness, Got the answer here..... just changed my host in the connection script from: host = localhost; to host = 127.0.0.1 My server responses went from 3min(180secs) to less thant 1 sec. Thanks alot.
  • williamcarswell
    williamcarswell over 10 years
    I struggled for hours and this was the solution.
  • milan
    milan over 10 years
    why is this a problem if localhost resolves properly? what's going on those 10sec before it responds? waiting to time-out somewhere?
  • Elliptical view
    Elliptical view about 10 years
    Also, why are you using an IP for ServerName? In the docs it talks about "Syntax: ServerName [scheme://]fully-qualified-domain-name[:port]", e.g. ServerName secure.localhost:80, or is it ServerName secure.localhost.:80 (with a right most dot to make it a FQDN? (See: httpd.apache.org/docs/current/mod/core.html#servername )
  • sshow
    sshow about 10 years
    @Elipticalview As the comment in the file says; If your host doesn't have a registered DNS name, enter its IP address here.
  • Mladen Janjetovic
    Mladen Janjetovic over 9 years
    This solved my problem. Strange... I didn't notice that problem in SQLite
  • Adrian
    Adrian about 9 years
    The same for me, but just for WordPress. Had to replace "localhost" with "127.0.0.1" in wp-config.php
  • Rafael Beckel
    Rafael Beckel over 8 years
    It was the case for me. My domain was set in ipv4 line, but not in ipv6.
  • mwallisch
    mwallisch over 8 years
    Same for me. Requests took > 5 sec before I put the additional line in /etc/hosts. Now my stuff runs in ~0.1 sec.
  • Dave Stewart
    Dave Stewart over 8 years
    Same here, after following all the other advice on the page (which probably helped in its own way) with a Laravel app, hosted via XAMPP: DB_HOST=127.0.0.1 # Use 10.0.2.2 when accessing va Homestead
  • T.Todua
    T.Todua over 7 years
    that link became expired. Use this: goo.gl/2EVth9
  • Vahid Amiri
    Vahid Amiri over 7 years
    This also solved my problem with my MAMP (Windows 10) installation!
  • JoeP
    JoeP about 7 years
    I logged in specifically to upvote this, but it turns out that I already did so in September 2015 and then promptly forgot about it. Completely solved my issue again anyway.
  • robsch
    robsch over 5 years
    Put me into the right direction. Added 127.0.0.1 something.atmy.localhost and now request doesn't take 20 seconds anymore. Instead the local apache responds immediately. Don't know that much about networks. I guess the domain name gets resolved too slowly, because something is not correcly configured.
  • Kamel Labiad
    Kamel Labiad almost 4 years
    how to replicate something on mac?
  • Kamel Labiad
    Kamel Labiad almost 4 years
    I use .test but still same problem
  • lepix
    lepix almost 4 years
    @KamelLabiad you should not use it, it is a real top level domain (TLD): fr.wikipedia.org/wiki/.test