Raspberry Pi NGINXのPHP実行環境(php-fpm)

Raspberry Pi

はじめに

NGINXを使用したPHP実行環境を作成する方法について記載します。
apacheでは、モジュール版のPHPが使用されることがほとんどだと思いますが、nginxでは、CGI版の「php-fpm」を使用します。
PHP-FPM(FastCGI Process Manager)は、PHPのFastCGI 実装のひとつです。
本文章では、NGINXおよびPHPは、インストールされているものとします。

環境

  1. ボード
    Raspberry Pi 4 Model B
  2. OS
    Raspberry Pi OS (32-bit) Lite
    Minimal image based on Debian Buster
    Version: August 2020
    Release date: 2020-08-20
    Kernel version: 5.4

パッケージのインストール

nginxとphp及びphp-fpmのインストールを行います。ここでは、php7.3を使用します。

pi@raspberrypi:~ $ sudo apt-get install nginx
pi@raspberrypi:~ $ sudo apt-get install php7.3 php7.3-fpm
pi@raspberrypi:~ $

php-fpmの設定

「etc/php/7.3/fpm/pool.d/www.conf」と「/etc/php/7.3/fpm/php.ini」の設定を行う必要があります。
この設定ファイルのパスは使用する環境に依存しますので、以下のコマンドを実行し設定ファイルの場所を確認してください。

pi@raspberrypi:~ $ sudo find / \( -iname "php.ini" -o -name "www.conf" \)
/etc/php/7.3/apache2/php.ini
/etc/php/7.3/fpm/pool.d/www.conf
/etc/php/7.3/fpm/php.ini
/etc/php/7.3/cli/php.ini
pi@raspberrypi:~ $

「www.conf」ファイルの設定

はじめに、「www.conf」ファイルの設定を行います。
PHP-FPMでは、FastCGIのリクエストを受け付ける方法として、TCP/IPソケットとUNIXソケットを使用できますが、ここでは、UNIXソケットを使用します。
FastCGIのリクエストを受け付ける場所(UNIXソケット)、UNIXソケットのlisten.ownerとlisten.groupの設定を行います。
(注)listen.ownerとlisten.groupは、NGINXの実行ユーザーとグループと一致させる必要があります。

pi@raspberrypi:~ $ vi /etc/php/7.3/fpm/pool.d/www.conf
【変更後】
listen = /run/php/php7.3-fpm.sock
listen.owner = www-data
listen.group = www-data
pi@raspberrypi:~ $

nginxの実行ユーザーとグループを確認するには、以下のコマンドを実行します。

pi@raspberrypi:~ $ ps -aux | grep nginx
root      1336  0.0  0.0  50224  1256 ?        Ss   06:43   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data  1337  0.0  0.1  50368  2784 ?        S    06:43   0:00 nginx: worker process
www-data  1338  0.0  0.1  50368  2784 ?        S    06:43   0:00 nginx: worker process
www-data  1339  0.0  0.1  50368  2784 ?        S    06:43   0:00 nginx: worker process
www-data  1340  0.0  0.1  50368  2784 ?        S    06:43   0:00 nginx: worker process
pi       10119  0.0  0.0   7348   580 pts/0    S+   06:59   0:00 grep --color=auto nginx
pi@raspberrypi:~ $

上記では、NGINXのマスタープロセスは「root」で実行され、ワーカーププロセスは「www-data」で実行されていることがわかります。

「php.ini」ファイルの設定

悪意のあるスクリプトの実行を防止するため、以下のように変更します。

pi@raspberrypi:~ $ sudo vi /etc/php/7.3/fpm/php.ini
【変更前】
;cgi.fix_pathinfo=1
【変更後】
cgi.fix_pathinfo=1
pi@raspberrypi:~ $

PHP-FPMの設定の反映

変更を行った設定を反映させるため、php-fpmのサービスを再起動します。

pi@raspberrypi:~ $ sudo systemctl restart php7.3-fpm.service
pi@raspberrypi:~ $

問題なくphp-fpmサービスが起動していることを確認します。

pi@raspberrypi:~ $ sudo systemctl status php7.3-fpm.service
pi@raspberrypi:~ $

NGINXの設定

NGINXの設定を行います。

「/etc/nginx/sites-enabled/default」ファイルの設定

「/etc/nginx/sites-enabled/default」ファイルを以下のように編集します。

pi@raspberrypi:~ $ sudo vi /etc/nginx/sites-enabled/default
        location ~ \.php$ {
               include snippets/fastcgi-php.conf;     <--- コメントアウトを削除
        #
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/run/php/php7.3-fpm.sock;     <--- コメントアウトを削除
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
pi@raspberrypi:~ $

「location ~* .php$」は、設定をルートディレクトリ配下のサブディレクトリを含めた「.php」に対して適用することを意味します。
「*」は、大文字・小文字を区別しないことを意味します。
「fastcgi_pass」は、www.confファイルに記載した「listen =」の値と一致させます。(UNIXソケットの場所)

参考までに、上記のファイルでインクルードされている「snippets/fastcgi-php.conf」ファイル、「snippets/fastcgi-php.conf」でインクルードされている「fastcgi.conf」ファイルの内容を記載します。

pi@raspberrypi:~ $ cat /etc/nginx/snippets/fastcgi-php.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;
pi@raspberrypi:~ $
pi@raspberrypi:~ $ cat /etc/nginx/fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
pi@raspberrypi:~ $

NGINXの設定の反映

変更を行った設定を反映させるため、nginxのサービスを再起動します。

pi@raspberrypi:~ $ sudo systemctl restart nginx.service
pi@raspberrypi:~ $

nginxサービスが問題なく起動しているか確認します。

pi@raspberrypi:~ $ sudo systemctl status nginx.service
pi@raspberrypi:~ $

PHPの稼働確認

nginxでphpが問題なく使用できるか確認を行います。
ドキュメントルート(ここでは、/var/www/html)に「test.php」を作成します。

pi@raspberrypi:~ $ sudo vi /var/www/html/test.php
<?php
phpinfo();
?>
pi@raspberrypi:~ $

webブラウザからアクセスし、PHP設定情報が表示されることを確認します。
例:http://192.168.xxx.xxx/test.php

タイトルとURLをコピーしました