Why is the response on localhost so slow?

Solution 1:

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 2:

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 3:

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