1. Home
  2. VPS
  3. Why is the response on localhost so slow?

Why is the response on localhost so slow?

Rsdaa 04/01/2022 273

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.

masegaloeh

17.5k 9 9 gold badges 51 51 silver badges 99 99 bronze badges

asked Sep 17 '09 at 17:15

Peter Perháč Peter Perháč

1,009 1 1 gold badge 9 9 silver badges 13 13 bronze badges

12

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

6

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.

8

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:

SetHandler application/x-httpd-php

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.

3

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.

3

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

I use Mac OS X MavericksI 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.

2

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

1

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.

The question has a tag apache-2.2, but if someone is affected by this nefarious issue also on WAMP with Apache 2.4 + PHP 5.5, the following answer on SO did the trick for me:

edit httpd.conf and disable the loading of the CGI module by commenting this line: LoadModule cgi_module modules/mod_cgi.so

https://stackoverflow.com/a/18786773/260080

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Server Fault works best with JavaScript enabled


PREV: Top 6 Benefits of Server Virtualization - HiTechNectar

NEXT: What is virtualization? - IONOS

Popular Articles

Hot Articles
Back to Top