ホーム Linux CentOS 7 nginxでPHPを動かす設定

CentOS 7 nginxでPHPを動かす設定

8661
0

Apacheとは違い、nginxではデフォルトの状態では動作するどころかphpファイルにアクセスするとそのアクセスしたファイルがダウンロードとなってしまいます。

そこで今回は、nginxでphpを動かす設定を行っていきます。

PHP-FPMのインストール

php-fpmは、yumでインストールが可能なので以下のコマンドを使用してインストールを行います。

※環境はPHP7がインストールされている事を前提としています。

yum install --enablerepo=remi,remi-php70 php php-fpm

PHP-FPMの設定変更

www.confというファイルに記述してある以下の箇所を変更します。

# vi /etc/php-fpm.d/www.conf
user = apache
group = apache

上記の部分を以下の様に変更

user = nginx
group = nginx

PHP-FPMの起動

# systemctl start php-fpm
# systemctl enable php-fpm

nginxのドメイン設定ファイルの変更

設定ファイルは、/etc/nginx/sites-available/example.com

■変更前

server {
        listen 80;
        server_name example.com;

        access_log /var/log/nginx/example.com_access.log main;

        location / {
                root /var/www/vhosts/example.com/httpdocs;
                index index.html index.php;
        }
}

■変更後

server {
	listen 80;
	server_name example.com;

	access_log /var/log/nginx/example.com_access.log main;
	error_log /var/log/nginx/example.com_error.log;
	root /var/www/vhosts/example.com/httpdocs;
	index index.html index.php;

	location ~ \.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
	}

}

nginxの再起動

# systemctl start nginx

動作確認

nginxの再起動を行ったら以下の通りドキュメントルートの中にinfo.php等のファイルを作成し、以下のコードを入力してみましょう。

上記の様にPHPの情報が表示されれば、PHPが動作していることが確認できます。
もし、phpへアクセスしてもダウンロードや、Bad Request等が表示される場合は、記述が間違っている事が考えられるため、設定を再度見直してみましょう。

[ad#g]

[ad#g]