1. Home
  2. Web Server
  3. How to configure Nginx with PHP and FastCGI on CentOS

How to configure Nginx with PHP and FastCGI on CentOS

We saw how to install Nginx using yum on CentOS and with luck going bad encountered errors like eventfd() failed and PCRE library problems. Found a quick solution of building Nginx from source to solve eventfd() error and installing PCRE library for PCRE library issues during compiling Nginx from source. Since i want to run wordpress on Nginx, before that, i should enable PHP on Nginx. Whenever i open a simple php file through browser, it started to download rather than executing. This tutorial shows you how you can enable PHP on Nginx.

Step 1: How to Install PHP and PHP-FPM

We saw how to install latest version of PHP. PHP on Nginx works through PHP-FPM(FastCGI Process Manager). PHP-FPM is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. Now we shall see how to install PHP-FPM. Follow the below command to install php-fpm using yum.

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

This installs php-fpm corresponds to php version 5.5 which we saw in article : How to install php 5.5

Step 2: How to configure PHP-FPM

PHP-FPM configuration files would be found in the below locations:

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

With your favorite editor open file /etc/php-fpm.d/www.conf and locate the line below:

listen = 127.0.0.1:9000

This corresponds to run FastCGI server on port 9000.

Step3 : How to Start PHP-FPM

PHP-FPM is a daemon process that runs a FastCGI server on port 9000. You can start the php-fpm process with the init script as shown below:

/etc/init.d/php-fpm start

Step 4: Configure Nginx to enable PHP

Goto Nginx configuration directory(By default /etc/nginx) and with your favorite editor open the file /etc/nginx/nginx.conf and uncomment the below lines of code:

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

Locate the line fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; and modify it as fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;. Also locate the line root html; and modify the html with your exact location of the html directory. In the above example, i have my html directory configured at /etc/nginx/html  for temporary purposes.

Step 5: Re-start Nginx service

Follow the guide to configure init script to start Nginx service. Once configured, restart the Nginx service as shown below:

/etc/init.d/nginx restart

Now am happy to see the PHPinfo() page :

Enable PHP on Nginx

Updated on November 20, 2017

Was this article helpful?

Related Articles

Leave a Comment