php8.5
Home/ Manual/ install / unix/ Apache httpd 2.x on Unix systems

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.

Warning

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:

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

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

    output
    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.:

    output
    /usr/local/apache2/bin/apachectl start

    and stop the server to continue with the configuration for PHP:

    output
    /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.

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

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

    apache-conf
    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.

    apache-conf
    <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:

    apache-conf
    <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:

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

    apache-conf
    <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:

    apache-conf
    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.:

    output
    /usr/local/apache2/bin/apachectl start

    OR

    output
    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

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.

Source: install/unix/apache2.xml · from the official PHP manual (php/doc-en)