Guide for Nginx/PHP on CentOS 7

Step 1 — Install Nginx

Install the EPEL repository for additional packages like Nginx,

yum install epel-release

Now install Nginx,

yum install nginx

Start Nginx,

systemctl start nginx

Set Nginx to start on boot:

systemctl enable nginx

Step 2 — Install PHP

Install the php, php-mysql and php-fpm packages

yum install php php-mysql php-fpm

Configure the PHP Processor:

Edit the php-fpm config file,

nano /etc/php.ini

Uncomment the line and change it to 0:

cgi.fix_pathinfo=0

Next, edit the php-fpm config www.conf:

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

Find the the listen parameter line, and change it so it looks like this:

listen = /var/run/php-fpm/php-fpm.sock 

Next, find the lines "listen.owner and listen.group" and uncomment them. They should look like this:

listen.owner = nobody
listen.group = nobody 

Lastly, find the lines that set the user and group and change their values from “apache” to “nginx”:

user = nginx
group = nginx

Then save and quit. Now, we just need to start our PHP processor by typing:

systemctl start php-fpm

Next, set php-fpm to start on boot:

systemctl enable php-fpm

Step 3 — Configure Nginx to Process PHP Pages

Edit the file,

nano /etc/nginx/conf.d/default.conf

Make the changes as seen below,

server {
    listen       80;
    server_name  server_domain_name_or_IP;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        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;
    }
}

Restart Nginx to reload the configuration changes

systemctl restart nginx

Step 4 — Test PHP Processing on your Web Server

In order for Nginx to find the file and serve it correctly, it must be saved to the "web root". Which can be found at the following path on CentOS 7,

/usr/share/nginx/html/

To create the file at that location:

nano /usr/share/nginx/html/info.php

We want to put the valid PHP code, inside the blank file:

<?php phpinfo(); ?>

After this, save and close the file.

Visit the following page in your web-browser to verify that PHP processing works, Replace "your_server_IP_address" with your VMs actual IP address,

http://your_server_IP_address/info.php