NginxとPHP-FPMをインストールしたら、とりあえずPHPを動かしたいよね。
NginxでPHPが動いた設定ファイルを残しておく。
ちなみに変更したのはNginxとPHP-FPMだけで、PHP自体の設定は変えてない。
下記の変更以外は全てデフォルトのまま。
環境は
- CentOS6.5
- Nginx1.7.4
- PHP5.5.16
- PHP-FPM5.5.16
1. Nginxの設定
まずはNginxの設定ファイルから。
sudo vim /etc/nginx/nginx.conf
以下のような設定。
このファイルは何も変更してなかった・・・はず・・・
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
最後の行の「include /etc/nginx/conf.d/*.conf;」で各バーチャルサーバの設定をインクルードしている。
バーチャルサーバはApacheのバーチャルホストのこと。
そのバーチャルサーバのデフォルトの設定ファイルを開く。
こちらは変更する。
sudo vim /etc/nginx/conf.d/default.conf
以下のような感じ。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
#ここを変更
location / {
root /var/www;
index index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#ここを変更
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
「location /」ディレクティブ内の「root」ディレクティブと「index」ディレクティブを変更する。
「root」ディレクティブは自分のサイトを置くところディレクトリを指定。
「index」ディレクティブはURLでファイルを指定しなかった場合に実行するファイル。
「location ~ \.php$」ディレクティブはコメントアウトを全て外し、「root」と「fastcgi_param」を変更する。
「root」は同一のもの。
「fastcgi_param」ディレクティブは「/scripts」から「$document_root」に変更している。
設定を変更したら再起動する。
sudo /etc/init.d/nginx restart
エラーが出なかったらOK。
出たら・・・がんばれ。
2. PHP-FPMの設定
続いてPHP-FPMの設定ファイルを開く。
sudo vim /etc/php-fpm.d/www.conf
変更した箇所は「user」と「group」と「listen.owner」と「listen.group」だけ。
; Start a new pool named 'www'. [www] ; 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 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses 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 ; Set listen(2) backlog. A value of '-1' means unlimited. ; Default Value: -1 ;listen.backlog = -1 ; List of ipv4 addresses of FastCGI clients which are allowed to connect. ; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original ; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address ; must be separated by a comma. If this value is left blank, connections will be ; accepted from any ip address. ; Default Value: any listen.allowed_clients = 127.0.0.1 ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. Many ; BSD-derived systems allow connections regardless of permissions. ; Default Values: user and group are set as the running user ; mode is set to 0660 listen.owner = nginx listen.group = nginx ;listen.mode = 0660 ; 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 Choosed to be able to access some dir as httpd user = nginx ; RPM: Keep a group allowed to write in log dir. group = nginx
PHP5.5.12以降では「listen.owner」と「listen.group」もnginxと指定しなければならないらしい。
PHP-FPMも再起動する。
sudo /etc/rc.d/init.d/php-fpm restart
3. PHPファイルの作成・設置
PHPファイルはおなじみのphpinfo()を作ろう。
ドキュメントルートへ移動。Nginxのバーチャルサーバの設定ファイルで指定したとこだ。
cd /var/www
ここにphpファイルを作成。
sudo vim index.php
<?php phpinfo();
ドメインとってないならIPアドレスだけ、ドメイン設定したならドメインだけにアクセス。
PHPの画面が表示されたらOK。
ダメなら一度VPSを再起動するのもいいかも。

コメント