nginxインストール用リポジトリの追加
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
OSのバージョンが異なる場合下記のサイトよりRPMを検索する
http://nginx.org/packages/centos/
nginxインストール
yum install nginx
nginx-php連携設定
nginxからPHPスクリプトを実行する際fastcgiという機構を利用します。
これを実現するために「PHP-FPM (PHP FastCGI Process Manager)」を導入します。
予めPHPをインストールしておきます。
PHP-FPMインストール
yum --enablerepo=remi-php70 install php-fpm
標準リポジトリを使用してPHPをインストールした場合は–enablerepo=remi-php70を削除してください。
PHP-FPMの設定
/etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000 listen = /var/run/php-fpm/php-fpm.sock listen.owner = apache listen.group = apache listen.mode = 0660 user = apache group = apache ;pm.max_children = 50 ;pm.min_spare_servers = 5 ;pm.max_spare_servers = 35 pm.max_children = 15 pm.min_spare_servers = 2 pm.max_spare_servers = 5
pm.max_childrenなどはサーバーのスペックに応じて適宜変更する。
userとgroupなどをapacheにするのは、yumでWEBアプリをインストールした場合、権限設定がが楽だからこのようにした。
PHPのエラーログを記録するよう下記の設定も実施しておく。
/etc/php-fpm.conf
catch_workers_output = yes
PHP-FPM起動
systemctl start php-fpm
PHP-FPMを自動起動するようにする
systemctl enable php-fpm
nginxの起動・自動起動設定
nginx起動
systemctl start nginx
nginxを自動起動するようにする
systemctl enable nginx
自動起動設定の確認
systemctl list-unit-files | grep nginx
webに使用するポートを解放する
firewall-cmd --permanent --zone=public --add-service=http firewall-cmd --permanent --zone=public --add-service=https firewall-cmd --reload
疎通確認
クライアントのWEBブラウザよりアクセスして無事にWelcome表示が表示されればインストール完了です
ドキュメントルートの変更・PHPの実行
ドキュメントルートを「/var/www/html」に変更しPHPを動作させる設定を行います。
ドキュメントルートの変更
/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 /usr/share/nginx/html; #index index.html index.htm; root /var/www/html; index index.html index.htm index.php; } # PHP実行設定 location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #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; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
赤字の部分を変更します。
nginxを再起動
systemctl restart nginx
phpの実行確認
下記のファイルを作成してブラウザで確認する
/usr/share/nginx/html/phpinfo.php
<?php phpinfo();
phpinfoの画面が表示されれば設定完了です。
表示されない場合は設定ファイルやPHP-FPMが起動しているかどうかを確認してみましょう。
PHP-FPMの起動確認
systemctl status php-fpm
PHPのログファイルは「/var/log/php-fpm」に出力されます。
yum --enablerepo=remi-php70 install phpmyadmin