16.6.2.2. nginx Web サーバーでの PHP の使用
nginx Web サーバーを介して PHP コードを実行できます。以下の手順は、PHP と連携するように nginx をインストールして設定する方法を示しています。
前提条件
PHP スクリプト言語がシステムにインストールされている。
PHP スクリプト言語のインストール を参照してください。
手順
nginxモジュールストリームをインストールします。stream はインストールするnginxのバージョン (例: 1.18) に置き換えてください。# yum module install nginx:stream# yum module install nginx:1.18PHP のインストール後、
nginxサーバーを起動します。すでに実行中の場合はnginxサービスを再起動します。# systemctl start nginx# systemctl restart nginxphp-fpmサービスを起動します。# systemctl start php-fpm必要に応じて、両方のサービスが起動時に開始できるようにします。
# systemctl enable php-fpm nginxPHP の設定に関する情報を取得するには、以下の内容を含む
index.phpファイルを/usr/share/nginx/html/ディレクトリーに作成します。# echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.phpindex.phpファイルを実行するには、ブラウザーで以下を指定します。http://<hostname>/オプション: 特定の要件がある場合は、設定を調整します。
-
/etc/nginx/nginx.conf-nginxmain configuration -
/etc/nginx/conf.d/php-fpm.conf-nginxの FPM 設定 -
/etc/php-fpm.conf- FPM の主な設定 -
/etc/php-fpm.d/www.conf- デフォルトのwwwプール設定
-
例 - "Hello, World!" を実行します。PHP スクリプト:
/usr/share/nginx/html/ディレクトリー内にプロジェクト用のhelloディレクトリーを作成します。# mkdir hello以下の内容で
/usr/share/nginx/html/hello/ディレクトリーにhello.phpファイルを作成します。# <!DOCTYPE html> <html> <head> <title>Hello, World! Page</title> </head> <body> <?php echo 'Hello, World!'; ?> </body> </html>nginxサーバーを起動します。# systemctl start nginxhello.phpファイルを実行するには、ブラウザーに以下を指定します。http://<hostname>/hello/hello.php結果として、"Hello, World!" というテキストを含む Web ページが表示されます。