3.1. Securing Apache

Securing the Apache Web server is a pretty broad topic, so rather than try to fit everything into one section, we focus on two ways to make Apache more secure when it's running PHP applications: using SuExec and ModSecurity.

3.1.1. Securing PHP applications with SuExec

If your application runs on Apache (as more than half the Web sites on the Internet do), you may want to consider enabling SuExec in your Apache configuration. SuExec is a mechanism that is bundled with Apache that causes scripts to be run as the user that owns the script, rather than running them as the Web server user.

In a non-SuExec environment, all scripts are run as the same user ID as the Web server itself. Unfortunately, one vulnerable script can give a malicious user back-door access to the entire Web server, including scripts running on other sites hosted on the same server.

SuExec attempts to mitigate this problem by restricting Web applications to their own areas and running them under their owner's user ID, rather than under the Web server's user ID. For example, this script would run under the user ID of jsmith:

/home/~jsmith/public_html/scripts/please_hack_me.php

A malicious user could exploit this script, but he or she would have access only to files and programs that the jsmith user is allowed to use. Every other user on the server would be protected from jsmith's insecure script.

Unfortunately, getting SuExec to work properly with virtual hosts, or multiple independent Web ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.