
macOS, Apache and ERR_CONNECTION_REFUSED fix
Fix ERR_CONNECTION_REFUSED on macOS: Learn how to delete the Apache PID file to resolve server startup issues.
The ERR_CONNECTION_REFUSED error is a common issue that occurs when a website or local server is inaccessible. This error indicates that the Apache server has refused the connection attempt, and it can be caused by various factors.
One possible cause is a hard shutdown of a macOS computer. When a macOS computer is forcibly turned off, all running processes, including Apache, are immediately terminated. This is because a hard shutdown bypasses the normal shutdown procedure, which allows the operating system to properly close running applications and services. As a result, Apache may not be able to restart automatically, leading to a connection refusal when you try to access the server again.
This usually appears like This site can’t be reached
and looks like on the following screenshot:

Fixing Apache ERR_CONNECTION_REFUSED error on macOS
If the Apache server is not starting up properly, one potential solution is to delete the Apache PID file. This can help resolve issues where the server is unable to start because the PID file is corrupted or points to a non-existent process.
After deleting the PID file, you can then try to restart the Apache server.
sudo rm /usr/local/var/run/httpd/httpd.pidUnderstanding the terminal command
The terminal command sudo rm /usr/local/var/run/httpd/httpd.pid performs several actions:
sudo: this prefix stands forsuperuser do
. It allows you to run commands with administrative privileges. This is necessary because removing files from certain directories typically requires higher permissions than those granted to regular users.rm: this is the remove command in Unix-like operating systems (macOS). It is used to delete files and directories./usr/local/var/run/httpd/httpd.pid: this specifies the path to the file that will be deleted. The path breaks down as follows:/usr/local: A standard directory where locally installed software and data reside./var/run: A temporary filesystem (tmpfs) that holds runtime data. Files in this directory are cleared on reboot.httpd: refers to the Apache HTTP Server.httpd.pid: this is the process ID file for the Apache HTTP Server. When Apache starts, it creates this file to store its process ID (PID). This file is used by system administrators to stop the server gracefully by sending a signal to the PID stored in this file.
Restarting Apache
Once you remove the process you need to restart the Apache process. For Apache managed by Brew use the following command: brew services restart httpd. Read more about how to restart Apache on macOS.
Summary
In summary, the command sudo rm /usr/local/var/run/httpd/httpd.pid uses superuser privileges to delete the Apache HTTP server’s PID file located in the /usr/local/var/run/httpd/ directory. This action can be taken to restart the Apache service by deleting the PID file, forcing the server to start anew upon the next request, or simply to clear out old PID files if they are no longer needed.
Comments