Fix cannot connect to Apache/httpd server after restart

Solved and Fixed: If you have restarted your server and it is failing to connect to the Apache/Httpd server there can be few reasons. In this post, I’ll cover the steps to troubleshoot the connectivity issue.

How to troubleshoot Apache not working?

Check Apache/HTTPD is running

The first troubleshooting is to find out whether Apache is running or not. We can use the systemctl command to find out.

systemctl status httpd

This commands displays the current status of the Apache server

On the highlighted number we can confirm that Apache is active and running. If the active status is not running, then we need to start the service.

systemctl start httpd
systemctl enable httpd.service

View the error log file

If the server is running and you’re still unable to connect, log file becomes handy.

tail -f -n 20 /var/log/httpd/error_log

Allow http,https to be open in the Firewall

We can utilise firewalld. Firewalld is a firewall management software which is available on most Linux OS including AWS AMI images.

List out all blocked/opened ports

firewall-cmd --list-all

Add http,https to the allowed list

firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --zone=public --add-service=https --permanent
firewall-cmd --reload

Now, we are confident we have allowed http and https in the allow list of our firewall. Usually this gets blocked by another program or you might have accidentally added it.

Verify the VirtualHost configuration

Virtualhost configurations can sometimes include an incorrect domain address, with the help of Apache DUMP_VHOSTS command we can display all site domains and their config file.

httpd -T -D DUMP_VHOSTS

apache display virtualhost with the website addresses

Perform a curl to localhost

Now, that we have verified the server is running and the port are on the allowed list of the firewall. It is time to execute a curl request to localhost. If we receive a response this means we can communicate the server internally and there are external factors that is blocking the connection.

curl localhost

Edit local hosts file on the server

This is a technique which can help you to verify the VirtualHost configuration file is working 100%. Over here we modify the /etc/hosts file on the server and then execute a curl request.

  1. vim /etc/hosts
  2. Enter 127.0.0.1 www.example.com
  3. execute curl www.example.com

When we run the curl command the machine will check for the website address in the hosts file first, once it founds it it will request the IP 127.0.0.1 which is server local IP. This should return a result, if it doesn’t then it is likely a configuration in Apache is incorrect.

Check if port 80/443 is allowed by our cloud provider security group

Cloud Providers offer a protection layer called Security Groups. Security groups determine which ports can be accessible. On AWS we can use VPC Security groupĀ  and on Azure Network security groupĀ 

Summary

The above steps goes through different methods to help you discover the connectivity issue. Ideally you don’t need to do these stuff manually.

It is a good industry practice to host your applications using Docker images or provision the servers via Ansible and Packer. The techniques mentioned here can also be applied to a Nginx server with minor modifications.

Drop a comment if you are experiencing an issue or you think three is a missing step.