HRR Co., Ltd.

技術的な記録を残していくことを目的としています。1次情報を大事にしています。

VirtualBox + CentOS7 + Nginx + PHP-FPM でPHPを動かすまで

はじめに

登場人物がインストール済みで、設定周りをどうするか、のメモになります。
というわけで、前提条件が結構あります。

  • PHPはインストール済み(記事を書いたものの保存し忘れでサヨウナラ…)
  • PHP-FPMもインストール済み
  • Nginxもインストール済み
  • 前回の記事の続きみたいな感じです。

hrroct.hatenablog.com

ゴール

http://localhost:8080/index.php にアクセスして、index.phpに書いたphpinfo()の結果が表示されるところまで。

やったこと

設定ファイルを変更する前にバックアップを取ることをオススメします。

PHP-FPMの設定

# vi /etc/php-fpm.d/www.conf

Nginxで動かすので、userとgroupの変更を行います。

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
;user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache
group = nginx

さらに今回の場合、ブラウザからアクセスするときにポート番号を8080にしているので、 listenのポート番号を変更します。

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = 127.0.0.1:8080

忘れずに再起動も行います。

# systemctl restart php-fpm

Nginxの設定

まずrootのindexファイルとして、index.phpを許可します。
rootも必要に応じて変更しましょう。

# vi /etc/nginx/conf.d/default.conf

  location / {
      root   /usr/share/nginx/html;
-     index  index.html index.htm;
+     index  index.html index.htm index.php;
  }

さらに、rootやポート番号、fastcgi_paramを書き換えます。

- #location ~ \.php$ {
- #    root           html;
- #    fastcgi_pass   127.0.0.1:9000;
- #    fastcgi_index  index.php;
- #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
- #    include        fastcgi_params;
- #}
+ location ~ \.php$ {
+     root           /usr/share/nginx/html;
+     fastcgi_pass   127.0.0.1:8080;
+     fastcgi_index  index.php;
+     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
+     include        fastcgi_params;
+ }

Nginxの再起動も忘れずに。

# systemctl restart nginx

SELinuxの設定

私の場合はこれも必要でした。

# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=enforcing
SELINUX=disabled

index.phpを作成する

# vi /usr/share/nginx/html/index.php
<?php
phpinfo();

設定は以上です。

確認

…する前に、ログファイルを監視することをオススメします。
うまくいかない場合、何かしらログに残っているはずです。

# tail -f /var/log/php-fpm/error.log /var/log/nginx/{access,error}.log

エラーコードやエラー内容で検索すれば、きっと解決できることでしょう。

http://localhost:8080/index.php
にアクセスして、インストールされたPHPの情報が表示されればOKです。

最後に

今や検索すれば、設定に困ることはなくなりましたね。
似たようなことをやっている方は、必ずいらっしゃるものですから。

以上でした!