Open Source

SolutionBase: Troubleshoot Apache with these tips

Scott Lowe offers 10 troubleshooting tips to help solve the most common Apache dilemmas.

As a community-supported project, the open source Apache Web server is well-proven, but can still offer an administrator headaches from time to time when things don't go quite as planned.

In this article, I will provide you with 10 tips to help you solve the most common Apache dilemmas.

Stay current with Apache releases

The Apache group regularly releases updates that correct bugs or improve potentially undesirable behavior. If you are having a particular problem with your Apache installation, look at the changelog for the latest version to see if your problem is addressed. Even if your problem is not specifically addressed, it's good practice to stay current with software updates as many changes will also improve the security of your system.

Know where to find Apache community resources

You're probably not the first person to run into a particular problem. The Apache Foundation has established a troubleshooting wiki that is updated with new information as situations arise. Further, Apache has an array of pretty good official documentation. There are also a number of Web forums that focus specifically on Apache. These forums include www.apacheforum.com and www.webmasterworld.com/forum92.

With that said, the Apache community is huge, and there are a ton of places to go for help when you have a problem. In my Apache experience, I've used TechRepublic and other professional sites, but my first stop is Google. Type in the exact error message or symptom, and the chances are really good that you'll get some hits back.

Failing that, here are some other outstanding resources that you can use for help:

  • Apache support WebRing: This is the main page of the Apache support webring that houses a plethora of information about Apache and provides a place to start when trying to solve a difficult problem.
  • IRC channel #Apache: This is an IRC room with live Apache experts that, if you are patient, are willing to help you through a problem. When using this resource, make sure to understand that these folks do this out of the goodness of their hearts, so be thankful.

Know where to look

If you're having trouble with Apache or one of its modules, your first stop should be in looking over Apache's detailed error log. Depending on how your system and Apache are configured, the error log may live in different locations. The default location for this file is a file named error_log, located in the logs directory inside your Apache root installation. If you can't find your error log, open the httpd.conf configuration file and look for the ErrorLog directive, which defines the location.

Apache is initially configured to the "warn" log level, meaning that any problem more serious than a warning (critical, emergency, error, alert, and warn) is logged. You can adjust the logging level in httpd.conf my manipulating the LogLevel directive.

From the Apache documentation, Table A outlines the eight available warning levels and provides an example of what would be logged at that level.

Table A

Level

Description

Example

Emerg

Emergencies - system is unusable.

"Child cannot open lock file. Exiting"

Alert

Action must be taken immediately.

"getpwuid: couldn't determine user name from uid"

Crit

Critical Conditions.

"socket: Failed to get a socket, exiting child"

Error

Error conditions.

"Premature end of script headers"

Warn

Warning conditions.

"child process 1234 did not exit, sending another SIGHUP"

Notice

Normal but significant condition.

"httpd: caught SIGBUS, attempting to dump core in ..."

Info

Informational.

"Server seems busy, (you may need to increase StartServers, or Min/MaxSpareServers)..."

Debug

Debug-level messages

"Opening config file ..."

If you can't figure out why your Apache server is having a problem, try adjusting the log level to a higher threshold to capture more information. After you change the level, stop and restart your server.

There are actually two log files in Apache: error_log, which I described in this section, and access_log. The error_log file, as you might expect, is the log of most interest for troubleshooting purposes. However, also make use of the access_log when looking for problems. This file lists all of the items pulled down by clients along with the HTTP error or success code.

Part of knowing where to look involves knowing what's actually running on your server, too. Used in conjunction with the httpd command, use the -l and -M parameters to see what is loaded in your Apache configuration. The -l (el) parameter lists modules compiled into the server, but does not include dynamically loaded modules included using the LoadModule directive in httpd.conf. The -M parameter does show you more information and lists all loaded static and shared modules.

Don't allow an AllowOverride to ruin your day

Depending on how you want to run your Web site, you can selectively alter the behavior of your Apache server by making use of .htaccess files. Simply put, an .htaccess file is a file in a directory that lets you make configuration changes that affect just that folder. For example, if you've disabled the "Indexes" in httpd.conf for all directories, none of your visitors will be able to access a directory listing. You may have a single folder for which this access should be allowed. In this case, you would have an .htaccess file with the "Options Indexes" directive.

You can probably begin to see some reasons why .htaccess files can be problematic. First of all, for very large sites, keeping track of these files could be a very difficult task. Now, when you have a functionality problem, you can't just look to one source for possible configuration problems; you now need to traverse your directory structure and look for .htaccess files.

Second, by allowing the use of these files, you may be allowing users the lack your Apache security prowess to make potentially insecure changes to your Web site.

Finally, use of .htaccess can exact a performance penalty on your web site due to the need of the Web server to look for an .htaccess file in the current directory and in every superior directory all the way to the document root of the Web server.

Unless you have a really good reason, avoid the use of .htaccess files. Instead, in the httpd.conf file, make liberal use of "Directory" sections to set per-directory options.

On the other hand, if you are using .htaccess files and they don't seem to be activated, look to the httpd.conf file and make sure the directive "AllowOverride" is not set to "None". You can limit what options are allowed in an .htaccess file by further manipulating the AllowOverride directive's type. Table B, based on the Apache documentation, provides you with a list of possible AllowOverride options. Only use the options you need.

Table B

Type

Description

All

Allow use of all directives listed in this table. This is generally considered to be a major security risk since it allows users to override httpd.conf settings such as disallowing the following of symbolic links along with other things.

None

Disallow the use of .htaccess files

AuthConfig

Allow use of the authorization directives (AuthDBMGroupFile, AuthDBMUserFile, AuthGroupFile, AuthName, AuthType, AuthUserFile, Require, etc.).

FileInfo

Allow use of the directives controlling document types (DefaultType, ErrorDocument, ForceType, LanguagePriority, SetHandler, SetInputFilter, SetOutputFilter, and mod_mime Add* and Remove* directives, etc.).

Indexes

Allow use of the directives controlling directory indexing (AddDescription, AddIcon, AddIconByEncoding, AddIconByType, DefaultIcon, DirectoryIndex, FancyIndexing, HeaderName, IndexIgnore, IndexOptions, ReadmeName, etc.).

Limit

Allow use of the directives controlling host access (Allow, Deny and Order).

Options

Allow use of the directives controlling specific directory features (Options and XBitHack).

A newly installed module/extension (such as PHP) is not working

Suppose, for example, that you recently installed the PHP extension, but, upon visiting your site, you're seeing the PHP code itself instead of the results of that code's execution. First off, for folks new to Linux, installing new modules and getting everything working perfectly can be akin to setting up a Christmas tree with your hands tied behind your back.

Since it's among the most popular available, let's use the PHP module for an example.

Your problem could be something very simple. Apache may not be configured to do anything with the .php extension. Look at your httpd.conf configuration file and look for the DirectoryIndex directive. Make sure the line reads:

DirectoryIndex index.html index.php

The default Apache installation omits the "Index.php" file, rendering many PHP-based sites useless.

Further, your httpd.conf file needs to tell Apache about the .php extension through the use of the AddType directive. If you're using PHP, you should have a line in your configuration that reads:

AddType application/x-httpd-php .php

Normally, this line is commented out.

Finally, make sure your httpd.conf file is actually loading a supported PHP module. If you're not loading the PHP handling module, Apache won't know what to do with .php pages, no matter how many AddType directives you include. Here is an example LoadModule directory for PHP 4.

(Apache 2+) LoadModule php4_module modules/libphp4.so
(Apache 1.3) LoadModule php4_module libexec/libphp4.so

Apache 1.3 also requires a fourth directive:

AddModule mod_php4.c

If this still isn't working, make sure your module is compatible with the version of Apache you're running. The PHP developers, for example, recommend that, for Apache 2 and later, you use at least PHP 4.3.0.

The short answer: Make sure you've strictly followed the instructions for setting up Apache with additional modules. I've highlighted some of PHP's requirements in this tip, but every module has its own nuances.

Don't worry about 'connection reset by peer' errors too much

When a user cancels a request to your site (presses the Stop button or hits Escape), your server logs will be appended with the message "connection reset by peer". If you see this message only occasionally, it probably means someone typed in the wrong address or just got impatient while waiting for your site to load. If you're seeing this message on a regular basis, you might have congestion issues slowing your site to a point beyond the tolerance of some people. You may have other network issues creating this problem.

Make sure Apache is actually running

I'm going to confess; this one has gotten me in the past. I spent quite some time looking through error logs and the httpd.conf file before I even bothered to make sure Apache was running. After reprimanding myself, I started the service and, until today, have never told a soul.

The point: Any day can be an off day! Look for the simple things, too.

Check for port conflicts

If you've installed Apache with the defaults, the httpd service listens on port 80 for traffic. If you have some other services — perhaps a different Web server — also listening on port 80, Apache will not be able to listen to requests (or, Apache will work fine, but the other application will be broken). In these cases, make sure Apache is the only service listening on port 80.

A combination of the fuser and ps commands handily accomplishes this goal.

Use the command fuser -n tcp 80 to get a list of processes that are listening on port 80. Then, use the ps command to see which processes are used by the httpd daemon. ps -ef | grep httpd accomplishes this part. You'll see results similar to those in Figure A.

Figure A

Show which processes are listening on port 80.

Now, match up the list of ports provided by the fuser command and those provided by the ps command. If there are more ports listed by fuser than are accounted for by ps, use the ps command to find out exactly which other services are listening on port 80.

Use configtest

So you've made some modifications to your httpd.conf file and now Apache isn't working properly, but you don't have a handy backup of the original file to find out what's wrong?

Well, the good folks that created Apache have provided you with a way to scan your httpd.conf file and make sure it's free from obvious errors. This error-checking tool is provided as a part of the apachectl program. To use it, execute apachectl -configtest from the command line. The apachectl program is located in the bin directory of your Apache installation.

If no errors are found, the utility will execute like this example:

[root@localhost bin]# ./apachectl configtest
Syntax OK

To show how this tool works, I've intentionally create an httpd.conf file with an error or two.

[root@localhost bin]# ./apachectl configtest
Syntax error on line 22 of /usr/local/apache/conf/httpd.conf:
Invalid command 'sserversignature', perhaps misspelled or defined by a module not included in the server configuration

In this case, I have misspelled a directive, which should read "ServerSignature", not "SServerSignature". Even if you correct the error, run the tool again as more errors may be found. As a highlight to this, I actually had another error in my httpd.conf file.

[root@localhost bin]# ./apachectl configtest
Syntax error on line 108 of /usr/local/apache/conf/httpd.conf:
DocumentRoot must be a directory

In this case, the directory name in the DocumentRoot directive also had a spelling error which would have resulted in Apache being unable to serve any content since the directory does not exist.

The apachectl program has a number of options. You've probably used "start" and "stop", but there are many more that may be useful, depending on what you're trying to do. Some of the options you can use with apachectl include:

  • configtest: Checks for errors in httpd.conf.
  • fullstatus: (Requires mod_status) Provides you with a configuration report at the location specified in the module's httpd.conf configuration.
  • Graceful: Restarts Apache, maintaining current connections.
  • Restart: Restarts Apache, killing all connections.
  • Start: Starts the Apache server.
  • Status: (Requires mod_status) Same as fullstatus, except omits details of current requests.
  • Stop: Stops Apache.

Understand the various HTTP/1.1 error codes

Specific HTTP errors on a client or in your server logs can help point you in the right direction. For example, if you have users complaining that they always get "404" errors when they click a link to visit your site, the host link is pointing to a page on your Apache server that does not exist. Or, if a client receives a "501" error, the client is attempting to access content on your server for which no handler exists. Often, this error can be the result of a problem with a CGI script.

In Table C, I've taken information (with permission) from the W3C, the organization responsible for keeping HTTP error messages consistent and reformatted it to be a little more easily read and removed some of the extraneous information.

Table C

200

OK

The request has succeeded. The information returned with the response is dependent on the method used in the request, for example: GET an entity corresponding to the requested resource is sent in the response; HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body; POST an entity describing or containing the result of the action; TRACE an entity containing the request message as received by the end server.

201

Created

The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field. The response SHOULD include an entity containing a list of resource characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. The origin server MUST create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.

202

Accepted

The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this. The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed. The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.

203

Non-Authoritative Information

The returned metainformation in the entity-header is not the definitive set as available from the origin server, but is gathered from a local or a third-party copy. The set presented MAY be a subset or superset of the original version. For example, including local annotation information about the resource might result in a superset of the metainformation known by the origin server. Use of this response code is not required and is only appropriate when the response would otherwise be 200 (OK).

204

No Content

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response may include new or updated metainformation in the form of entity-headers, which, if present, should be associated with the requested variant. If the client is a user agent, it should not change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation should be applied to the document currently in the user agent's active view. The 204 response must not include a message-body, and thus is always terminated by the first empty line after the header fields.

205

Reset Content

The server has fulfilled the request and the user agent should reset the document view which caused the request to be sent. This response is primarily intended to allow input for actions to take place via user input, followed by a clearing of the form in which the input is given so that the user can easily initiate another input action. The response must not include an entity.

206

Partial Content

The server has fulfilled the partial get request for the resource. The request must have included a Range header field indicating the desired range, and may have included an If-Range header field to make the request conditional.

300

Multiple Choices

The requested resource corresponds to any one of a set of representations, each with its own specific location, and agent- driven negotiation information is being provided so that the user (or user agent) can select a preferred representation and redirect its request to that location.

301

Moved Permanently

The requested resource has been assigned a new permanent URI and any future references to this resource should use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise. The new permanent URI should be given by the Location field in the response. Unless the request method was HEAD, the entity of the response should contain a short hypertext note with a hyperlink to the new URI(s).

302

Found

The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client should continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

303

See Other

The response to the request can be found under a different URI and should be retrieved using a GET method on that resource. This method exists primarily to allow the output of a post-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response must not be cached, but the response to the second (redirected) request might be cacheable.

304

Not Modified

If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server should respond with this status code. The 304 response must not contain a message-body, and thus is always terminated by the first empty line after the header fields.

305

Use Proxy

The requested resource must be accessed through the proxy given by the Location field. The Location field gives the URI of the proxy. The recipient is expected to repeat this single request via the proxy. 305 responses must only be generated by origin servers.

307

Temporary Redirect

The requested resource resides temporarily under a different URI. Since the redirection may be altered on occasion, the client should continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

400

Bad Request

The request could not be understood by the server due to malformed syntax. The client should not repeat the request without modifications.

401

Unauthorized

The request requires user authentication. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. The client may repeat the request with a suitable Authorization header field. If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user should be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" [43].

403

Forbidden

The server understood the request, but is refusing to fulfill it. Authorization will not help and the request should not be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it should describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.

404

Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code should be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

405

Method Not Allowed

The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

406

Not Acceptable

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

407

Proxy Authentication Required

This code is similar to 401 (Unauthorized), but indicates that the client must first authenticate itself with the proxy. The proxy must return a Proxy-Authenticate header field containing a challenge applicable to the proxy for the requested resource. The client may repeat the request with a suitable Proxy-Authorization header field.

408

Request Timeout

The client did not produce a request within the time that the server was prepared to wait. The client may repeat the request without modifications at any later time.

409

Conflict

The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body should include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required. Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

410

Gone

The requested resource is no longer available at the server and no forwarding address is known. This condition is expected to be considered permanent. Clients with link editing capabilities should delete references to the Request-URI after user approval. If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) should be used instead. This response is cacheable unless indicated otherwise. The 410 response is primarily intended to assist the task of web maintenance by notifying the recipient that the resource is intentionally unavailable and that the server owners desire that remote links to that resource be removed. Such an event is common for limited-time, promotional services and for resources belonging to individuals no longer working at the server's site. It is not necessary to mark all permanently unavailable resources as "gone" or to keep the mark for any length of time — that is left to the discretion of the server owner.

411

Length Required

The server refuses to accept the request without a defined Content- Length. The client may repeat the request if it adds a valid Content-Length header field containing the length of the message-body in the request message.

412

Precondition Failed

The precondition given in one or more of the request-header fields evaluated to false when it was tested on the server. This response code allows the client to place preconditions on the current resource metainformation (header field data) and thus prevent the requested method from being applied to a resource other than the one intended.

413

Request Entity Too Large

The server is refusing to process a request because the request entity is larger than the server is willing or able to process. The server may close the connection to prevent the client from continuing the request.

414

Request-URI Too Long

The server is refusing to service the request because the Request-URI is longer than the server is willing to interpret. This rare condition is only likely to occur when a client has improperly converted a POST request to a GET request with long query information, when the client has descended into a URI "black hole" of redirection (e.g., a redirected URI prefix that points to a suffix of itself), or when the server is under attack by a client attempting to exploit security holes present in some servers using fixed-length buffers for reading or manipulating the Request-URI.

415

Unsupported Media Type

The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.

416

Requested Range Not Satisfiable

A server should return a response with this status code if a request included a Range request-header field, and none of the range-specifier values in this field overlap the current extent of the selected resource, and the request did not include an If-Range request-header field. (For byte-ranges, this means that the first- byte-pos of all of the byte-range-spec values were greater than the current length of the selected resource.)

417

Expectation Failed

The expectation given in an Expect request-header field could not be met by this server, or, if the server is a proxy, the server has unambiguous evidence that the request could not be met by the next-hop server.

500

Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

501

Not Implemented

The server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.

502

Bad Gateway

The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.

503

Service Unavailable

The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay may be indicated in a Retry-After header. If no Retry-After is given, the client should handle the response as it would for a 500 response.

504

Gateway Timeout

The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI (e.g. HTTP, FTP, LDAP) or some other auxiliary server (e.g. DNS) it needed to access in attempting to complete the request.

505

HTTP Version Not Supported

The server does not support, or refuses to support, the HTTP protocol version that was used in the request message. The server is indicating that it is unable or unwilling to complete the request using the same major version as the client, other than with this error message. The response should contain an entity describing why that version is not supported and what other protocols are supported by that server.

Used with permission from http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Summary

Will these ten tips help you solve all of your problems? Probably not, but these tips were designed to help point you in the right direction to solve problems. Share your Apache troubleshooting tips with me for a future version of this article.

About Scott Lowe

Since 1994, Scott Lowe has been providing technology solutions to a variety of organizations. After spending 10 years in multiple CIO roles, Scott is now an independent consultant, blogger, author, owner of The 1610 Group, and a Senior IT Executive w...

Editor's Picks

Free Newsletters, In your Inbox