How to fix common problems with Apache2 – UpCloud

Problems with loading a website are often attributed to the internet connection, but even the most perfectly configured network can’t help if there’s no service to respond at your destination. One of the most popular HTTP servers used for this task is Apache2. Much of Apache’s popularity can be attributed to its ease of installation and use, but it’s never less possible to have problems with even the easiest software. If you’ve encountered a problem loading your web page, follow these simple troubleshooting methods described in this guide to try to get your web server up and running again.

Try UpCloud for free! Deploy a server in as little as 45 seconds

Make sure the service is running The first step in troubleshooting any service is to verify that

the service is running

and can work. A simple approach is to simply restart the service. On Ubuntu and Debian servers use the following command.

sudo systemctl restart apache2

On CentOS and other Red Hat environments, the Apache2 service goes

by the name ‘httpd’, so use this command instead. sudo systemctl restart httpd

Depending on your choice of distribution, the output of the restart command will vary. Subjectively, Ubuntu usually gives the most useful answer as shown below.

* Reboot the apache2 web server [ OK ]

CentOS and Debian probably don’t say anything as long as the reboot doesn’t fail

.

If your system failed to restart Apache2, you most likely received some sort of error message. In case you have received a response similar to any of the output examples below, most likely the service is not installed properly on your system or some files are missing.

apache2: service not recognizedError restarting apache2.service: apache.service drive could not be loaded: There is no such file or directory.

If you see these types of errors, try installing the service again. On servers running Debian or Ubuntu use the following command.

sudo apt-get install apache2

And on CentOS

install httpd instead with the following command. sudo yum install httpd

Once you are sure that the web server is fully installed, go ahead and restart the service again with the same command you used before

.

If you receive a different error message, try learning more about the type of state your web service is in. Use one of the following commands applicable to your system.

sudo systemctl status apache2sudo systemctl status httpd

The result of the health check will at least indicate whether the service is running or stopped. Debian and CentOS systems generally display more detailed reports that include service uptime and a couple of log lines. Below is an example of a Debian state visualization, one on CentOS would be almost identical. Ubuntu does not have this type of output by default.

● apache2.service – LSB: Apache2 web server Loaded: loaded (/etc/init.d/apache2) Active: active (running) since Fri 2015-07-31 10:44:26 EEST; 2h 14min ago Process: 9704 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS) Process: 9711 ExecStart=/etc/init.in.d/apache2 start (code=exited, status=0/SUCCESS) CGroup: /system.slice/apache2.service ├─9726 /usr/sbin/apache2 -k start ├─9730 /usr/sbin/apache2 -k start ├─9731 /usr/sbin/apache2 -k start ├─9732 /usr/sbin/apache2 -k start ├─9733 /usr/sbin/apache2 -k start ├─9734 /usr/sbin/apache2 -k start └─9747 /usr/sbin/apache2 -k start Jul 31 10:44:26 debian.example.com apache2[9711]: Starting web server: apache2.

The important part here is on the third line, active and running means that the process should be running, if instead it shows active and stopped or failed, most likely the process has crashed.

Then check the processes by name for apache2 or httpd.

sudo ps aux | grep -E ‘apache2|httpd’For each mention of the searched keyword, grep will

print a line on which the keyword was found, this includes the search process itself, so you will see at least the grep command. If there is more than one output line, all but the last one are processes related to your web service. An example below shows apache2 processes running on Ubuntu.

root 1457 0.0 1.5 321908 16132 ? Ss Jul30 0:02 /usr/sbin/apache2 -k start www-data 1461 0.0 2.8 326532 29172 ? S Jul30 0:00 /usr/sbin/apache2 -k start www-data 1462 0.0 3.1 327480 32364 ? S Jul30 0:00 /usr/sbin/apache2 -k start www-data 1463 0.0 2.9 326688 30260 ? S Jul30 0:00 /usr/sbin/apache2 -k start www-data 1464 0.0 3.1 326496 32148 ? S Jul30 0:00 /usr/sbin/apache2 -k start www-data 1465 0.0 2.7 326816 28040 ? S Jul30 0:00 /usr/sbin/apache2 -k start www-data 1841 0.0 2.0 323132 21044 ? S Jul30 0:00 /usr/sbin/apache2 -k start www-data 1871 0.0 2.2 323396 23280 ? S Jul30 0:00 /usr/sbin/apache2 -k start user 11669 0.0 0.0 11744 928 pts/0 S+ 15:32 0:00 grep -color=auto -E apache2|httpd Since the service

restart command didn’t work before, any list of processes grep might list has probably stopped working and needs to be shut down before the service can start again. On Debian and Ubuntu systems stop all apache2 processes using this command.

sudo killall apache2

On CentOS servers not only the web service is called something else, but also the kill command works little differently, use the following instead for the same result

. sudo kill -a httpd

After the kill command, you can run the process check again just to confirm that there are no more zombies left. Then, try to restart the service using these same commands as at the beginning of this guide.

sudo systemctl status apache2sudo systemctl status httpd

This should cause the web service to run as long as it has not been configured incorrectly

.

Check

the server configuration

When starting the service fails giving errors that refer to files located in /etc/apache2 or /etc/httpd/, the system had problems reading the service configuration files. Apache2 comes with some useful tools for file integrity and syntax checks that can help locate any typing errors or other irregularities in the configuration. With Debian and Ubuntu servers, check the configuration files by running the following command.

sudo apache2ctl -t

On CentOS machines simply call

httpd instead. sudo httpd -t

The output will show any problems encountered with the configuration file. Or if everything is in order, simply print a confirmation like the one below.

OK Syntax

Another troubleshooting option for the web service is to display the parsed virtual host and run the configuration with commands for Debian/Ubuntu and

CentOS respectively. sudo apache2ctl -Ssudo httpd -S

Below is an example of command output from a CentOS system. Make sure that the server and document roots point to the correct directories.

VirtualHost Configuration: ServerRoot: “/etc/httpd” Main DocumentRoot: “/var/www/html” Main ErrorLog: “/etc/httpd/logs/error_log” Mutex proxy: using_defaults Mutex authn-socache: using_defaults Mutex default: dir=”/run/httpd/” mechanism=default Mutex mpm-accept: using_defaults Mutex authdigest-opaque: using_defaults Mutex proxy-balancer-shm: using_defaults Mutex rewrite-map: using_defaults Mutex authdigest-client: using_defaults PidFile: “/run/httpd/httpd.pid” Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name=”apache” id=48 Group: name=”apache” id=48 If the server uses a custom virtual host

configuration, such as when hosting multiple websites on a server, verify that each virtual host file has the correct domain name and points to the correct root directory. On Debian and Ubuntu machines they have a virtual host file by default and it is stored in /etc/apache2/sites-enabled/. Open the file for editing.

sudo nano /etc/apache2/sites-enabled/000-default.conf

This file usually has some instructions on what each parameter means in the comments, which have been omitted in the following example, but the important parts are ServerName and DocumentRoot as already mentioned.

<VirtualHost *:80> ServerName www.example.com ServerAdmin [email protected] DocumentRoot /var/www/html #LogLevel ssl info: warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined #Include conf-available/serve-cgi-bin.conf </VirtualHost>

CentOS and httpd do not have the same virtual host file set by default, but use the httpd service settings to store the default settings. Verify the configuration file with the following command.

sudo vi /etc/httpd/conf/httpd.conf Look for the

same ServerName and DocumentRoot parameters, and make sure they are configured correctly

.

If you made any changes to the configuration files, the service must be reloaded for the changes to take effect. Restarting the service does the job, but if you want to avoid downtime on your web server, use reload instead with one of the following commands.

sudo systemctl reload apache2sudo systemctl reload httpd

Check

logs

When everything on the service side works as expected and you can’t find a fault, but the website just won’t load, it’s always a good time to search the logs. Apache2 maintains two sets of logs, access and error. You can find the logs stored in /var/log/apache2/ or /var/log/httpd depending on your choice of Linux distribution. You can list all the files in the log directory of your web server using the following commands.

sudo ls /var/log/apache2/sudo ls /var/log/httpd/

The log lists will differ slightly as different systems name the logs a little differently. Ubuntu and Debian servers store current uptime logs for access.log or error.log, previous logs are marked with a run number, 1 being last, and records older than that are also compressed. On CentOS and other Red Hat variants, records are called access_log and error_log, older records have their name appended with the date the record was written, for example, access_log-20150108.

An easy way to start reading logs, when you don’t necessarily know what you’re looking for, is to use the ‘grep’ filtering app. Look for any errors using one of the following commands that corresponds to the name of your system’s web application.

sudo grep -i -r error /var/log/apache2/sudo grep -i -r error /var/log/httpd/

Ubuntu and Debian users may also need to review the compressed log files. This can be done using ‘zgrep’ instead, but due to its limitations, you can only search for one record at a time, for example, using the following command.

sudo zgrep error /var/log/apache2/error.log.2.gz

Not every error logged by your web service necessarily means that there is something wrong with your server, but keep an eye out for any repetitive issues like missing files in the example error below

. [Fri Jul 17 12:36:08.431065 2015] [:error] [PID 4649] [client 80.69.160.92] script ‘/var/www/html/index.php’ not found or could not be started

You may also want to enable more detailed logging if the error search does not produce any results. The amount of log output is controlled by a parameter named ‘LogLevel’ in the configuration file. On Debian and Ubuntu systems, open the configuration file with the following command.

sudo nano /etc/apache2/apache2.conf

With CentOS and other Red Hat-based servers, use the command here instead

. sudo vi /etc/httpd/conf/httpd.conf

Find the LogLevel parameter and change it from the default ‘warn’ to ‘debug’ instead, then save the file and exit. Again, for the changes to take effect, the service needs to be reloaded. You can do this with one of the following commands appropriate for your system.

sudo systemctl reload apache2sudo systemctl reload httpd

To generate some new entries using the new logging levels, try accessing your website again a couple of times, even if it doesn’t work. More detailed logs should help reduce any underlying problems.

Check

other services If your

website still doesn’t load after all troubleshooting with Apache, check other related services on your cloud server, such as databases, firewall, and network connection, which also have their own troubleshooting instructions

.

Feel free to contact our support team. Remember to include any relevant information you may have discovered during troubleshooting, as every detail will be useful in future research.