NginxでPHPを表示してみる

※CentOS8の環境

Nginxをインストール。

$ sudo dnf install -y nginx
$ nginx -v
nginx version: nginx/1.14.1

nginx.confの中身を見ると、conf.dとdefault.dのファイルをincludeするようになっている。

$ cat /etc/nginx/nginx.conf
・・・
http {
    ・・・
    root         /usr/share/nginx/html;
    ・・・
    include /etc/nginx/conf.d/*.conf;

    server {
        ・・・
        include /etc/nginx/default.d/*.conf;
        ・・・
    }

    ・・・
}

現状、どちらのディレクトリも空。

$ ls /etc/nginx/conf.d
$ ls /etc/nginx/default.d

PHPをインストール。

$ sudo dnf install -y php
$ php --version
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
・・・

PHP-FPMも入っている。

$ php-fpm --version
PHP 7.2.24 (fpm-fcgi) (built: Oct 22 2019 08:28:36)
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

conf.dにはphp-fpm.confが作られ、

$ ls /etc/nginx/conf.d
php-fpm.conf

中身は下記のようになっている。

$ cat /etc/nginx/conf.d/php-fpm.conf
# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        server unix:/run/php-fpm/www.sock;
}

また、default.dにはphp.confが作られ、

$ ls /etc/nginx/default.d/
php.conf

中身は下記のようになっている。

$ cat /etc/nginx/default.d/php.conf
# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
index index.php index.html index.htm;

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   php-fpm;
}

nginxとphp-fpmの自動起動の設定をして、起動もする。

$ sudo systemctl enable --now nginx
$ sudo systemctl enable --now php-fpm

PHPのファイルを設置してみる。

$ sudo vi /usr/share/nginx/html/test.php
<?php
echo 'Hello, world!';

ブラウザで表示できた。
f:id:yk5656:20210129112513j:plain