PHP,广泛用于Web开发的脚本语言,在创建动态和交互式网站方面起着至关重要的作用。在部署PHP应用程序时,Debian 12,一个强大而可靠的Linux发行版,提供了一个稳定的基础。
在 Debian 12 书虫上安装 PHP
第 1 步。在我们安装任何软件之前,通过在终端中运行以下命令来确保您的系统是最新的非常重要:apt
<span class="pln">sudo apt update sudo apt install apt</span><span class="pun">-</span><span class="pln">transport</span><span class="pun">-</span><span class="pln">https lsb</span><span class="pun">-</span><span class="pln">release ca</span><span class="pun">-</span><span class="pln">certificates</span>
此命令将刷新存储库,允许您安装最新版本的软件包。
现在您的系统是最新的,是时候在 Debian 12 上安装 PHP 了。首先,通过在终端中执行以下命令,将 Ondřej Surý PHP 仓库添加到 Debian 12 中:
<span class="pln">wget </span><span class="pun">-</span><span class="pln">O </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">trusted</span><span class="pun">.</span><span class="pln">gpg</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">gpg https</span><span class="pun">:</span><span class="com">//packages.sury.org/php/apt.gpg</span><span class="pln"> echo </span><span class="str">"deb https://packages.sury.org/php/ $(lsb_release -sc) main"</span> <span class="pun">|</span><span class="pln"> sudo tee </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">sources</span><span class="pun">.</span><span class="pln">list</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">list</span>
这些命令将使您的系统能够从存储库中获取 PHP 包。
接下来,再次更新软件包列表,并通过运行以下命令安装 PHP 和必要的扩展:
<span class="pln">sudo apt update sudo apt install php8</span><span class="pun">.</span><span class="lit">1</span>
注: 根据您的要求,替换为所需的 PHP 版本,例如 。php8.1
php8.0
对于模块,do (将 EXTENSION 替换为相应的 PHP 模块):
<span class="pln">sudo apt install php8</span><span class="pun">.</span><span class="lit">1</span><span class="pun">-</span><span class="pln">EXNTENSION</span>
为了确保正确安装 PHP,让我们运行一个简单的测试:
<span class="pln">sudo nano </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">info</span><span class="pun">.</span><span class="pln">php</span>
将以下内容添加到文件中:
<span class="pun"><?</span><span class="pln">php phpinfo</span><span class="pun">();</span> <span class="pun">?></span>
保存文件并退出文本编辑器,然后打开 Web 浏览器并导航到 。您应该会看到一个 PHP 信息页面,其中显示了有关 PHP 安装的详细信息。http://your_server_IP/info.php
第 3 步。自定义 PHP 配置。
PHP 的默认配置可能并不总是符合项目的要求。让我们探讨如何自定义 PHP 设置以微调您的环境。
- PHP 配置概述:
PHP 配置通过文件进行管理。此文件包含控制 PHP 行为的各种指令:php.ini
- 修改 PHP 设置:
找到文件:php.ini
<span class="pln">sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="lit">8.1</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">ini</span>
在此文件中,您可以修改memory_limit、error_reporting和display_errors等设置。例如,要将内存限制增加到 256MB,请找到指令并按如下方式更新它:memory_limit
<span class="pln">memory_limit </span><span class="pun">=</span> <span class="lit">256M</span>
保存更改并重新启动 Web 服务器以使新配置生效:
<span class="pln">sudo systemctl restart apache2</span>
第 4 步。故障排除提示。
即使仔细安装,也可能会出现问题。以下是一些常见的故障排除提示。
- 通过运行以下命令验证 PHP 软件包是否已正确安装:
<span class="pln">php </span><span class="pun">-</span><span class="pln">v</span>
- 检查 Web 服务器日志中是否有任何可能指示配置错误或其他问题的错误消息:
<span class="pln">sudo tail </span><span class="pun">-</span><span class="pln">f </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">error</span><span class="pun">.</span><span class="pln">log</span>
- 确保已安装应用程序所需的 PHP 扩展。您可以使用以下命令搜索可用的 PHP 扩展:
<span class="pln">apt search php </span><span class="pun">|</span><span class="pln"> grep </span><span class="pun">^</span><span class="pln">php</span>
感谢您使用本教程在 Debian 11 书虫上安装最新版本的 PHP 编程语言。有关其他帮助或有用信息,我们建议您查看 PHP 官方网站。