Localhost Refused to Connect – How to Fix the Error

Introduction

If you are a web developer, you may have encountered the error message “localhost refused to connect” when trying to access a website or web application. This error can be caused by a variety of issues, including incorrect server settings, network issues, or a misconfigured firewall. In this article, we will discuss the causes of this error and how to fix it. We will also provide some tips on how to prevent this error from occurring in the future.

Localhost Refused to Connect – How to Fix the Error

1. Check your internet connection: Make sure your internet connection is working properly.

2. Check your firewall settings: Your firewall may be blocking the connection. Check your firewall settings and make sure the connection is allowed.

3. Check your browser settings: Make sure your browser is set to accept cookies and JavaScript.

4. Check your proxy settings: If you are using a proxy server, make sure it is configured correctly.

5. Restart your computer: Sometimes a simple restart can fix the issue.

6. Clear your browser cache: Clearing your browser cache can help resolve the issue.

7. Try a different browser: If the issue persists, try using a different browser.

8. Contact your ISP: If all else fails, contact your internet service provider for assistance.

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.

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.

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.

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

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

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.

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.

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.

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

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.

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-

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

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.

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.

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.

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.

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 [email protected] error. Follow the link to troubleshoot Access denied for user [email protected] error in a few simple steps.

Localhost Refused to Connect – How to Fix the Error

If you’re trying to access your localhost and you’re getting an error message that says “localhost refused to connect,” you’re not alone. This is a common problem that can be caused by a variety of issues. Fortunately, there are a few simple steps you can take to troubleshoot and fix the issue.

Check Your Internet Connection

The first step is to make sure your internet connection is working properly. If you’re using a wired connection, check the cables and make sure they’re securely connected. If you’re using a wireless connection, make sure your device is connected to the correct network and that the signal is strong. If your connection is working properly, move on to the next step.

Check Your Localhost Settings

The next step is to check your localhost settings. If you’re using a web server, make sure it’s running and that the port is open. If you’re using a database, make sure it’s running and that the port is open. If everything looks good, move on to the next step.

Check Your Firewall Settings

The next step is to check your firewall settings. If you’re using a firewall, make sure it’s not blocking access to your localhost. If you’re using a router, make sure it’s not blocking access to your localhost. If everything looks good, move on to the next step.

Check Your Hosts File

The next step is to check your hosts file. This file contains a list of IP addresses and domain names that your computer uses to access websites. If the IP address or domain name for your localhost is not listed, add it to the file and save the changes. If everything looks good, move on to the next step.

Restart Your Computer

The last step is to restart your computer. This will reset any settings that may have been changed and can help resolve the issue. Once your computer has restarted, try accessing your localhost again. If the issue is still present, contact your internet service provider or system administrator for further assistance.

By following these steps, you should be able to fix the “localhost refused to connect” error. If you’re still having trouble, contact your internet service provider or system administrator for further assistance.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?