Apache2 Problems
From: https://upcloud.com/resources/tutorials/fix-common-problems
-apache2
How to fix common problems with Apache2
Apache2
Problems with loading a website are often blamed on the Internet connection,
but even the most perfectly set up network cannot help if there is no
service to reply 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 easy installation and use, but never the less it is
possible to run into problems with even the easiest of the software. If
you’ve encountered an issue loading your web page, follow these simple
troubleshooting methods outlined in this guide to attempt to get your web
server back up and working again.
Try UpCloud for free!
Make sure the service is running
The first step to take in troubleshooting any service is to check that the
service is running and able to function. A straightforward approach is to
simply restart the service. On Ubuntu and Debian servers use the following
command.
sudo systemctl restart apache2
In CentOS and other Red Hat environments Apache2 service goes by the name
‘httpd’, so use this command instead.
sudo systemctl restart httpd
Depending on your choice of distribution the output from the restart command
will vary. Subjectively Ubuntu usually gives the most helpful reply as shown below.
* Restarting web server apache2 [ OK ]
CentOS and Debian probably do not say anything as long as the restart
didn’t fail.
If your system wasn’t able to restart Apache2 you most likely got some
form of an error message. In case you got a reply resembling either one of
the output examples below, the service is most likely not properly installed
on your system or some files are missing.
apache2: unrecognized service
Failed to restart apache2.service: Unit apache.service failed to load: No
such file or directory.
Should you see these type 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 next command.
sudo yum install httpd
Once you are sure the web server is fully installed go ahead and restart the
service again with the same command you used before.
If you got a different error message, try to get more information on what
kind of state your web service is in. Use one of the following commands
applicable to your system.
sudo systemctl status apache2
sudo systemctl status httpd
The output from the status check will tell at least whether the service is
running or stopped. Debian and CentOS systems usually show more detailed
report including service up time and a couple of log lines. Below is an
example of a Debian status display, one on CentOS would be nearly 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.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 the
process should be working, if instead it shows active and stopped or failed,
the process has most likely crashed.
Next check the processes by their name for either apache2 or httpd.
sudo ps aux | grep -E 'apache2|httpd'
For every mention of the searched keyword grep will print out a line the
keyword was found on, this includes the search process itself so you will
see at least the grep command. If there are more than one line of output,
all but the last are processes related to your web service. An example below
displays 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 earlier, any processes grep
might list have probably stopped functioning and need to be closed 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 is the web service called something else but also
the kill command functions 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 apache2
sudo systemctl status httpd
This should get the web service running provided that it has not been
misconfigured.
Check your server configuration
When starting the service fails giving errors referring to files located in
either /etc/apache2 or /etc/httpd/, the system had trouble reading the
service configuration files. Apache2 comes with some handy tools for file
integrity and syntax checks that can help in locating any typing mistakes or
other irregularities in the configuration. With Debian and Ubuntu servers,
check the config files by running the following command.
sudo apache2ctl -t
On CentOS machines just call httpd instead.
sudo httpd -t
The output will show any problems found with the configuration file. Or if
everything is in order, it simply prints out a confirmation like the one
shown below.
Syntax OK
Another troubleshooting option for the web service is to show parsed virtual
host and run settings with commands for Debian/Ubuntu and CentOS
respectively.
sudo apache2ctl -S
sudo httpd -S
Below is an example of the command output from a CentOS system. Make sure
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 your server uses a custom virtual host configuration, like when hosting
multiple websites on one server, check that each virtual host file has the
correct domain name and points to the correct root directory. On Debian and
Ubuntu machines have a virtual host file by default and it is stored in
/etc/apache2/sites-enabled/. Open the file for edit.
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 left out in the example below, but the important
parts are ServerName and DocumentRoot as already mentioned.
ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Include conf-available/serve-cgi-bin.conf
CentOS and httpd do not have the same virtual host file set by default but
instead uses the httpd service configuration to store the default settings.
Check the configuration file with the command below.
sudo vi /etc/httpd/conf/httpd.conf
Look for the same parameters ServerName and DocumentRoot, and make sure they
are correctly set.
If you made any changes to the configuration files, the service needs to be
reloaded for the changes to take an effect. Restarting the service does the
job, but if you wish to avoid downtime on your web server use reload instead
with one of the following commands.
sudo systemctl reload apache2
sudo systemctl reload httpd
Check Logs
When everything on the service side is working as expected and you cannot
find a fault, but the website just still won’t load, it’s always a good
time to dig through logs. Apache2 keeps two sets of logs, access and error.
You can find the logs stored at /var/log/apache2/ or /var/log/httpd
depending on your choice of Linux distribution. You can list all files in
your web server’s log directory using the commands below.
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 the current uptime logs
to access.log or error.log, previous logs are marked with a running number,
1 being the latest, and older logs than that are also compressed. On CentOS
and other Red Hat variants, the logs are named access_log and error_log,
older logs have their name appended with the date the log was written on
e.g. access_log-20150108.
An easy way to start reading the logs, when you don’t necessarily know
what you are looking for, is to use the filtering app ‘grep’. Search for
any errors using one of the commands below which corresponds with your
system’s web application name.
sudo grep -i -r error /var/log/apache2/
sudo grep -i -r error /var/log/httpd/
Ubuntu and Debian users might also need to check through the compressed log
files. This can be done using ‘zgrep’ instead, but due to its
limitations, you can only search one log at the time, for example using this
following command.
sudo zgrep error /var/log/apache2/error.log.2.gz
Not all errors logged by your web service necessarily mean there is
something wrong with your server, but keep an eye out for any repeating
problems 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 unable to start
You may also wish to enable more verbose logging if searching for errors is
not yielding any results. The log output amount is controlled by a parameter
called ‘LogLevel’ in the configuration file. On Debian and Ubuntu
systems, open your configuration file with the following command.
sudo nano /etc/apache2/apache2.conf
With CentOS and other Red Hat based server use the command here instead.
sudo vi /etc/httpd/conf/httpd.conf
Find the LogLevel parameter and change it from the default ‘warn’ value to
‘debug’ instead, then save the file and exit. Again for any changes to be
applied, the service needs to be reloaded. You can do this with one of the
following commands appropriate for your system.
sudo systemctl reload apache2
sudo systemctl reload httpd
To generate some new entries using the new log levels, try to access your
website again a couple of times even if it doesn’t work. The more verbose
logs should help with narrowing down any underlying issues.
Check other services
If your website still won’t load after all the 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.
Do not hesitate to contact our support team. Remember to include any
relevant information you may have discovered while troubleshooting, as every
bit of detail, will be useful in further investigations.