Apache httpd 2.x on Unix systems
This section contains notes and hints specific to Apache HTTP Server 2.x installs of PHP on Linux and Unix-like systems.
This section contains notes and hints specific to Apache HTTP Server 2.x installs of PHP on Linux and Unix-like systems.
Do not use mod_php for new installations. The instructions on this page are retained for historical reference and for the rare cases where embedding PHP directly in the Apache httpd process is specifically required.
For all modern deployments, use PHP-FPM (FastCGI Process Manager) with Apache httpd's mod_proxy_fcgi module. PHP-FPM provides better resource management, process isolation, independent restart of PHP without restarting Apache httpd, and compatibility with Apache httpd's event MPM (the default since Apache httpd 2.4). Major Linux distributions ship this as the default configuration.
The mod_php approach embeds PHP directly into every Apache httpd worker process. Unless PHP is compiled with thread safety (--enable-zts), mod_php requires the prefork MPM, which significantly limits concurrency. If you proceed with mod_php, you should fully understand the performance and security implications.
The Apache Documentation is the most authoritative source of information on the Apache httpd 2.x server. More information about installation options for Apache httpd may be found there.
The most recent version of Apache httpd may be obtained from Apache download site, and a fitting PHP version from the above mentioned places. This quick guide covers only the basics to get started with Apache httpd 2.x and PHP. For more information read the Apache Documentation. The version numbers have been omitted here, to ensure the instructions are not incorrect. In the examples below, 'NN' should be replaced with the specific version of Apache httpd being used.
These instructions apply to Apache httpd 2.4, which is the only supported release branch of Apache httpd. Earlier versions (2.2 and below) are end of life and should not be used.
As of PHP 8.4.0, the apache2handler SAPI requires at least Apache 2.4; support for the End-of-Life Apache 2.0 and 2.2 has been removed.
Obtain Apache httpd from the location listed above, and unpack it:
outputtar -xzf httpd-2.x.NN.tar.gzLikewise, obtain and unpack the PHP source:
outputtar -xzf php-NN.tar.gzBuild and install Apache httpd. Consult the Apache httpd install documentation for more details on building Apache httpd. Note that
mod_phprequires thepreforkMPM unless PHP was compiled with thread safety (--enable-zts). If you intend to use PHP-FPM instead (recommended), you can use the defaulteventMPM and skip to the PHP-FPM installation instructions.outputcd httpd-2_x_NN ./configure --enable-so --with-mpm=prefork make make installNow you have Apache httpd 2.x.NN available under /usr/local/apache2, configured with loadable module support and the prefork MPM. To test the installation use your normal procedure for starting the Apache httpd server, e.g.:
output/usr/local/apache2/bin/apachectl startand stop the server to continue with the configuration for PHP:
output/usr/local/apache2/bin/apachectl stopNow, configure and build PHP. This is where you customize PHP with various options, like which extensions will be enabled. Run
./configure --helpfor a list of available options. In our example we'll do a simple configure with Apache httpd and MySQL support.If you built Apache httpd from source, as described above, the below example will match your path for
apxs, but if you installed Apache httpd some other way, you'll need to adjust the path toapxsaccordingly. Note that some distributions may renameapxstoapxs2.outputcd ../php-NN ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pdo-mysql make make installIf you decide to change your configure options after installation, you'll need to re-run the
configure,make, andmake installsteps. You only need to restart apache for the new module to take effect. A recompile of Apache httpd is not needed.Note that unless told otherwise,
make installwill also install PEAR, various PHP tools such as phpize, install the PHP CLI, and more.Setup your
php.ini.outputcp php.ini-development /usr/local/lib/php.iniYou may edit your
.inifile to set PHP options. If you prefer havingphp.iniin another location, use--with-config-file-path=/some/pathin step 5.If you instead choose
php.ini-production, be certain to read the list of changes within, as they affect how PHP behaves.Edit your
httpd.confto load the PHP module. The path on the right hand side of theLoadModulestatement must point to the path of the PHP module on your system. Themake installfrom above may have already added this for you, but be sure to check.apache-confLoadModule php_module modules/libphp.soTell Apache httpd to parse certain extensions as PHP. For example, let's have Apache httpd parse
.phpfiles as PHP. Instead of only using theAddTypedirective, we want to avoid potentially dangerous uploads and created files such asexploit.php.jpgfrom being executed as PHP. Using this example, you could have any extension(s) parse as PHP by simply adding them. We'll add.phpto demonstrate.apache-conf<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>Or, if we wanted to allow
.php,.php2,.php3,.php4,.php5,.php6, and.phtmlfiles to be executed as PHP, but nothing else, we'd use this:apache-conf<FilesMatch "\.(php[2-6]?|phtml)$"> SetHandler application/x-httpd-php </FilesMatch>And to allow
.phpsfiles to be handled by the php source filter, and displayed as syntax-highlighted source code, use this:apache-conf<FilesMatch "\.phps$"> SetHandler application/x-httpd-php-source </FilesMatch>To allow use of a PHP file as the default handler if no other handler is found, for example when using a routing engine, the
FallbackResourcedirective may be used.Because the
SetHandlerdirective is applied whether the file exists or not, and theFallbackResourceis only applied if a handler has not already been set, you may need to use theIfdirective to ensure that the handler is only applied if the file exists. This allows theFallbackResourceto handle paths which end in.phpbut do not exist, which may be useful for error handling and routing.apache-conf<FilesMatch "\.php$"> <If "-f %{REQUEST_FILENAME}"> SetHandler application/x-httpd-php </If> </FilesMatch> FallbackResource /index.phpmod_rewritemay be used to allow any arbitrary.phpfile to be displayed as syntax-highlighted source code, without having to rename or copy it to a.phpsfile:apache-confRewriteEngine On RewriteRule (.*\.php)s$ $1 [H=application/x-httpd-php-source]The php source filter should not be enabled on production systems, where it may expose confidential or otherwise sensitive information embedded in source code.
Use your normal procedure for starting Apache httpd, e.g.:
output/usr/local/apache2/bin/apachectl startOR
outputservice httpd restart
Following the steps above you will have a running Apache httpd web server with support for PHP as a SAPI module. There are many more configuration options available for Apache httpd and PHP. For more information type ./configure --help in the corresponding source tree.
Unless PHP was compiled with Zend Thread Safety (--enable-zts), mod_php requires the prefork MPM. For information on why, read the related FAQ entry on using Apache httpd with a threaded MPM.
Most distribution packages of mod_php are not built with ZTS, so prefork is typically required. If you need a threaded MPM (recommended for better performance under load), use PHP-FPM with mod_proxy_fcgi instead.
The Apache MultiViews FAQ discusses using multiviews with PHP.