Localhost Refused to Connect – What This Error Means & How to Fix It
Table of Contents
Are you seeing a “localhost refused to connect” error message? While not so common on live websites, this error usually means your browser is unable to access a local web server.
It is a network connection error that typically happens in a local development environment when your system can’t reach the server software running on your machine. Common reasons include a stopped Apache web server, blocked ports, or incorrect firewall settings.
This article explains what causes the error and how to troubleshoot it effectively.
Key Takeaways
- The “localhost refused to connect” error appears when your browser is unable to reach a local server—usually because the server software is down, the specified port is blocked, or a firewall is interfering.
- Common fixes include checking whether your web server is running properly, flushing your DNS cache, and verifying your localhost IP address.
- Using the right URL format and making sure that no conflicting services are occupying the default port are also key steps.
What Does “Localhost Refused to Connect” Mean?
This error occurs when your browser tries to connect to localhost—which points to your local machine—but the connection fails. This typically means the server expected to respond at that network address isn’t available or is rejecting the connection.

Localhost (usually 127.0.0.1) is part of the loopback network interface used to simulate a live environment on your device. If something is misconfigured, your browser won’t be able to load the web page you’re developing or testing.
Common Causes of the “Localhost Refused to Connect” Error
Several local system or configuration issues can trigger this error. Understanding these most common causes can help you pinpoint the problem:
- Your local Apache or Nginx server isn’t running: If no web server is active on your machine, there’s nothing for the browser to connect to.
- Another program is using the same port: Other applications or dev tools may already be using port 80 or 8080, and cause a conflict.
- Your system firewall is blocking connections: Security software may interpret local requests as suspicious and block them.
- Localhost isn’t properly mapped: If your hosts file doesn’t map localhost to 127.0.0.1, the connection can’t be established.
- Corrupted DNS or browser cache: Old DNS records or cached files may interfere with how your browser handles local requests.
- Browser or proxy misconfiguration: Extensions or proxy settings may be intercepting requests to localhost or rerouting them incorrectly.
How to Fix the “Localhost Refused to Connect” Error
1. Make Sure Your Server (Apache, Nginx, etc.) Is Running
If no web server is running on your computer, your browser won’t receive a response. First, check the status of the Apache web server (or Nginx, if used):
- Ubuntu/Debian: sudo systemctl status apache2
- CentOS/Red Hat: sudo systemctl status httpd
- Windows (XAMPP): Open the XAMPP Control Panel window and look for Apache’s status
If it’s not active, start the server:
- Linux: sudo systemctl start apache2 or httpd
- Windows: Click “Start” next to Apache in XAMPP
If Apache fails to start, inspect your httpd.conf or apache2.conf file for syntax errors or port conflicts. Also make sure the config file includes a valid Listen directive (e.g., Listen 80).
2. Temporarily Disable Your Firewall or Antivirus
Security software and network settings can block your local web server from accepting connections. To rule this out:
- Disable your firewall temporarily:
- Windows: Go to Control Panel > Windows Defender Firewall. Turn Windows Defender Firewall Off for private networks.
- macOS: Go to System Settings > Network > Firewall and disable it.
- Linux: Run the following command: sudo ufw disable
- Windows: Go to Control Panel > Windows Defender Firewall. Turn Windows Defender Firewall Off for private networks.
- Try accessing http://localhost in your browser.
- If it works, the issue is likely caused by your firewall preventing Google Chrome or other apps from accessing local ports.
- Re-enable the firewall once testing is complete, and create a rule to allow localhost connections if needed.
Also check for antivirus software that may block web traffic or sandbox browser activity.
3. Verify the Localhost IP Address (127.0.0.1)
On all systems, localhost should resolve to 127.0.0.1. You can test this by:
- Opening a browser and entering 127.0.0.1 directly in the address bar
- Running ping localhost or ping 127.0.0.1 in a terminal or command prompt
If the ping fails or shows a different address, check your hosts file:
- Windows: Use Notepad to open the directory C:\Windows\System32\drivers\etc\hosts
- macOS/Linux: Use the Terminal to open /etc/hosts
Add this line if it’s missing or commented out:
127.0.0.1 localhost
Save the change, restart your browser and flush DNS afterward.
4. Flush Your DNS Cache
Although localhost shouldn’t rely on DNS, a corrupted DNS cache can interfere with local name resolution or redirect requests unexpectedly.
- Windows: Open the Start Menu, search for “cmd,” then run: ipconfig /flushdns
- macOS: In Terminal: sudo killall -HUP mDNSResponder
- Linux: Try sudo systemd-resolve –flush-caches or restart the nscd/dnsmasq service
This clears records stored in your DNS cache, which may help resolve misconfigured internet lookups affecting localhost.
5. Check Port Configuration
Make sure your web server is listening on the correct port. Use these commands to identify active ports:
- netstat -na | find “LISTEN” (Windows)
- ss -tuln (Linux/macOS)
If port 80 (the default port number) is in use by another service, change the Listen directive in your Apache or Nginx config to a free port number, such as 8080 or 8888.
Then restart the server and access the page using http://localhost:8080.
6. Use the Correct URL Format (With Port)
Ensure your browser is using the proper http web address to access the local server. If the server is set to a non-default port, include it explicitly in the URL:
http://localhost:8080
Also check for typos like htp:// or localhst, which can trigger a refused to connect error or redirect to a live site instead of your local environment.
7. Adjust Browser Settings or Try a Different Browser
Browser extensions, cached data, or misconfigured proxy settings can block localhost. To test:
- Clear cache and cookies from your browser’s settings
- Check if a proxy is set (in Chrome: chrome://settings/system)
- Try accessing localhost from another browser like Firefox or Edge
- In Chrome, navigate to chrome://net-internals/#hsts and delete any domain security policies for localhost
If the same error appears across browsers, the problem likely lies outside the browser itself.
8. Check for Hardcoded localhost URLs in Themes or Plugins
If you’re seeing this error on a live SiteGround-hosted site, it may be caused by a theme or plugin trying to load a file or resource from http://localhost.
This usually happens when a developer hardcodes a localhost URL during local development (e.g. for a JavaScript file or API endpoint) and forgets to update it before moving the site live.
To check:
- Open the browser’s Developer Tools (right-click → Inspect → Console or Network tab)
- Look for failed requests to localhost or 127.0.0.1
- Update your theme or plugin code to remove or replace those URLs with the correct live site path
This is a browser-side error — your web hosting server isn’t refusing the connection. The request is being sent to the visitor’s own computer, which naturally doesn’t respond.
Alternative Method to Access Localhost
If localhost isn’t working, try accessing your project using the machine’s local internet address (private IP):
- Windows: ipconfig → Look for “IPv4 Address”
- macOS/Linux: ifconfig or ip a
Use the IP like this (replace XXX.XXX.X.XX with your IP address):
http://XXX.XXX.X.XX:8080
This can also help when testing from another device on the same network. Just make sure the device firewall settings allow incoming connections.
Final Tips to Avoid “Localhost Refused to Connect” in the Future
Once you’ve resolved the issue, here are a few tips to help prevent it from happening again:
- Always verify that your local web server is running before accessing localhost
- Use consistent and correct port numbers in your server configuration and URLs
- Check for any leftover localhost references before moving a site from local development to a live environment
- Regularly flush your DNS cache and browser cache if you notice connection errors
- Review your firewall settings if local connections suddenly stop working
If you’re using SiteGround for hosting, consider skipping complex local setups entirely by using our built-in Staging tool for WordPress sites, or a subdomain for testing. This usually eliminates the need to run a web server on your local computer.
Whether you’re just starting with local development or deploying live projects, understanding what triggers a “localhost refused to connect” error helps you solve it faster — and avoid it in the future.