{"id":2443,"date":"2015-08-04T12:19:26","date_gmt":"2015-08-04T09:19:26","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/fix-common-problems-apache2\/"},"modified":"2015-08-04T12:19:26","modified_gmt":"2015-08-04T09:19:26","slug":"fix-common-problems-apache2","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/fix-common-problems-apache2\/","title":{"rendered":"How to fix common problems with Apache2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Problems 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. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Much of Apache\u2019s popularity can be attributed to its easy installation and use, but nonetheless, it is possible to run into problems with even the easiest of the software. If you\u2019ve 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.<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Make sure the service is running<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The first step in troubleshooting any service is to check that it is running and able to function. A straightforward approach is to restart the service. On Ubuntu and Debian servers, use the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart apache2<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In CentOS and other Red Hat environments, the Apache2 service is called \u2018httpd\u2019, so use this command instead.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart httpd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The restart command output will vary depending on your distribution choice. Subjectively, Ubuntu usually gives the most helpful reply, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">* Restarting web server apache2             [ OK ]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">CentOS and Debian probably do not say anything as long as the restart didn\u2019t fail.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your system couldn\u2019t restart Apache2, you most likely got an error message. If you got a reply resembling either of the output examples below, the service is most likely not properly installed on your system, or some files are missing.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apache2: unrecognized service<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Failed to restart apache2.service: Unit apache.service failed to load: No such file or directory.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you see these errors, try installing the service again. On servers running Debian or Ubuntu, use the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install apache2<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On CentOS, install httpd instead with the next command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install httpd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once you are sure the web server is fully installed, restart the service using the same command you used before.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you received a different error message, try obtaining more information on the state of your web service. Use one of the following commands that applies to your system.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl status apache2<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl status httpd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output from the status check will tell at least whether the service is running or stopped. Debian and CentOS systems usually show a more detailed report, including service uptime 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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u25cf apache2.service - LSB: Apache2 web server\n&nbsp;&nbsp;&nbsp; Loaded: loaded (\/etc\/init.d\/apache2)\n&nbsp;&nbsp;&nbsp; Active: active (running) since Fri 2015-07-31 10:44:26 EEST; 2h 14min ago\n&nbsp;&nbsp; Process: 9704 ExecStop=\/etc\/init.d\/apache2 stop (code=exited, status=0\/SUCCESS)\n&nbsp;&nbsp; Process: 9711 ExecStart=\/etc\/init.d\/apache2 start (code=exited, status=0\/SUCCESS)\n&nbsp;&nbsp;&nbsp; CGroup: \/system.slice\/apache2.service\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u251c\u25009726 \/usr\/sbin\/apache2 -k start\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u251c\u25009730 \/usr\/sbin\/apache2 -k start\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u251c\u25009731 \/usr\/sbin\/apache2 -k start\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u251c\u25009732 \/usr\/sbin\/apache2 -k start\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u251c\u25009733 \/usr\/sbin\/apache2 -k start\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u251c\u25009734 \/usr\/sbin\/apache2 -k start\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u2514\u25009747 \/usr\/sbin\/apache2 -k start\n Jul 31 10:44:26 debian.example.com apache2[9711]: Starting web server: apache2.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The important part here is on the third line, active and running, which means the process should be working; if instead it shows active and stopped or failed, the process will most likely crash.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, check the processes by their name for either apache2 or httpd.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ps aux | grep -E 'apache2|httpd'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For every mention of the searched keyword, grep will print out 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 line of output, all but the last are processes related to your web service. An example below displays apache2 processes running on Ubuntu.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1457&nbsp; 0.0&nbsp; 1.5 321908 16132 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ss&nbsp;&nbsp; Jul30&nbsp; 0:02 \/usr\/sbin\/apache2 -k start\nwww-data&nbsp; 1461&nbsp; 0.0&nbsp; 2.8 326532 29172 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S&nbsp;&nbsp;&nbsp; Jul30&nbsp; 0:00 \/usr\/sbin\/apache2 -k start\nwww-data&nbsp; 1462&nbsp; 0.0&nbsp; 3.1 327480 32364 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S&nbsp;&nbsp;&nbsp; Jul30&nbsp; 0:00 \/usr\/sbin\/apache2 -k start\nwww-data&nbsp; 1463&nbsp; 0.0&nbsp; 2.9 326688 30260 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S&nbsp;&nbsp;&nbsp; Jul30&nbsp; 0:00 \/usr\/sbin\/apache2 -k start\nwww-data&nbsp; 1464&nbsp; 0.0&nbsp; 3.1 326496 32148 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S&nbsp;&nbsp;&nbsp; Jul30&nbsp; 0:00 \/usr\/sbin\/apache2 -k start\nwww-data&nbsp; 1465&nbsp; 0.0&nbsp; 2.7 326816 28040 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S&nbsp;&nbsp;&nbsp; Jul30&nbsp; 0:00 \/usr\/sbin\/apache2 -k start\nwww-data&nbsp; 1841&nbsp; 0.0&nbsp; 2.0 323132 21044 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S&nbsp;&nbsp;&nbsp; Jul30&nbsp; 0:00 \/usr\/sbin\/apache2 -k start\nwww-data&nbsp; 1871&nbsp; 0.0&nbsp; 2.2 323396 23280 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S&nbsp;&nbsp;&nbsp; Jul30&nbsp; 0:00 \/usr\/sbin\/apache2 -k start\nuser&nbsp;&nbsp;&nbsp;&nbsp; 11669&nbsp; 0.0&nbsp; 0.0&nbsp; 11744&nbsp;&nbsp; 928 pts\/0&nbsp; S+&nbsp;&nbsp; 15:32  0:00 grep --color=auto -E apache2|httpd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since the service restart command didn\u2019t 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, all Apache2 processes are stopped using this command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo killall apache2<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo kill -a httpd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After the kill command, you can run the process check again to confirm that no more zombies are left. Then, try to restart the service using these same commands as at the beginning of this guide.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl status apache2<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl status httpd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This should get the web service running, provided that it has not been misconfigured.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\/\">Try UpCloud for free!<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Check your server configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When starting a service fails with 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. Check the config files with Debian and Ubuntu servers by running the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apache2ctl -t<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On CentOS machines, call httpd instead.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo httpd -t<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Syntax OK<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Another troubleshooting option for the web service is to show the parsed virtual host and run settings with commands for Debian\/Ubuntu and CentOS, respectively.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apache2ctl -S<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo httpd -S<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Below is an example of the command output from a&nbsp;CentOS system. Make sure the server and document roots point to the correct directories.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">VirtualHost configuration:\nServerRoot: \"\/etc\/httpd\"\nMain DocumentRoot: \"\/var\/www\/html\"\nMain ErrorLog: \"\/etc\/httpd\/logs\/error_log\"\nMutex proxy: using_defaults\nMutex authn-socache: using_defaults\nMutex default: dir=\"\/run\/httpd\/\" mechanism=default\nMutex mpm-accept: using_defaults\nMutex authdigest-opaque: using_defaults\nMutex proxy-balancer-shm: using_defaults\nMutex rewrite-map: using_defaults\nMutex authdigest-client: using_defaults\nPidFile: \"\/run\/httpd\/httpd.pid\"\nDefine: DUMP_VHOSTS\nDefine: DUMP_RUN_CFG\nUser: name=\"apache\" id=48\nGroup: name=\"apache\" id=48<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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. Debian and Ubuntu machines have a virtual host file by default, and it is stored in \/etc\/apache2\/sites-enabled\/. Open the file for editing.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/etc\/apache2\/sites-enabled\/000-default.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This file usually has some instructions on what each parameter means in the comments, which have been left out in the example below. However, as already mentioned, the important parts are ServerName and DocumentRoot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;VirtualHost *:80&gt;\n   ServerName www.example.com\n   ServerAdmin webmaster@localhost \n   DocumentRoot \/var\/www\/html\n   #LogLevel info ssl:warn      \n   ErrorLog ${APACHE_LOG_DIR}\/error.log\n   CustomLog ${APACHE_LOG_DIR}\/access.log combined\n   #Include conf-available\/serve-cgi-bin.conf\n&lt;\/VirtualHost&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">CentOS and httpd do not have the same virtual host file set by default but instead use the httpd service configuration to store the default settings. Check the configuration file using the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo vi \/etc\/httpd\/conf\/httpd.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Look for the same parameters, ServerName and DocumentRoot, and make sure they are correctly set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you made any changes to the configuration files, the service needs to be reloaded for the changes to take effect. Restarting the service does the job, but if you wish to avoid downtime on your web server, reload instead with one of the following commands.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl reload apache2<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl reload httpd<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Check Logs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When everything on the service side is working as expected, and you cannot find a fault, but the website still won\u2019t load, it\u2019s always a good time to dig through logs. Apache2 keeps two sets of logs: access and error. Depending on your choice of Linux distribution, you can find the logs stored at \/var\/log\/log\/apache2\/ or \/var\/log\/httpd. You can list all files in your web server\u2019s log directory using the commands below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ls \/var\/log\/apache2\/<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ls \/var\/log\/httpd\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An easy way to start reading the logs when you don\u2019t necessarily know what you are looking for is to use the filtering app \u2018grep\u2019. Search for any errors using one of the commands below that correspond with your system\u2019s web application name.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo grep -i -r error \/var\/log\/apache2\/<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo grep -i -r error \/var\/log\/httpd\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ubuntu and Debian users might also need to check through the compressed log files. This can be done using \u2018zgrep\u2019 instead, but due to its limitations, you can only search one log at a time, for example, using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo zgrep error \/var\/log\/apache2\/error.log.2.gz<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Not all errors logged by your web service necessarily mean something is wrong with your server, but look for repeating problems, like missing files in the example error below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[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<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If searching for errors does not yield results, you may also wish to enable more verbose logging. The configuration file\u2019s parameter &#8216;LogLevel&#8217; controls the log output amount. On Debian and Ubuntu systems, open your configuration file using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/etc\/apache2\/apache2.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With CentOS and other Red Hat-based servers, use the command here instead.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo vi \/etc\/httpd\/conf\/httpd.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Find the LogLevel parameter and change it from the default \u2018warn\u2019 value to \u2018debug\u2019, 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 that are appropriate for your system.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl reload apache2<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl reload httpd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To generate new entries using the new log levels, try accessing your website again a couple of times, even if it doesn\u2019t work. The more verbose logs should help narrow down any underlying issues.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check other services<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If your website still won\u2019t load after all the troubleshooting with Apache, check other related services on your cloud server<span style=\"margin: 0px;padding: 0px\">, such as databases,\u00a0<a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/troubleshoot-network-connectivity-linux-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">firewalls, and network connections<\/a><\/span>, which also have their own troubleshooting instructions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n","protected":false},"author":3,"featured_media":27308,"comment_status":"open","ping_status":"closed","template":"","community-category":[289,250],"class_list":["post-2443","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2443"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2443\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2443"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}