<?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.debian.php',
    1 => 'Debian GNU/Linux 及其相关发行版上从软件包中安装 PHP',
    2 => 'Debian GNU/Linux 及其相关发行版上从软件包中安装 PHP',
  ),
  'up' => 
  array (
    0 => 'install.unix.php',
    1 => '在 Unix 系统上安装 PHP',
  ),
  'prev' => 
  array (
    0 => 'install.unix.php',
    1 => '在 Unix 系统上安装 PHP',
  ),
  'next' => 
  array (
    0 => 'install.unix.dnf.php',
    1 => '在使用 DNF 的 GNU/Linux 发行版上通过软件包安装',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'install/unix/debian.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="install.unix.debian" class="sect1">
 <h2 class="title">Debian GNU/Linux 及其相关发行版上从软件包中安装 PHP</h2>
 <p class="para">
  虽然 PHP 可以从源码安装，但也可以通过 <a href="http://www.debian.org/" class="link external">&raquo;&nbsp;Debian GNU/Linux</a>
  中的软件包获取。其他基于 Debian 的发行版也是如此，例如 Ubuntu、Kali Linux 和 Linux Mint。
 </p>
 <div class="warning"><strong class="warning">警告</strong>
 <p class="para">
  第三方提供的版本被视为非官方版本，不直接受 PHP 项目支持。
  除非可以通过 <a href="https://www.php.net/downloads.php" class="link external">&raquo;&nbsp;官方下载区</a>的构建版重现，
  否则遇到的任何错误都应报告给这些非官方构建版的提供者。
 </p>
</div>
 <p class="para">
  可以使用 <strong class="command">apt</strong> 或 <strong class="command">aptitude</strong> 命令来安装软件包。本节中这两条命令可以互换。
 </p>
 <div class="sect2" id="install.unix.debian.apt">
  <h3 class="title">使用 APT</h3>
   <p class="simpara">
    首先，注意其它有关的包可能需要
    <code class="literal">libapache-mod-php</code> 集成入
    Apache 2，以及 PEAR 的 <code class="literal">php-pear</code>。
   </p>
   <p class="simpara">
    其次，在安装一个包之前，最好先确定该包是最新版。通常可以运行命令
    <strong class="command">apt update</strong>。
   </p>
   <div class="example" id="install.unix.debian.apt.example">
    <p><strong>示例 #1 Debian 下将 PHP 安装入 Apache 2 的例子</strong></p>
    <div class="example-contents">
<div class="shellcode"><pre class="shellcode"># apt install php-common libapache2-mod-php php-cli</pre>
</div>
    </div>

   </div>
   <p class="simpara">
    APT 将自动安装 Apache 2 的 PHP
    模块以及所有依赖的库并激活之。应重启动
    Apache 以使更改生效，例如：
   </p>
   <div class="example" id="install.unix.debian.apt.example2">
    <p><strong>示例 #2 安装完 PHP 后停止并启动 Apache</strong></p>
    <div class="example-contents">
<div class="shellcode"><pre class="shellcode"># /etc/init.d/apache2 stop
# /etc/init.d/apache2 start</pre>
</div>
    </div>

   </div>
  </div>

  <div class="sect2" id="install.unix.debian.config">
   <h3 class="title">更好地控制配置</h3>
   <p class="simpara">
    上一节中 PHP 仅安装了核心模块。很可能还需要更多模块，例如
    <a href="book.mysql.php" class="link">MySQL</a>，<a href="book.curl.php" class="link">cURL</a>，<a href="book.image.php" class="link">GD</a>
    等。这些模块也可以通过 <strong class="command">apt</strong> 命令安装。
   </p>
   <div class="example" id="install.unix.debian.config.example">
    <p><strong>示例 #3 取得 PHP 附加软件包的列表</strong></p>
    <div class="example-contents">
<div class="shellcode"><pre class="shellcode"># apt-cache search php
# apt search php | grep -i mysql
# aptitude search php</pre>
</div>
    </div>

   </div>
   <p class="simpara">
    软件包列表将包含大量软件包，其中包括基本 PHP 组件，比如 <code class="literal">php-cgi</code>、<code class="literal">php-cli</code>
    和 <code class="literal">php-dev</code> 以及许多 PHP 扩展。安装扩展时，将根据需要自动安装其他软件包以满足这些软件包的依赖关系。
   </p>
   <div class="example" id="install.unix.debian.config.example2">
    <p><strong>示例 #4 安装 PHP 的 MySQL 和 cURL 支持</strong></p>
    <div class="example-contents">
<div class="shellcode"><pre class="shellcode"># apt install php-mysql php-curl</pre>
</div>
    </div>

   </div>
   <p class="simpara">
    APT 会自动把适当的行添加到不同的 <var class="filename">php.ini</var>
    相关文件中去，例如
    <var class="filename">/etc/php/7.4/php.ini</var>，<var class="filename">/etc/php/7.4/conf.d/*.ini</var>
    等，并且根据扩展，还会添加类似
    <code class="literal">extension=foo.so</code> 的内容。不过还是需要重新启动
    web 服务器（例如 Apache）以使这些改动生效。
   </p>
  </div>

  <div class="sect2" id="install.unix.debian.faq">
   <h3 class="title">常见问题</h3>
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
     如果 PHP 脚本没有通过 web 服务器被解析，则有可能是 PHP
     没有被加入到 web 服务器的配置文件中，在 Debian
     中可能是 <var class="filename">/etc/apache2/apache2.conf</var>
     或类似文件。具体内容参见 Debian 手册。
     </span>
    </li>
   <li class="listitem">
    <span class="simpara">
     如果某扩展貌似已经安装，但其函数却又未定义，确保合适的 ini
     文件已被加载并且 web 服务器在安装后重新启动过。
    </span>
   </li>
  </ul>
 </div>
</div><?php manual_footer($setup); ?>