How to Customize the Apache Server for PHP Programming

By Richard Blum

The Apache server is a must anyone learning PHP programming. By default, the Apache web server uses the httpd.conf configuration file to store its settings. For Linux and Mac systems, the file is usually stored in the /etc folder structure, often under either /etc/httpd or /etc/apache2.

The XAMPP package installs the Apache configuration file in the c:\xampp\apache\conf folder in Windows or /Applications/XAMPP/apache/conf in macOS.

The httpd.conf configuration file contains individual lines called directives. Each directive defines one configuration option, along with the value that you set.

The Apache web server is very versatile, with lots of different options and features. The downside to that is it can make the configuration seem complex at first, but the configuration file is organized such that you should be able to find what you’re looking for relatively easily.

Many systems break the Apache web server configurations into multiple files to help make the features more modular. Look for the Include directive lines in the main httpd.conf configuration file to see what other files contain Apache web server configuration settings.

Defining the web folder location

The main job of the Apache web server is to serve files to remote clients. However, you don’t want just anyone retrieving just any file on your system! To limit what files the Apache server serves, you must restrict it to a specific folder area in the system.

You set the folder where the Apache web server serves files using the DocumentRoot directive:

DocumentRoot c:/xampp/htdocs

The htdocs folder is the normal default used for the Apache web server in Windows and macOS environments (for macOS, it’s located in /Applciations/XAMPP/htdocs). For Linux environments, it has become somewhat common to use /var/www/html as the DocumentRoot folder.

If you choose to move the DocumentRoot folder to another folder location on the server, make sure the user account that runs the Apache web server has access to at least read files from the folder.

Setting the default TCP port

The Apache web server listens for incoming connections from client browsers using two different default TCP network ports:

  • TCP port 80 for HTTP requests
  • TCP port 443 for HTTPS requests

HTTPS requests use encryption to secure the communication between the browser and the server. This method is quickly becoming a standard for all web servers on the Internet.

You set the ports the Apache web server accepts incoming requests on using the Listen directive:

Listen 80
Listen 443

You can use multiple Listen directives in the configuration file to listen on more than one TCP port.

Interacting with the PHP server

The Apache web server must know how to pass files that contain PHP code to the PHP server for processing. This is a two-step process.

First, you have to tell the Apache web server to load the PHP server module so that it can establish the link between the Apache and PHP servers. You do that using the LoadModule directive:

LoadModule php7_module "c:/xampp/php/apache2_4.dll"

After Apache loads the PHP module, you have to tell it what type of files to send to the PHP server. You do this using the AddHandler directive:

AddHandler application/x-httpd-php .php

This directive tells the Apache web server to forward all files with the .php file extension to the PHP module, which then forwards the files to the PHP server for processing.

It may be tempting to just forward all .html files to the PHP server, because the PHP server will pass any HTML code directly to the client browser. However, this will add extra processing time to load your static web pages, causing a performance issue with your HTML pages.

Tracking errors

When working in a development environment, it’s always helpful to be able to track any errors that occur in your applications. The Apache web server supports eight different levels of error messages.

Apache Web Server Error Levels
Error Level Description
emerg A fatal error will halt the Apache web server.
alert A severe error will have an adverse impact on your application and should be resolved immediately.
crit A critical condition caused the operation to fail, such as a failure to access the network.
error An error occurred in the session, such as an invalid HTTP header.
warn A minor issue occurred in the session but didn’t prevent it from continuing.
notice Something out of the normal occurred.
debug A low-level detailed message occurs for each step the server takes in processing a request.

You define the level of error tracking using the LogLevel directive and the location of the error log using the ErrorLog directive:

LogLevel warn
ErrorLog logs/error.log

The debug log level can be useful for troubleshooting issues but is not recommended for normal activity, because it generates lots of output!

You can customize the appearance of the log messages using the LogFormat directive. Apache allows you to determine just what information appears in the log file, which can be handy when trying to troubleshoot specific problems. Consult the Apache server documentation for the different options you have available for customizing the logs.