Ugacomp

How to troubleshoot Apache “Connection refused” error?

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

The “Connection Refused” error occurs when the client cannot establish a connection with the server. This can be due to several reasons, and in this comprehensive guide, we’ll explore the various reasons behind this error and provide step-by-step troubleshooting solutions.

Step 1: Verify Apache is Running

The first step in troubleshooting any Apache-related issue is to ensure that the Apache server is running. Use the following command to check the status:

sudo systemctl status apache2

If Apache is not running, start it with by running the following command:

sudo systemctl start apache2

Step 2: Check Firewall Settings

Firewall can determine how traffic comes in and out of your server. For example, it can block incoming connections to the Apache server. Now, you need to verify that the necessary ports (usually 80 for HTTP and 443 for HTTPS) are open through the firewall. Use the following commands to check and modify firewall settings:

sudo ufw status
sudo ufw allow 80
sudo ufw allow 443

How to install and configure UFW firewall on Ubuntu Linux

Step 3: Confirm the Apache Listening Port

By default, Apache server listens on port 80 for HTTP and Port 443 for HTTPS traffic. These ports are critical if you want your website to be accessed on the internet by users.

So, to check and verify which port Apache is listening to, you can use one of the following methods:

Method #1: Use the netstat command.

Open a terminal and run the following command:

netstat -anp | grep apache

This will list all of the network ports that are currently open, along with the processes that are listening on them. Look for the line that says LISTEN and has the word apache in it. The port number will be listed in the Local Address column.

Here is an example of the output of the netstat command, showing that Apache is listening on port 80:

tcp6 0 0 :::80 :::* LISTEN 113959/apache2

Method #2: Use the lsof command.

Open a terminal and run the following command:

lsof -i:80 | grep apache

This will list all of the processes that are listening on port 80, which is the default port for Apache. Look for the line that says LISTEN and has the word apache in it. The port number will be listed in the COMMAND column.

Here is an example of the output of the lsof command, showing that Apache is listening on port 80:

apache2 113959 root 4u IPv6 3543809823 0t0 TCP *:80 (LISTEN)

RECOMMENDED READING: How to use the apt command in Linux | Syntaxes & Examples

Edit the Apache Configuration file

Editing the apache configuration file could be one of the ways you should explore to troubleshoot the connection refused error on your server. The Apache configuration file is usually located at /etc/apache2/apache2.conf or /etc/httpd/httpd.conf. Look for the Listen directive and verify the port number.

grep -i 'Listen' /etc/apache2/apache2.conf

If the port is different or not specified, update the configuration file and restart Apache:

sudo nano /etc/apache2/apache2.conf

The port number will be listed next to the Listen directive. Here is an example of the Apache configuration file, showing that Apache is listening on port 80:

Listen 80

Step 4: Investigate Server Load

High server load can lead to connection issues. Use the top command to check server resource usage.

To use the top command, simply open a terminal window and type top. This will display a list of all running processes, sorted by CPU usage. The top of the list will show the processes that are using the most CPU time.

The following is an example of the output of the top command on a Linux terminal:

top - 10:28:39 up 1 day, 14:47,  1 user,  load average: 0.01, 0.02, 0.05
Tasks: 185 total,   2 running,  183 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.1 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  158944 total,   158636 free,    1488 used,    1320 cached
KiB Swap:   102396 total,   102396 free,      0 used.   105640 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
    1 root      20   0     1240     340     248 S  0.0  0.2   0:00.01 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
    4 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 watchdog/0
    5 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 khelper
    6 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
    7 root      20   0       0      0      0 S  0.0  0.0   0:00.00 net/x
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    9 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 mm_percpu
   10 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs/0
   11 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_sched
   12 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
   13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khelper/0
   14 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 watchdog/1
   15 root      20   0       0      0      0 S  0.0  0.0   0:00.00 migration/1
   16 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
   17 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 mm_percpu
   18 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs/1
   19 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_sched
   20 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh

The top command displays several useful metrics, such as:

  • Total CPU usage: The percentage of CPU time that is being used by all processes.
  • Free memory: The amount of memory that is not currently being used by any processes.
  • Buffers: The amount of memory that is being used by the kernel to buffer disk I/O.
  • Cached: The amount of memory that is being used to cache recently used files.
  • Swap: The amount of swap space that is being used.

RECOMMENDED READING: How to host a Laravel application on Ubuntu using Apache

Step 5: Examine Apache Log Files

The apache log files can help you to review aany issues affecting your server’s connection. The logs are typically located in /var/log/apache2/error.log or /var/log/httpd/error_log. Look for entries related to the “Connection Refused” error:

tail -f /var/log/apache2/error.log

Step 6: Test Network Connectivity

Ensure that there are no network issues preventing communication between the client and the server. Ping the server’s IP address to check for connectivity:

ping your_server_ip

What is ‘Reachable’ and ‘Unreachable’ host?

A reachable destination in Ping means that the source computer is able to send ping requests to the destination host (your server) and receive responses. This indicates that the destination host is turned on and connected to the network and that there is a valid route between the source computer and the destination host.

RECOMMENDED READING: How to deploy WordPress on a Ubuntu LAMP Server

Here is a sample output for a ping test on a reachable destination host: In this example, we’re using Google’s Public DNS server:

C:\WINDOWS\System32>ping 8.8.8.8

Pinging 8.8.8.8 with 32 bytes of data:

Reply from 8.8.8.8: bytes=32 time=17ms TTL=51
Reply from 8.8.8.8: bytes=32 time=18ms TTL=51
Reply from 8.8.8.8: bytes=32 time=19ms TTL=51
Reply from 8.8.8.8: bytes=32 time=20ms TTL=51

Ping statistics for 8.8.8.8:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 17ms, Maximum = 20ms, Average = 18ms

The above results mean the following:

  • Pinging 8.8.8.8 with 32 bytes of data: This line shows that we are sending ping requests to the IP address 8.8.8.8, which is the Google Public DNS server. The packet size is 32 bytes.

  • Reply from 8.8.8.8: bytes=32 time=17ms TTL=51: This line shows that we received a reply from the destination host. The reply packet size is 32 bytes, and the round trip time (RTT) is 17 milliseconds. The TTL (time to live) is 51, which means that the packet can be forwarded up to 51 times before it is discarded.

RECOMMENDED READING: How to Install WordPress on a VPS Server using Cloudron?

  • Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 17ms, Maximum = 20ms, Average = 18ms: This section summarizes the results of the ping test. It shows that we sent 4 ping requests and received all 4 replies, so there was no packet loss. The average RTT is 18 milliseconds, which is a good response time.

On the other hand, an unreachable destination in Ping means that the source computer is unable to send ping requests to the destination host or receive responses. This can be caused by a number of factors, such as:

  • The destination host is turned off or offline.
  • The destination host is blocking ping requests.
  • There is a problem with the network between the source computer and the destination host.
  • There is no valid route between the source computer and the destination host.

Here is an example of a ping result for an unreachable destination:

C:\WINDOWS\System32>ping 192.168.1.1

Pinging 192.168.1.1 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.168.1.1:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

In this example, all four ping requests timed out, which means that the destination host was unreachable.

The ping results will vary depending on the specific reason for the unreachability. However, some common ping results for unreachable destinations include:

  • Request timed out. This means that the ping request did not receive a response from the destination host within a certain amount of time. This can be caused by a number of factors, such as a network outage, a problem with the destination host, or a firewall blocking the ping requests.

  • Destination host unreachable. This means that there is no valid route to the destination host. This can be caused by a number of factors, such as a misconfigured router, a network outage, or the destination host being offline.

  • Unknown host. This means that the ping request could not resolve the destination hostname to an IP address. This can be caused by a problem with the DNS server, or by the destination host not having a valid DNS record.

If you are unable to ping a destination host, it is important to troubleshoot the problem to determine the cause of the unreachability. This may involve checking the network connection, verifying that the destination host is turned on and connected to the network, and checking that there is a valid route between the source computer and the destination host.

RECOMMENDED READING: How can I simulate traffic on a Linux server using Apache Bench?

Step 7: Check Virtual Host Configurations

If you’re hosting multiple websites on the same Apache server, ensure that virtual host configurations are correct. Misconfigured virtual hosts can lead to connection issues. Review the virtual host files located in /etc/apache2/sites-available/ or /etc/httpd/conf.d/.

Conclusion

The “Connection Refused” error in Apache can be caused by various factors, from server misconfigurations to network issues. This guide provides a systematic approach to troubleshooting the error, covering key aspects such as Apache’s status, firewall settings, port configurations, server load, log files, network connectivity, and virtual host configurations.

More Apache-related articles to explore

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.