<?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 2.x on Unix systems',
    2 => 'Apache 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 2.x on Unix systems</h2>

 <p class="para">
  This section contains notes and hints specific to Apache 2.x installs
  of PHP on Unix systems.
 </p>
   
 <div class="warning"><strong class="warning">Warning</strong><p class="para">We do not recommend using a
threaded MPM in production with Apache 2. Use the prefork MPM, which is
the default MPM with Apache 2.0 and 2.2.
For information on why, read the related FAQ entry on using
<a href="faq.installation.php#faq.installation.apache2" class="link">Apache2 with a threaded MPM</a></p></div>
    
 <p class="para">
  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 2.x server.
  More information about installation options for Apache may be found
  there.
 </p>

 <p class="para">
  The most recent version of Apache HTTP Server 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 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 being used.
 </p>

 <p class="para">
  There are currently two versions of Apache 2.x - there&#039;s 2.4 and 2.2.
  While there are various reasons for choosing each, 2.4 is the current
  latest version, and the one that is recommended, if that option is
  available to you. However, the instructions here will work for either
  2.4 or 2.2. Note that Apache httpd 2.2 is officially End Of Life,
  and no new development or patches are being issued for it.
 </p>

 <ol type="1">
  <li class="listitem">
   <p class="para">
    Obtain the Apache HTTP server 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="para">
    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="para">
    Build and install Apache. Consult the Apache install documentation for
    more details on building Apache.
   </p>

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

  <li class="listitem">
   <p class="para">
    Now you have Apache 2.x.NN available under /usr/local/apache2,
    configured with loadable module support and the standard MPM prefork.
    To test the installation use your normal procedure for starting
    the Apache 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 go on 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="para">
    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 2 and MySQL support.
   </p>

   <p class="para">
    If you built Apache from source, as described above, the below example will
    match your path for <strong class="command">apxs</strong>, but if you installed Apache some other way, you&#039;ll
    need to adjust the path to <strong class="command">apxs</strong> accordingly. Note that some distros 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="para">
    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 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="para">
    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="para">
    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="para">
    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="para">
    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">
    <p class="para">
     For PHP 8:
    </p>

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

   </div>

   <div class="informalexample">
    <p class="para">
     For PHP 7:
    </p>

    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">LoadModule php7_module modules/libphp7.so</pre>
</div>
    </div>

   </div>
  </li>

  <li class="listitem">
   <p class="para">
    Tell Apache to parse certain extensions as PHP. For example, let&#039;s have
    Apache parse <code class="literal">.php</code> files as PHP. Instead of only using the Apache <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="para">
    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;\.ph(p[2-6]?|tml)$&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="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="para">
    Use your normal procedure for starting the Apache server, 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="para">
  Following the steps above you will have a running Apache2 web server with
  support for PHP as a <code class="literal">SAPI</code> module. Of course, there are
  many more configuration options available for Apache and PHP. For more
  information type <strong class="command">./configure --help</strong> in the corresponding
  source tree.
 </p>

 <p class="para">
  Apache may be built multithreaded by selecting the
  <var class="filename">worker</var> MPM, rather than the standard
  <var class="filename">prefork</var> MPM, when Apache is built. This is done by
  adding the following option to the argument passed to <strong class="command">./configure</strong>, in
  step 3 above:
 </p>

 <div class="informalexample">
  <div class="example-contents screen">
<div class="cdata"><pre>
--with-mpm=worker
</pre></div>
  </div>
 </div>

 <p class="para">
  This should not be undertaken without being aware of the consequences of
  this decision, and having at least a fair understanding of
  the implications. The Apache documentation
  regarding <a href="http://httpd.apache.org/docs/current/mpm.html" class="link external">&raquo;&nbsp;MPM-Modules</a>
  discusses MPMs in a great deal more detail.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   The <a href="faq.installation.php#faq.installation.apache.multiviews" class="link">Apache MultiViews
   FAQ</a> discusses using multiviews with PHP.
  </p>
 </p></blockquote>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   To build a multithreaded version of Apache, the target system must support threads.
   In this case, PHP should also be built with
   Zend Thread Safety (ZTS). Under this configuration, not all extensions will be available.
   The recommended setup is to build Apache with the default
   <var class="filename">prefork</var> MPM-Module.
  </p>
 </p></blockquote>
</div><?php manual_footer($setup); ?>