
Clker-Free-Vector-Images / Pixabay
nginxのインストール
以前の記事を参照。
PHP-FPMのインストール
nginx自体にはPHPを実行する機能ないので、PHP-FPMに実行させる。
PHP7.0を使用しているので、PHP7.0用のPHP-FPMをインストールする。
yum install --enablerepo=remi,remi-php70 php-fpm
PHP-FPMの設定
vi /etc/php-fpm.d/www.conf
下記のように修正する。
今回は高速なUNIXドメインソケットを使用する。
デフォルトだとApacheと連携することが前提になっているので実行ユーザ等をnginxへ変更する。
; 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
; 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 = /var/run/php-fpm/php-fpm.sock
PHP-FPMの起動
service php-fpm start
PHP-FPMの自動起動設定
chkconfig php-fpm on
nginxの設定
vi /etc/nginx/conf.d/default.conf
下記のように修正する。
server {
listen 80;
#server_name localhost;
server_name (ドメイン名)
#charset koi8-r;
charset utf-8
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
#index index.html index.htm;
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 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 /var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
nginxの起動
service nginx start
※同じportでApacheが動いている場合、競合するのであらかじめ終了させておくこと。
nginxの自動起動設定
chkconfig nginx on
※同じportでApacheが自動起動設定になっている場合、競合するのであらかじめoffにしておくこと。
これでnginxにおけるPHP実行環境の構築完了。