Sunday, December 29, 2024
Google search engine
HomeGuest BlogsLocalhost Refused to Connect – How to Fix the Error

Localhost Refused to Connect – How to Fix the Error

Introduction

Localhost refused to connect is a common error that may occur when working on a local machine. In this article, you will learn the most common causes of the localhost refused to connect error and how to resolve it.

How to fix Localhost refused to connect error.How to fix Localhost refused to connect error.

What is the Localhost Refused to Connect Error?

Localhost is a loopback address used for testing and developing on a local machine. When a user pings localhost, the request remains on the local network and connects to the machine in use. If such a connection is not possible, the browser displays an error message:

localhost refused to connect.

Browser showing the localhost refused to connect error.Browser showing the localhost refused to connect error.

The localhost refused to connect message displays one of two possible errors:

  • ERR_CONNECTION_REFUSED (the server rejected the request for connection)
  • ERR_CONNECTION_TIMEOUT (the server has not responded to the request and the client is still waiting for a response)

What Causes the Localhost Refused to Connect Error

Common causes that result in localhost refusing to connect:

  • The server being blocked by the firewall.
  • Localhost doesn’t resolve to 127.0.0.1.
  • Apache failing to run properly.
  • DNS failing to function.
  • The server being accessed from the wrong port.
  • The browser settings blocking the connection.

To solve the issue, identify the source of the error. The best way to do so it to test out each probable cause, starting from the most frequent ones.

How to Solve the Localhost Refused to Connect Error?

The sections below provide methods to resolve the localhost refused to connect error.

Temporary Disable the Firewall

Check whether the firewall settings are blocking the localhost connection by disabling the firewall temporarily. (For OS-specific instructions, see sections below.)

Once you disable the firewall, navigate to localhost in a browser to check whether it connects. If localhost still refuses to connect, move on to other potential causes. Also, make sure to turn on the firewall once you have finished working with localhost.

Disable Firewall on Linux

The firewall configuration tool on Linux systems varies depending on the distro. While Ubuntu and Debian distributions utilize UFW, CentOS and RHEL systems use firewalld by default.

To disable the UFW firewall on Ubuntu/Debian, open the terminal window and run:

sudo ufw disable

Check the status by running:

sudo ufw status
Check ufw status on Ubuntu.Check ufw status on Ubuntu.

To disable firewalld on CentOS/RHEL, use the command:

sudo systemctl stop firewalld

Verify the status is inactive by running:

sudo systemctl status firewalld
Disable firewalld on CentOS to troubleshoot localhost error.Disable firewalld on CentOS to troubleshoot localhost error.

Disable the Firewall on Mac OS

1. Click the Apple icon in the upper-left corner to open the menu and select System Preferences.

Open System Preferences on Mac OS.Open System Preferences on Mac OS.

2. Navigate to Security and Privacy settings and move to the Firewall tab.

Open the firewall settings on Mac.Open the firewall settings on Mac.

3. If the lock at the bottom left of the pane is locked, click the icon to unlock the preference pane. Type in your password to confirm.

Unlock preference pane on Mac.Unlock preference pane on Mac.

4. Click Turnoff Firewall to complete the process.

Disable the Firewall on Windows

1. Open the Windows Control Panel and click the Windows Defender Firewall icon.

Open Windows Defender Firewall on Windows.Open Windows Defender Firewall on Windows.

2. Select Turn Windows Defender Firewall on or off from the menu on the left side of the window.

Modify the firewall settings on Windows.Modify the firewall settings on Windows.

3. Finally, check the Turn off Windows Defender Firewall checkbox and click OK.

Turn off Windows Defender Firewall.Turn off Windows Defender Firewall.

Check Localhost IP Address

Localhost and the IP address 127.0.0.1 are not completely synonymous. In most cases, 127.0.0.1 is used for loopback, however not exclusively. For instance, IPv6 systems link the localhost to the address : :1.

Additionally, if the host file has been modified, the localhost could link to a different IP address.

Note: Learn more about the differences between localhost and 127.0.01 in our article Localhost vs. 127.0 0.1.

Try typing both localhost and 127.0.0.1 in the browser search bar, in case of a connection error.

Check Apache Server Status

Another important factor for accessing the localhost is that the Apache server is running properly. Therefore, if the system rejects the connection, it is necessary to check Apache’s status.

The command for verifying whether Apache is running on a Linux OS depends on the distribution in use. Follow the steps below and use the appropriate command.

1. Verify Apache status with the command:

Ubuntu/Debian:

sudo systemctl status apache2

CentOS/RHEL:

sudo systemctl status httpd

The output should display that Apache is active (running), as in the image below. If that is the case, the Apache server is working correctly and you can move to the next probably cause for error.

Check Apache status on Ubuntu-Check Apache status on Ubuntu-

2. If Apache is not running properly, the output displays that its status is inactive (dead).

Apache status showing an inactive server.Apache status showing an inactive server.

3. To fix the issue, try restarting the Apache service using:

Ubuntu/Debian:

sudo systemctl restart apache2

CentOS/RHEL:

sudo systemctl restart httpd

4. Check the Apache status again:

Ubuntu/Debian:

sudo systemctl status apache2

CentOS/RHEL:

sudo systemctl status httpd

5. If the status remains inactive, move on to deleting and reinstalling Apache on the system:

Flush the DNS

DNS cache is stored to speed up loading of previously visited websites. If DNS collects too many records or some of the records get corrupt, it may fail to function. Such an issue can result in localhost refusing to connect.

The best way to ensure DNS is not preventing the localhost connection is to clear the DNS cache and delete all saved DNS lookup information.

The steps for flushing the DNS vary depending on the OS in use. For detailed instructions, refer to How to Flush DNS Cache in macOS, Windows, & Linux.

Linux:

sudo /etc/init.d/nscd restart

MacOS:

sudo killall -HUP mDNSResponder

Windows:

ipconfig /flushdns

Change Port Settings

By default, localhost uses port number 80. If another application is using the same port, it can create a conflict resulting in localhost refusing to connect.

Change Port Number on XAMPP

If you are using the XAMPP stack, follow the steps below:

1. Open the XAMPP control panel and click on the Netstat button.

Open Netstat on XAMPP control panel.Open Netstat on XAMPP control panel.

2. Netstat opens a list of all TCP listening sockets. If port 80 is already in use by another application, move on to choosing a free port for Apache.

3. Before proceeding to the next step, make sure to stop the Apache service.

Stop Apache service on XAMPP control panel.Stop Apache service on XAMPP control panel.

4. Locate and open the httpd.conf file. The location of the file varies depending on the OS:

  • Linux users will find it in bin/apache.
  • Windows stores the file in C:\xampp\apache\conf.
  • The path to the file on Mac OS is Applications/XAMPP/xamppfiles/etc/httpd.conf.

5. Find the following lines in the file:

Listen 80
ServerName localhost:80

6. Change the port number to any other free port. In this example, we change it to 8080.

Listen 8080
ServerName localhost:8080

7. Save and exit the file. Restart the Apache web server in the XAMPP control panel and try connecting to localhost again.

Change Port Number on WAMPP

If you are using the WAMP software stack and want to change the Apache port:

1. First, stop all instances running in WAMP with the End Task button.

2. Then, open the httpd.conf file located in C:\wamp\apache2\conf.

3. Locate the following lines in the file:

Listen 80
ServerName localhost:80

4. Change the port number to a free port (8080 for example).

Listen 8080
ServerName localhost:8080

5. Save the changes and exit the file.

Check Browser Settings

Make sure the browser is not responsible for causing the localhost error.

Start by navigating to localhost in a different browser. If the connection establishes, then the settings in the initial browser are the issue.

Often, browsers automatically redirect HTTP addresses to HTTPS causing the localhost refused to connect error.

Change DNS Settings in Chrome

1. To change the default setting on Chrome, paste the following URL in the address bar: chrome://net-internals/#hsts

2. Then, open the Domain Security Policy tab and scroll down to the Delete domain security policies section.

3. Add localhost as the domain name and click Delete.

Change Chrome settings to disable converting http addresses to https.Change Chrome settings to disable converting http addresses to https.

4. Restart the browser to apply the changes.

Change DNS Settings in Firefox

1. Open the Application Menu in the top right corner of the browser window and select Settings.

2. Navigate to Privacy & Security and scroll down to the HTTPS-Only Mode section.

Disable HTTPS-only mode on Firefox.Disable HTTPS-only mode on Firefox.

3. To apply the changes, reopen the browser.

Conclusion

After reading this article, you should know the steps to locate the cause and fix the localhost refused to connect error.

Another common issue that occurs when working locally on software stacks such as LAMP, WAMP, and others is the Access denied for user root@localhost error. Follow the link to troubleshoot Access denied for user root@localhost error in a few simple steps.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments