<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/install.unix.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'install.unix.apache2.php',
    1 => 'Apache httpd 2.x on Unix systems',
    2 => 'Apache httpd 2.x on Unix systems',
  ),
  'up' => 
  array (
    0 => 'install.unix.php',
    1 => 'Installation on Unix systems',
  ),
  'prev' => 
  array (
    0 => 'install.unix.commandline.php',
    1 => 'CGI and command line setups',
  ),
  'next' => 
  array (
    0 => 'install.unix.nginx.php',
    1 => 'Nginx 1.4.x on Unix systems',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'install/unix/apache2.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="install.unix.apache2" class="sect1">
 <h2 class="title">Apache httpd 2.x on Unix systems</h2>

 <p class="simpara">
  This section contains notes and hints specific to Apache HTTP Server
  2.x installs of PHP on Linux and Unix-like systems.
 </p>

 <div class="warning"><strong class="warning">Warning</strong>
  <h1 class="title">Use PHP-FPM Instead</h1>
  <p class="simpara">
   <strong>Do not use <code class="literal">mod_php</code> for new
   installations.</strong> 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.
  </p>
  <p class="simpara">
   For all modern deployments, use
   <a href="install.fpm.php" class="link">PHP-FPM</a> (FastCGI Process Manager)
   with Apache httpd&#039;s <code class="literal">mod_proxy_fcgi</code> module. PHP-FPM
   provides better resource management, process isolation, independent
   restart of PHP without restarting Apache httpd, and compatibility with
   Apache httpd&#039;s <code class="literal">event</code> MPM (the default since Apache httpd 2.4).
   Major Linux distributions ship this as the default configuration.
  </p>
  <p class="simpara">
   The <code class="literal">mod_php</code> approach embeds PHP directly into every
   Apache httpd worker process. Unless PHP is compiled with thread safety
   (<code class="literal">--enable-zts</code>), <code class="literal">mod_php</code> requires
   the <code class="literal">prefork</code> MPM, which significantly limits
   concurrency. If you proceed with <code class="literal">mod_php</code>, you
   should fully understand the performance and security implications.
  </p>
 </div>

 <p class="simpara">
  The <a href="http://httpd.apache.org/docs/current/" class="link external">&raquo;&nbsp;Apache Documentation</a>
  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.
 </p>

 <p class="simpara">
  The most recent version of Apache httpd may be obtained from
  <a href="http://httpd.apache.org/" class="link external">&raquo;&nbsp;Apache download site</a>,
  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
  <a href="http://httpd.apache.org/docs/current/" class="link external">&raquo;&nbsp;Apache Documentation</a>.
  The version numbers have been omitted here, to ensure the
  instructions are not incorrect. In the examples below, &#039;NN&#039; should be
  replaced with the specific version of Apache httpd being used.
 </p>

 <p class="simpara">
  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.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="simpara">
   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.
  </p>
 </p></blockquote>

 <ol type="1">
  <li class="listitem">
   <p class="simpara">
    Obtain Apache httpd from the location listed above,
    and unpack it:
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
tar -xzf httpd-2.x.NN.tar.gz
</pre></div>
    </div>
   </div>
  </li>

  <li class="listitem">
   <p class="simpara">
    Likewise, obtain and unpack the PHP source:
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
tar -xzf php-NN.tar.gz
</pre></div>
    </div>
   </div>
  </li>

  <li class="listitem">
   <p class="simpara">
    Build and install Apache httpd. Consult the Apache httpd install documentation for
    more details on building Apache httpd. Note that <code class="literal">mod_php</code>
    requires the <code class="literal">prefork</code> MPM unless PHP was compiled
    with thread safety (<code class="literal">--enable-zts</code>). If you intend
    to use PHP-FPM instead (recommended), you can use the default
    <code class="literal">event</code> MPM and skip to the
    <a href="install.fpm.php" class="link">PHP-FPM installation instructions</a>.
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
cd httpd-2_x_NN
./configure --enable-so --with-mpm=prefork
make
make install
</pre></div>
    </div>
   </div>
  </li>

  <li class="listitem">
   <p class="para">
    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.:

    <div class="informalexample">
     <div class="example-contents screen">
<div class="cdata"><pre>
/usr/local/apache2/bin/apachectl start
</pre></div>
     </div>
    </div>

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

    <div class="informalexample">
     <div class="example-contents screen">
<div class="cdata"><pre>
/usr/local/apache2/bin/apachectl stop
</pre></div>
     </div>
    </div>
   </p>
  </li>

  <li class="listitem">
   <p class="simpara">
    Now, configure and build PHP. This is where you customize PHP
    with various options, like which extensions will be enabled. Run
    <strong class="command">./configure --help</strong> for a list of available options. In our example
    we&#039;ll do a simple configure with Apache httpd and MySQL support.
   </p>

   <p class="simpara">
    If you built Apache httpd from source, as described above, the below example will
    match your path for <strong class="command">apxs</strong>, but if you installed Apache httpd some other way, you&#039;ll
    need to adjust the path to <strong class="command">apxs</strong> accordingly. Note that some distributions may rename
    <strong class="command">apxs</strong> to <strong class="command">apxs2</strong>.
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
cd ../php-NN
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pdo-mysql
make
make install
</pre></div>
    </div>
   </div>

   <p class="simpara">
    If you decide to change your configure options after installation,
    you&#039;ll need to re-run the <strong class="command">configure</strong>, <strong class="command">make</strong>,
    and <strong class="command">make install</strong> steps.
    You only need to restart apache for the new module to take effect.
    A recompile of Apache httpd is not needed.
   </p>

   <p class="para">
    Note that unless told otherwise, <strong class="command">make install</strong> will also install
    <a href="https://pear.php.net/" class="link external">&raquo;&nbsp;PEAR</a>,
    various PHP tools such as <a href="install.pecl.phpize.php" class="link">phpize</a>,
    install the PHP CLI, and more.
   </p>
  </li>

  <li class="listitem">
   <p class="simpara">
    Setup your <var class="filename">php.ini</var>.
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
cp php.ini-development /usr/local/lib/php.ini
</pre></div>
    </div>
   </div>

   <p class="simpara">
    You may edit your <code class="literal">.ini</code> file to set PHP options. If you prefer having
    <var class="filename">php.ini</var> in another location,
    use <code class="literal">--with-config-file-path=/some/path</code> in step 5.
   </p>

   <p class="simpara">
    If you instead choose <var class="filename">php.ini-production</var>, be certain to read the list
    of changes within, as they affect how PHP behaves.
   </p>
  </li>

  <li class="listitem">
   <p class="simpara">
    Edit your <var class="filename">httpd.conf</var> to load the PHP module. The path on the right hand
    side of the <code class="literal">LoadModule</code> statement must point to the path of the PHP
    module on your system. The <strong class="command">make install</strong> from above may have already
    added this for you, but be sure to check.
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">LoadModule php_module modules/libphp.so</pre>
</div>
    </div>

   </div>
  </li>

  <li class="listitem">
   <p class="simpara">
    Tell Apache httpd to parse certain extensions as PHP. For example, let&#039;s have
    Apache httpd parse <code class="literal">.php</code> files as PHP. Instead of only using the <code class="literal">AddType</code>
    directive, we want to avoid potentially dangerous uploads and created
    files such as <var class="filename">exploit.php.jpg</var> from being executed as PHP. Using this
    example, you could have any extension(s) parse as PHP by simply adding
    them. We&#039;ll add <code class="literal">.php</code> to demonstrate.
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">&lt;FilesMatch \.php$&gt;
    SetHandler application/x-httpd-php
&lt;/FilesMatch&gt;</pre>
</div>
    </div>

   </div>

   <p class="simpara">
    Or, if we wanted to allow <code class="literal">.php</code>, <code class="literal">.php2</code>,
    <code class="literal">.php3</code>, <code class="literal">.php4</code>, <code class="literal">.php5</code>,
    <code class="literal">.php6</code>, and <code class="literal">.phtml</code> files to be
    executed as PHP, but nothing else, we&#039;d use this:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">&lt;FilesMatch &quot;\.(php[2-6]?|phtml)$&quot;&gt;
    SetHandler application/x-httpd-php
&lt;/FilesMatch&gt;</pre>
</div>
    </div>

   </div>

   <p class="para">
    And to allow <code class="literal">.phps</code> files to be handled by the php source filter, and
    displayed as syntax-highlighted source code, use this:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">&lt;FilesMatch &quot;\.phps$&quot;&gt;
    SetHandler application/x-httpd-php-source
&lt;/FilesMatch&gt;</pre>
</div>
    </div>

   </div>

   <p class="simpara">
    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 <code class="literal">FallbackResource</code> directive may be used.
   </p>

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

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">&lt;FilesMatch &quot;\.php$&quot;&gt;
    &lt;If &quot;-f %{REQUEST_FILENAME}&quot;&gt;
        SetHandler application/x-httpd-php
    &lt;/If&gt;
&lt;/FilesMatch&gt;
FallbackResource /index.php</pre>
</div>
    </div>

   </div>

   <p class="para">
    <code class="literal">mod_rewrite</code> may be used to allow any arbitrary <code class="literal">.php</code> file to be displayed
    as syntax-highlighted source code, without having to rename or copy it
    to a <code class="literal">.phps</code> file:
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">RewriteEngine On
RewriteRule (.*\.php)s$ $1 [H=application/x-httpd-php-source]</pre>
</div>
    </div>

   </div>

   <p class="para">
    The php source filter should not be enabled on production systems, where
    it may expose confidential or otherwise sensitive information embedded in
    source code.
   </p>
  </li>

  <li class="listitem">
   <p class="simpara">
    Use your normal procedure for starting Apache httpd, e.g.:
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
/usr/local/apache2/bin/apachectl start
</pre></div>
    </div>
   </div>

   <p class="para">OR</p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
service httpd restart
</pre></div>
    </div>
   </div>
  </li>
 </ol>

 <p class="simpara">
  Following the steps above you will have a running Apache httpd web server with
  support for PHP as a <code class="literal">SAPI</code> module. There are
  many more configuration options available for Apache httpd and PHP. For more
  information type <strong class="command">./configure --help</strong> in the corresponding
  source tree.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <strong>MPM Compatibility</strong><br />
  <p class="simpara">
   Unless PHP was compiled with Zend Thread Safety
   (<code class="literal">--enable-zts</code>), <code class="literal">mod_php</code> requires
   the <code class="literal">prefork</code> MPM.
   For information on why, read the related FAQ entry on using
   <a href="faq.installation.php#faq.installation.apache2" class="link">Apache httpd with a threaded
   MPM</a>.
  </p>

  <p class="simpara">
   Most distribution packages of
   <code class="literal">mod_php</code> are not built with ZTS, so
   <code class="literal">prefork</code> is typically required. If you need a
   threaded MPM (recommended for better performance under load), use
   <a href="install.fpm.php" class="link">PHP-FPM</a> with
   <code class="literal">mod_proxy_fcgi</code> instead.
  </p>
 </p></blockquote>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="simpara">
   The <a href="faq.installation.php#faq.installation.apache.multiviews" class="link">Apache MultiViews
   FAQ</a> discusses using multiviews with PHP.
  </p>
 </p></blockquote>
</div><?php manual_footer($setup); ?>