PHP 8.6.0 Beta 2 available for testing

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.

Warning

Use PHP-FPM Instead

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.

Note:

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.

  1. Obtain Apache httpd from the location listed above, and unpack it:

    tar -xzf httpd-2.x.NN.tar.gz
    
  2. Likewise, obtain and unpack the PHP source:

    tar -xzf php-NN.tar.gz
    
  3. Build and install Apache httpd. Consult the Apache httpd install documentation for more details on building Apache httpd. Note that mod_php requires the prefork MPM unless PHP was compiled with thread safety (--enable-zts). If you intend to use PHP-FPM instead (recommended), you can use the default event MPM and skip to the PHP-FPM installation instructions.

    cd httpd-2_x_NN
    ./configure --enable-so --with-mpm=prefork
    make
    make install
    
  4. Now 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.:

    /usr/local/apache2/bin/apachectl start
    
    and stop the server to continue with the configuration for PHP:
    /usr/local/apache2/bin/apachectl stop
    

  5. Now, configure and build PHP. This is where you customize PHP with various options, like which extensions will be enabled. Run ./configure --help for 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 to apxs accordingly. Note that some distributions may rename apxs to apxs2.

    cd ../php-NN
    ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pdo-mysql
    make
    make install
    

    If you decide to change your configure options after installation, you'll need to re-run the configure, make, and make install steps. 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 install will also install » PEAR, various PHP tools such as phpize, install the PHP CLI, and more.

  6. Setup your php.ini.

    cp php.ini-development /usr/local/lib/php.ini
    

    You may edit your .ini file to set PHP options. If you prefer having php.ini in another location, use --with-config-file-path=/some/path in step 5.

    If you instead choose php.ini-production, be certain to read the list of changes within, as they affect how PHP behaves.

  7. Edit your httpd.conf to load the PHP module. The path on the right hand side of the LoadModule statement must point to the path of the PHP module on your system. The make install from above may have already added this for you, but be sure to check.

    LoadModule php_module modules/libphp.so
  8. Tell Apache httpd to parse certain extensions as PHP. For example, let's have Apache httpd parse .php files as PHP. Instead of only using the AddType directive, we want to avoid potentially dangerous uploads and created files such as exploit.php.jpg from being executed as PHP. Using this example, you could have any extension(s) parse as PHP by simply adding them. We'll add .php to demonstrate.

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

    Or, if we wanted to allow .php, .php2, .php3, .php4, .php5, .php6, and .phtml files to be executed as PHP, but nothing else, we'd use this:

    <FilesMatch "\.(php[2-6]?|phtml)$">
        SetHandler application/x-httpd-php
    </FilesMatch>

    And to allow .phps files to be handled by the php source filter, and displayed as syntax-highlighted source code, use this:

    <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 FallbackResource directive may be used.

    Because the SetHandler directive is applied whether the file exists or not, and the FallbackResource is only applied if a handler has not already been set, you may need to use the If directive to ensure that the handler is only applied if the file exists. This allows the FallbackResource to handle paths which end in .php but do not exist, which may be useful for error handling and routing.

    <FilesMatch "\.php$">
        <If "-f %{REQUEST_FILENAME}">
            SetHandler application/x-httpd-php
        </If>
    </FilesMatch>
    FallbackResource /index.php

    mod_rewrite may be used to allow any arbitrary .php file to be displayed as syntax-highlighted source code, without having to rename or copy it to a .phps file:

    RewriteEngine 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.

  9. Use your normal procedure for starting Apache httpd, e.g.:

    /usr/local/apache2/bin/apachectl start
    

    OR

    service 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.

Note: MPM Compatibility

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.

Note:

The Apache MultiViews FAQ discusses using multiviews with PHP.

add a note

User Contributed Notes 2 notes

up
13
nmmm at nmmm dot nu
17 years ago
When I upgrade to apache 2.2, this:

AddType application/x-httpd-php .php5
AddType application/x-httpd-php .php42
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtm
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .asp

...does not worked for me, so I did this:

<FilesMatch "\.(php*|phtm|phtml|asp|aspx)$">
SetHandler application/x-httpd-php
</FilesMatch>

Another interesting point with Apache 2.2 is following.
Let suppose we installed PHP as module. But for some directory, we need to use PHP as CGI (probably because of custom configuration). This can be done using:

<FilesMatch "\.(php*|phtm|phtml|asp|aspx)$">
SetHandler none
</FilesMatch>

AddType application/x-httpd-php-custom .php
Action  application/x-httpd-php-custom  /cgi-bin/php-huge

Note type must be different than "application/x-httpd-php" and also you need to deactivate the handler on sertain extention. You can do mixed configuration:

<FilesMatch "\.(php)$">
SetHandler none
</FilesMatch>

AddType application/x-httpd-php-custom .php
Action  application/x-httpd-php-custom  /cgi-bin/php-huge

in such case files like *.php5 and so on will be parsed via module, but *.php will go to php-huge executable.
up
7
Morning Star
3 years ago
I had just installed php8.1.12 on a machine used for writing C code. 

Below are some libraries that I needed to download on a debian-based OS. 

apt-get install libpcre3 libpcre3-dev 
apt-get install apache2-dev
apt-get install libxml2-dev
apt-get install libsqlite3-dev

These were the missing packages that I required. 
If you get an error regarding a missing package or library, for example when I needed sqlite3, run the command: 

apt search sqlite3

And you'll be able to see if there's any dev or lib packages. 

The apache2 instructions worked flawlessly at the time of php8.1.12; and in order to get certain requirements for an application, I had to run the php configure file like so:

./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pdo-mysql --with-mysqli --with-zip --enable-gd

The extra flags allowed me to use both types of mysql, allowed me to utilize PHP zip archiving, and allowed me to use Gnatt stuff.
To Top