Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 I that got back from a long, long holiday, I find the responses from localhost painfully slow.

It takes like 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 are the responses so slow? 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 just sleeping for a couple of seconds before it serves out the response.

Any help would be much appreciated.

share|improve this question
2  
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. – Ladadadada Nov 1 '12 at 12:20

11 Answers

up vote 11 down vote accepted

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

Thank you all for your efforts.

share|improve this answer
Thank you for this explanation. It was very annoying. – Mert Nuhoglu Jun 29 '10 at 15:01
My Apache stops on adding the lines as mentioned in the Source. How do I do in Windows? – AgA Apr 2 at 13:02

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
share|improve this answer
3  
This works -- wish I could upvote twice – hohner May 19 '12 at 20:46
2  
This fixes it for me too! Gone from 10s latency to 2ms! – DouglasHeriot Jul 2 '12 at 5:32

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.

share|improve this answer
thank you man, you solved my months-lasting problem! +1 – GRoNGoR Dec 8 '12 at 11:06

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.

share|improve this answer
1  
seems to be pointing in the right direction, a 30k html file loads almost instantaneously while a small php script is awfully slow. Any ideas what could be wrong with my php installation? – MasterPeter Sep 17 '09 at 18:32
now i'm quite convinced it's php. thanks, i'll go from here (maybe will need to raise another question soon :)) – MasterPeter Sep 17 '09 at 18:39
well, first start with a simple <? phpinfo(); ?> to check whether it's your skript or php in general... phpinfo should also load quiet fast – Marcus Spiegel Sep 17 '09 at 19:42

In your httpd.conf be sure to set the setting HostnameLookups Off.

share|improve this answer
3  
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 – MasterPeter Sep 17 '09 at 18:28

Is localhost resolving properly DNS-wise?

ping localhost should come back instantaneously with 127.0.0.1.

share|improve this answer
that works fine, response received in <1ms – MasterPeter Sep 17 '09 at 17:36
i thought firefox could be the problem, but takes 5s even in IE, so must be either some system setting or Apache setting or gremlins. – MasterPeter Sep 17 '09 at 17:38
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. – Alexis Lê-Quôc Sep 17 '09 at 18:00
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 – MasterPeter Sep 17 '09 at 18:27

I had the same problem, and this article fixed it:

Cubic: Fixing slow Apache on localhost under Windows 7.

share|improve this answer
2  
Linking to the article is good, but you should also summarize it into a meaningful answer. – Scott Pack Mar 15 '11 at 13:30

In case it helps anyone, I had this problem and it boiled down to being incorrect DNS lookup.

The DNS Server on the server was set to 127.0.0.1 - I changed it to use the Google Public DNS servers, and that made it a whole heap faster.

share|improve this answer

If you are using any type of CMS, make sure that you disable reverse DNS querying in the database.

share|improve this answer

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 http://localhost/project/test.php or http://127.0.0.1/project/test.php?

Do you have HostnameLookups On set as a configuration directive?

share|improve this answer
1  
I tried both localhost and 127.0.0.1 but no difference. I am going to check out the HostnameLookups directive – MasterPeter Sep 17 '09 at 18:19

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.