<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'install.unix.apache2.php',
    1 => 'Unix 系统下的 Apache 2.x',
    2 => 'Unix 系统下的 Apache 2.x',
  ),
  'up' => 
  array (
    0 => 'install.unix.php',
    1 => '在 Unix 系统上安装 PHP',
  ),
  'prev' => 
  array (
    0 => 'install.unix.commandline.php',
    1 => 'CGI 和命令行设置',
  ),
  'next' => 
  array (
    0 => 'install.unix.nginx.php',
    1 => 'Unix 系统下的 Nginx 1.4.x',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    '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">Unix 系统下的 Apache 2.x</h2>

 <p class="para">
  本节包括在 Unix 平台的 Apache 2.x 下安装 PHP 的说明和提示。
 </p>
   
 <div class="warning"><strong class="warning">警告</strong><p class="para">不推荐在使用 Apache 2
的产品中使用线程化 MPM。应使用预分支 MPM，Apache 2.0 和 2.2
默认的 MPM。其原因见 FAQ 中的相关条目<a href="faq.installation.php#faq.installation.apache2" class="link">使用线程化 MPM 的
Apache2</a>。</p></div>
    
 <p class="para">
  推荐阅读 <a href="http://httpd.apache.org/docs/current/" class="link external">&raquo;&nbsp;Apache 文档</a>，了解一下
  Apache 2.x 服务器，以及详细的安装参数。
 </p>

 <p class="para">
  可以从 <a href="http://httpd.apache.org/" class="link external">&raquo;&nbsp;Apache 下载站点</a>下载最新版本的Apache，并且根据上文选择合适版本的
  PHP 下载。本向导仅包含最基础的内容，只能让 Apache 2.x 和 PHP
  能够正常工作。更多信息请阅读 <a href="http://httpd.apache.org/docs/current/" class="link external">&raquo;&nbsp;Apache
  文档</a>。这里省略所有的版本号，以保证本文的正确性。需要将本文的“NN”替换为相应的版本号。
 </p>

 <p class="para">
  当前 Apache 2.x 有两个流行的版本 - 2.4 和 2.2。虽然选择某个版本会有种种原因，但是如果可以考虑的话，我们还是建议使用最新的
  Apache 2.4 版本。当然，以下的介绍同样适合 Apache 2.4 和 2.2。请注意 Apache 2.2 已经不再被官方支持，并且不再发布新功能和补丁更新。
 </p>

 <ol type="1">
  <li class="listitem">
   <p class="para">
    从上面列出的地方获取 Apache 源码包，然后解压：
   </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">
    同样，获取 PHP 源码包并解压：
   </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">
    编译并安装 Apache。请参考 Apache 安装文档了解编译 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">
    现在已经将 Apache 2.x.NN 安装在 /usr/local/apache2。本安装支持可装载模块和标准的
    MPM prefork。之后，可以使用如下命令启动 Apache 服务器：

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

    如果成功，可以停止 Apache 服务器并继续安装 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">
    现在需要配置并编译 PHP。在这里可以用各种各样的参数来自定义
    PHP，例如启动哪些扩展功能包的支持等。用 <strong class="command">./configure --help</strong>
    命令可以列出当前可用的所有参数。在此例中，将给出一个在有
    MySQL 支持的 Apache 2 上进行配置的范例。
   </p>

   <p class="para">
    如果按照上面的说明从源代码编译了 Apache，下面的例子会正确匹配 <strong class="command">apxs</strong>
    的路径。如果通过其他方式安装了 Apache，需要相应的调整 <strong class="command">apxs</strong> 
    的路径。注意，在有些发行版本中，可能将 <strong class="command">apxs</strong> 更名为 <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">
    如果决定在安装后改变配置选项，只需重复最后的三步
    <strong class="command">configure</strong>，<strong class="command">make</strong>，以及 <strong class="command">make install</strong>，然后需要重新启动 Apache
    使新模块生效。Apache 不需要重新编译。
   </p>
         
   <p class="para">
    请注意，除非明确有提示，否则“make install”命令将安装 PEAR、各种
    PHP 工具诸如 phpize，并安装 PHP CLI 等等。
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    配置 <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">
    可以编辑 <code class="literal">.ini</code> 来设置 PHP
    选项。如果想要把 <var class="filename">php.ini</var> 放到另外的位置，需要在步骤
    5 使用 <code class="literal">--with-config-file-path=/some/path</code> 选项。
   </p>
   
   <p class="para">
    如果选择了 <var class="filename">php.ini-production</var>，请务必阅读其中的变更列表，它们将如何影响
    PHP 的行为。
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    编辑 <var class="filename">httpd.conf</var> 文件以调用 PHP 模块。<code class="literal">LoadModule</code>
    表达式右边的路径必须指向系统中的 PHP 模块。以上的 <strong class="command">make install</strong>
    命令可能已经完成了这些，但务必要检查。
   </p>

   <div class="informalexample">
    <p class="para">
     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">
     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">
    告知 Apache 将特定的扩展名解析成 PHP，例如，让 Apache 将扩展名 <code class="literal">.php</code>
    解析成 PHP。为了避免潜在的危险，例如上传或者创建类似 <var class="filename">exploit.php.jpg</var>
    的文件并被当做 PHP 执行，我们不再使用 Apache 的 <code class="literal">AddType</code>
    指令来设置。参考下面的例子，你可以简单的将需要的扩展名解释为 PHP。
    下面将添加 <code class="literal">.php</code> 来进行演示。
   </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">
    或者也想将 <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>
    以及 <code class="literal">.phtml</code> 文件都当做 PHP 来运行，我们无需额外的设置，仅需按照下面这样来：
   </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">
    然后，可以将 <code class="literal">.phps</code> 文件由 PHP 源码过滤器处理，使得其在显示时可以高亮源码，设置如下：
   </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> 也有助于将那些不需要运行的 <code class="literal">.php</code> 文件的源码高亮显示，而并不需要将他们更名为 <code class="literal">.phps</code> 文件：
   </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">
    不要在正式生产运营的系统上启动 PHP 源码过滤器，因为这可能泄露系统机密或者嵌入的代码中的敏感信息。
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    按照通常的方式启动 Apache 服务：
   </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">或者</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">
  按照上面的步骤便可以使 Apache 2.ｘ 将 PHP 作为 <code class="literal">SAPI</code>
  模块。当然 Apache 和 PHP 都还有很多配置选项，可以在相应的源代码目录中使用
  <strong class="command">./configure --help</strong> 获得更多信息。
 </p>

 <p class="para">
  可以选择 <var class="filename">worker</var> MPM 而不是标准的
  <var class="filename">prefork</var> MPM 来编译多线程版本的 Apache。只需在上面的第
  3 步，将选项使用如下参数传递到 <strong class="command">./configure</strong>中：
 </p>

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

 <p class="para">
  如果不是很明确这样做的后果并且大概理解其含义的话，最好不要进行这一步。更多信息请参考
  Apache 文档中关于 <a href="http://httpd.apache.org/docs/current/mpm.html" class="link external">&raquo;&nbsp;MPM-Modules</a> 的部分。
 </p>

 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <p class="para">
   <a href="faq.installation.php#faq.installation.apache.multiviews" class="link">Apache MultiViews
   常见问题</a>中讨论了在 PHP 中使用 MultiViews。
  </p>
 </p></blockquote>

 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <p class="para">
   要编译多线程版本的 Apache，系统必须支持多线程。这也意味着需要将 PHP
   编译为 Zend 线程安全（ZTS）。在此配置下。并不是所有的扩展都可以使用。推荐设置是使用默认的
   <var class="filename">prefork</var> MPM-Module 编译 Apache。
  </p>
 </p></blockquote>
</div><?php manual_footer($setup); ?>