4.2. 通过 Web 服务器使用 PHP 脚本语言


4.2.1. 在 Apache HTTP 服务器中使用 PHP

在 Red Hat Enterprise Linux 10 中,Apache HTTP 服务器 允许您将 PHP 作为 FastCGI 进程服务器运行。FastCGI 进程管理器 (FPM)是 PHP FastCGI 守护进程的一种替代,它允许网站管理高负载。PHP 默认在 RHEL 10 中使用 FastCGI 进程管理器

先决条件

  • 在您的系统上安装 PHP 脚本语言。

流程

  • 配置 Apache HTTP 服务器和 php-fpm,以运行 PHP 脚本。

    1. 安装 httpd 软件包:

      # dnf install httpd
    2. 启动 Apache HTTP 服务器

      # systemctl start httpd

      或者,如果 Apache HTTP 服务器已在您的系统中运行,请在安装 PHP 后重启 httpd 服务:

      # systemctl restart httpd
    3. 启动 php-fpm 服务:

      # systemctl start php-fpm
    4. 可选: 在引导时启用这两个服务:

      # systemctl enable php-fpm httpd
    5. 要获取有关 PHP 设置的信息,请在 /var/www/html/ 目录中创建带有以下内容的 index.php 文件:

      # echo '<?php phpinfo(); ?>' > /var/www/html/index.php
    6. 要运行 index.php 文件,请将浏览器指向:

      http://<hostname>/
    7. 可选:如果您有具体要求,请调整配置:

      • /etc/httpd/conf/httpd.conf - 一般的 httpd 配置
      • /etc/httpd/conf.d/php.conf - httpd特定 PHP 配置
      • /usr/lib/systemd/system/httpd.service.d/php-fpm.conf - 默认情况下,php-fpm 服务与 httpd 一起启动
      • /etc/php-fpm.conf - FPM 主配置
      • /etc/php-fpm.d/www.conf - 默认 www 池配置
  • 运行 "Hello, World!"使用 Apache HTTP 服务器的 PHP 脚本.

    1. /var/www/html/ 目录中为您的项目创建一个 hello 目录:

      # mkdir hello
    2. /var/www/html/hello/ 目录中创建 hello.php 文件,其内容如下:

      # <!DOCTYPE html>
      <html>
      <head>
      <title>Hello, World! Page</title>
      </head>
      <body>
      <?php
          echo 'Hello, World!';
      ?>
      </body>
      </html>
    3. 启动 Apache HTTP 服务器

      # systemctl start httpd
    4. 要运行 hello.php 文件,请将浏览器指向:

      http://<hostname>/hello/hello.php

      因此,会显示带有 "Hello, World!" 文本的网页。

      如需更多信息,请参阅 httpd (8)httpd.conf (5)php-fpm (8) 手册页。

4.2.2. 使用带有 nginx web 服务器的 PHP

您可以通过 nginx web 服务器运行 PHP 代码。

先决条件

  • 在您的系统上安装 PHP 脚本语言。

流程

  • 配置 nginx web 服务器和 php-fpm,以运行 PHP 脚本。

    1. 安装 nginx 软件包:

      # dnf install nginx
    2. 启动 nginx 服务器:

      # systemctl start nginx

      或者,如果 nginx 服务器已在您的系统中运行,请在安装 PHP 后重启 nginx 服务:

      # systemctl restart nginx
    3. 启动 php-fpm 服务:

      # systemctl start php-fpm
    4. 可选: 在引导时启用这两个服务:

      # systemctl enable php-fpm nginx
    5. 要获取 PHP 设置的信息,请在 /usr/share/nginx/html/ 目录中使用以下内容创建 index.php 文件:

      # echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.php
    6. 要运行 index.php 文件,请将浏览器指向:

      http://<hostname>/
    7. 可选:如果您有具体要求,请调整配置:

      • /etc/nginx/nginx.conf - nginx 主配置
      • /etc/nginx/conf.d/php-fpm.conf - FPM 配置 nginx
      • /etc/php-fpm.conf - FPM 主配置
      • /etc/php-fpm.d/www.conf - 默认 www 池配置
  • 运行 "Hello, World!"使用 nginx 服务器的 PHP 脚本。

    1. /usr/share/nginx/html/ 目录中为您的项目创建一个 hello 目录:

      # mkdir hello
    2. /usr/share/nginx/html/hello/ 目录中创建一个包含以下内容的 hello.php 文件:

      # <!DOCTYPE html>
      <html>
      <head>
      <title>Hello, World! Page</title>
      </head>
      <body>
      <?php
          echo 'Hello, World!';
      ?>
      </body>
      </html>
    3. 启动 nginx 服务器:

      # systemctl start nginx
    4. 要运行 hello.php 文件,请将浏览器指向:

      http://<hostname>/hello/hello.php

      因此,会显示带有 "Hello, World!" 文本的网页。

      如需更多信息,请参阅 nginx (8)php-fpm (8) man page。

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部