6.2. Web サーバーでの PHP スクリプト言語の使用

6.2.1. Apache HTTP Server での PHP の使用

Red Hat Enterprise Linux 9 では、Apache HTTP Server で PHP を FastCGI プロセスサーバーとして実行できます。FastCGI Process Manager (FPM) は、Web サイトで高負荷を管理できるようにする代替の PHP FastCGI デーモンです。RHEL 9 では、PHP はデフォルトで FastCGI Process Manager を使用します。

FastCGI プロセスサーバーを使用して PHP コードを実行できます。

前提条件

  • PHP スクリプト言語がシステムにインストールされている。

手順

  1. httpd パッケージをインストールします。

    # dnf install httpd
  2. Apache HTTP Server を起動します。

    # systemctl start httpd

    または、Apache HTTP Server をシステムで実行している場合は、PHP のインストール後に httpd サービスを再起動します。

    # systemctl restart httpd
  3. php-fpm サービスを起動します。

    # systemctl start php-fpm
  4. オプション:両方のサービスが起動時に開始できるようにします。

    # systemctl enable php-fpm httpd
  5. PHP の設定に関する情報を取得するために、以下の内容を含む index.php ファイルを /var/www/html/ ディレクトリーに作成します。

    # 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 プール設定

例6.1 "Hello, World!" の実行 Apache HTTP Server を使用した 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 Server を起動します。

    # systemctl start httpd
  4. hello.php ファイルを実行するには、ブラウザーに以下を指定します。

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

    結果として、"Hello, World!" というテキストを含む Web ページが表示されます。

6.2.2. nginx Web サーバーでの PHP の使用

nginx Web サーバーを介して PHP コードを実行できます。

前提条件

  • 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 の設定に関する情報を取得するには、以下の内容を含む index.php ファイルを /usr/share/nginx/html/ ディレクトリーに作成します。

    # 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 - nginx の FPM 設定
    • /etc/php-fpm.conf - FPM の主な設定
    • /etc/php-fpm.d/www.conf - デフォルトの www プール設定

例6.2 "Hello, World!" の実行 nginx サーバーを使用した PHP スクリプト

  1. プロジェクトの hello ディレクトリーを /usr/share/nginx/html/ ディレクトリーに作成します。

    # 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!" というテキストを含む Web ページが表示されます。