Apache Localhost Loading Very Slow: Here is the Solution
When you are working on your local development server or live production server running with Apache, you may experience a slowness issue while loading your scripts and web pages, for various reasons. In this post, I will provide a number of solutions that may help you solve this issue.
What Causes Localhost to Load Slow?
There are a number of things that contribute to the server response time of scripts and web pages and sometimes one or a combination of these things may result in the slow loading of your scripts and pages. I will briefly mention some of these things which may help you solve the loading speed issues on your Apache localhost server environment without needing to touch any server settings. In case they don't work for you, I will continue with what you can do in your server setup to solve the slowness issue.
Check If You Have the Correct Apache Version Installed
First of all, make sure that you have installed Apache on your computer or server correctly (see how to install Apache on Windows) and that you chose the correct version of Apache (see Apache vs. Apache Lounge) to suit your requirements.
If you are not using the correct Apache version, uninstall the current one and install the correct one after a system restart. If possible, try using the latest stable Apache release, which will ensure your server will be free of previous bugs and issues.
Check for System Requirements and Possible Hardware Issues
Another reason why the localhost is loading very slow might be the hardware on your computer (processor, memory, etc.) are not powerful enough to run the server platform you are using or the scripts that you are trying to run. Make sure that your system meets the requirements of Apache and any third party program you are using. Also, make sure that all the hardware is working properly.
Use the Most Up-to-Date Browser or Try Another One
Outdated web browsers may also cause the web pages and scripts to load slow. Update your web browser to the latest version and also try running your scripts on other web browsers to see if the same issue will happen in them too.
Check Your Antivirus Software
In some cases, the antivirus software or the default Windows Defender may block Apache, resulting in not functioning or decreased loading speeds. Make sure that's not the case and add Apache to the exceptions list if necessary.
Check If Your Scripts and Pages are Properly Optimized
It is also important to know if the slowness is a permanent or temporary issue. Does it happen at certain times, for example when you are running another application? Did it start to happen after you changed something on your computer or server environment? Did you notice the slowness after installing a new program or a browser extension? Answering these questions may also help you solve the issue.
Assuming the above recommendations didn't solve your Localhost slowness issue, you can try the following actions, separately.
Possible Solutions to Localhost Slow Loading Issue
Unfortunately, each case is unique and one solution that worked for someone else may not work in your case, hence you may need to try a number of possible solutions to fix the issue.
#1 Load PHP as an Apache Module
As it is also explained in the official PHP manual on this page, it is possible to run PHP with Apache in three ways: As CGI, FastCGI or as an Apache handler. Make sure that PHP is installed and loaded as an Apache handler on your server.
In order to load PHP as an Apache handler, you need to insert the following lines into the Apache configuration file, httpd.conf, which is located in the conf folder of your Apache installation:
LoadModule php5_module "c:/php/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
The above code is for PHP 5 and it assumes that PHP is installed in C: directory. Change the path based on your installation path.
#2 Disable the CGI Module
In some cases, disabling the Apache CGI module get rids of the lag while loading scripts and makes your pages load faster. To disable the CGI module, find the Apache configuration file (httpd.conf) and comment out (or remove) the following line:
LoadModule cgi_module modules/mod_cgi.so
Remember to restart the Apache server after making the above change and saving the httpd.conf file.
TIP: In order to comment out a line in the httpd.conf file, you simply add # to the beginning of that line, as follows:
#LoadModule cgi_module modules/mod_cgi.so
#3 The Hosts File
Find the hosts file on your computer and open it in a text editor. Make sure that the localhost entry is set properly like the following:
Also remove the following line (or comment out) if present:
TIP: In general, you can find the hosts file in the following location on your computer:
There are also cases where an antivirus or similar application may insert lots of domains to the hosts file, making the size of the file grow larger. Remove those domains from the hosts file and it may help increase the page loading speed.
#4 Replace localhost with 127.0.0.1 in MySQL Connection
When connecting to a MySQL database on localhost (e.g. using the mysqli_connect function), the string localhost is used as the database server. Replacing it 127.0.0.1 is known to solve the slow loading issue in some cases.
So, instead of using this:
mysqli_connect("localhost", "db_user", "db_password", "db")
try using this:
mysqli_connect("127.0.0.1", "db_user", "db_password", "db")
#5 Set ServerName in httpd.conf File
The ServerName is probably set correctly in your httpd.conf file, if not, insert the following line to set it.
#6 Change the DNS
Changing the DNS details for your server may also solve the issue. If your DNS is 127.0.0.1 and none of the other solutions worked for you, try using a different DNS such as OpenDNS or Google public DNS and see if it will work for you.
#7 Remove zend_extension from php.ini
If you are working on a XAMPP server, comment out (or remove) the line that activates the zend_extension. Depending on the version of your server, the following line may show difference:
zend_extension = "\xampp\php\ext\php_xdebug-2.1.0-5.3-vc6.dll"
TIP: To comment out a line in php.ini file, simply ad ; in front of that line.
More Server Tips
How to Set FTP Quota for an FTP Account Apache vs. Apache Lounge: Which One to Choose? How to Check If cURL is Enabled on Your Server How to Install Apache HTTP Server on Windows: The Apache Lounge Version How to Install Apache HTTP Server on Windows What is the Use of .ftpquota File? How to Prevent Search Engines from Indexing Your Pages and FilesServer Tips