Aug 29, 2024
Nadia
8min Read
Aug 29, 2024
Nadia
8min Read
Among the different network connection errors you might encounter is “localhost refused to connect” or the “ERR_Connection_Refused” error.
When you try to connect to an IP address, you usually try to connect to a different computer. However, since localhost uses the loopback address network, connecting to it means you’re establishing a connection with your own computer.
That’s why many developers and network administrators use localhost to test programs or sites that are still in development. Doing so reduces the risk of breaking a live site.
So, when you can’t access localhost, you won’t be able to access your project in a local environment.
There are various causes of this error, as shown in the table below. Also, note that you have to be on your local machine to access your localhost – you can’t access it on any other device.
| Error code | Localhost Refused to Connect |
| Error type | Browser/Firewall |
| Error variations | ERR_CONNECTION_REFUSED 127.0.0.1 refused to connect This site can’t be reached localhost refused to connect. |
| Error causes | Server blocked by firewall Server running on a wrong port Apache server not running properly Browser settings need to be reconfigured |

It is a common error that can happen when working on a local machine. The localhost simulates a web server running on your computer. When the “localhost refused to connect” error appears, it is likely due to misconfigured port. Other common reasons include insufficient permissions and the Apache webserver not running properly.
If you prefer a visual guide to fixing this error, watch our video tutorial below. Otherwise, feel free to skip to the next section.
Here are five methods to solve the “localhost refused to connect” error, in no particular order.
A firewall is a security system that monitors and filters your traffic, blocking potential threats. While it’s an important tool, the detection protocols may run inaccurately sometimes.
A firewall might incorrectly block safe web pages or incoming connections. Therefore, if you’re currently developing a website and you need to access its locally hosted version, it’s best to temporarily disable the firewall. That way, your test site won’t be blocked.
Remember that re-enable your firewall immediately once the website goes live and you no longer see the “ERR_CONNECTION_REFUSED” localhost problem message.
Here are the steps to disable the firewall on Mac, Windows, and Linux, respectively.
How to disable the firewall on Mac

Note that the steps to disable the firewall on Mac might be slightly different depending on your macOS version.
How to disable the firewall on Windows
There are two ways to disable the firewall on Windows 10, 8, 7, and Vista – via the control panel or command prompt.
Here are the steps to disable the firewall using the control panel:

If the option is not clickable, it means the firewall settings are under another security program’s control. If this is the case, you might want to uninstall that app first.
Meanwhile, these steps apply for turning off the firewall via the command prompt:
netsh advfirewall set allprofiles state off
How to disable the firewall on Linux
The steps to stop and disable the firewall vary according to your Linux distribution.
If you use Ubuntu or Debian, your firewall should be managed by ufw. If you use CentOS or RHEL, the default firewall management tool will be firewalld.
It’s important to stop the firewall first before disabling it. Otherwise, you’ll experience errors during the process.
To disable the firewall on ufw, follow these steps:
sudo ufw disable
sudo systemctl disable ufw
sudo ufw status sudo systemctl status ufw
sudo systemctl enable ufw sudo ufw enable ## verify that ufw started ## sudo ufw status
Alternatively, here are the steps to stop and disable the firewall on firewalld:
sudo systemctl stop firewalld
sudo systemctl disable firewalld sudo systemctl mask --now firewalld
sudo systemctl status firewalld
sudo systemctl unmask --now firewalld sudo systemctl enable firewalld sudo systemctl start firewalld ## verify that the firewalld started ## sudo firewall-cmd --state
The DNS cache stores data from websites you visit, so these web pages will load quicker the next time you access them.
However, if the DNS contains too many records, it might fail to function.
To solve this, flush the DNS to erase the records:
ipconfig /flushdns
The default port number for the web server is 80. If you’re trying to access it by typing localhost in your browser – which will direct to localhost:80 – but it turns out your web server is not configured to use the default port, you’ll get the localhost error or 127.0 0.1 refused to connect message.
However, port 80 may sometimes be used by another application on your computer. For example, if Skype is currently using it, you wouldn’t be able to run the Apache server on the same port simultaneously.
To solve this problem, first, let’s check whether port 80 is free. We’re going to use XAMPP to do this.
XAMPP is a web server solution stack used to build websites offline on a local web server. It’s a cross-platform tool, so it works on Windows, Mac, and Linux. It’s commonly used to install WordPress on a local computer.
Here are the steps to check free port number on Windows and Linux – it will be slightly different on Mac.

Next, let’s proceed to connect with the correct port number using the listen command.
The listen command is required to instruct Apache HTTPd to listen to only specific IP addresses or ports. If this command is not present in the config file, your server will fail to start.
Here are the steps to change Apache’s port in XAMPP:
Listen 80 ServerName localhost:80
Listen 8080 ServerName localhost:8080
If you’re using WAMP – a solution stack for the Windows operating system – do the following steps to change the running port.
Listen 80 ServerName localhost:80
Listen 8080 ServerName localhost:8080
If your Apache web server is not running, you won’t be able to access localhost.
To check the status of your Apache, the command will vary depending on the distribution that you use.
Here’s the command for Debian or Ubuntu:
$ sudo systemctl status apache2
And here’s the command for CentOS/RHEL/Fedora:
# systemctl status httpd
The result from this status check will tell whether your server is running or stopped.
If it tells you that the server is stopped, try to troubleshoot by restarting it using the following command.
For Debian and Ubuntu:
sudo systemctl restart apache2
For CentOS/RHEL/Fedora:
# systemctl restart httpd
The output of the restart command will look like this:
* Restarting web server apache2 [ OK ]
If the restart attempt fails, you’ll get an error message. The message may vary depending on your choice of distribution, but it will fall along these lines:
apache2: unrecognized service
Failed to restart apache2.service: Unit apache.service failed to load: No such file or directory.
If you see these error messages, try to reinstall the service. Once you’re sure the web server is installed correctly, restart it using the corresponding command.
Your browser settings can also cause the 127.0 0.1 “refused to connect” error because every web browser has a different setup and characteristics.
For instance, when you enter an HTTP web address, some browsers automatically redirect you to its HTTPS equivalent. However, if that site has an invalid SSL certificate or doesn’t have any, you’ll get the localhost refused to connect error message.
If you’re using Chrome browser, follow these steps:

When trying to access localhost, you may encounter two potential errors – ERR_CONNECTION_REFUSED and ERR_CONNECTION_TIMEOUT. Both are closely related but require different solutions.
“ERR_CONNECTION_REFUSED” localhost error indicates that the server rejected the attempted connection, while “ERR_CONNECTION_TIMEOUT” indicates that nothing happened and the client is still waiting for a response.
Therefore, to solve “ERR_CONNECTION_REFUSED” localhost issue, you need to check the firewall since it’s what rejects the data packets. You’ll also need to look at the Listen directive in the httpd.conf file and make sure it’s correctly set.
Meanwhile, to solve “ERR_CONNECTION_TIMEOUT” on Chrome, you’ll need to look at your connection settings because this error means the service is not running as it should or is not correctly installed.
“Localhost refused to connect” is a network connection error you might encounter when working on a project on your local web server.
It is triggered if the firewall blocks your server or you use the wrong port. The localhost error can also happen if your Apache web server or Chrome browser is not configured correctly.
There are five methods to solve the “localhost refused to connect” error:
We hope this article helps you solve the “localhost refused to connect” error.
One alternative to using localhost for local development is to use your computer’s IP address as the host. You can find your IP address in your system preferences or by typing ipconfig in the Command prompt for Windows or ifconfig in the Terminal for Mac or Linux.
A localhost IP address refers to the computer that the user is currently using. It is the IP address of the loopback network interface, which is typically assigned the IP address 127.0.0.1. Users use this IP address to access the computer’s own network services without connecting to an external network.
All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.