Web Servers

How To Configure Nginx For PHP Page

In this article, we have shown how to Configure Nginx for PHP page on CentOS.

For this tutorial, we’ve installed NGINX. This tutorial will cover the basic configuration of the NGINX webserver to process PHP application and server them on port 80. It is recommended you study NGINX and PHP-FPM.


  • Dedicated or cloud server with CentOS installed.
  • NGINX installed on CentOS.
  • PHP installed.
  • SSH as a root user (You can use a normal user with sudoer rights, use 'sudo' with every command).

Here are the articles for the installation of the Nginx and the installation of the PHP.

Once you install both, open main PHP-fpm configuration file:

# vi /etc/php.ini

We are looking for the parameter that sets cgi.fix_pathinfo. We will be uncommented by removing semi-colon(;) and set to 0.

This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if a PHP file does not match exactly. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn’t be allowed to execute.

So, we will change both of these conditions by uncommenting the line and setting it to “0” like this:


Once you have done, save and exit.

Next, restart php-fpm service:

# systemctl restart php-fpm

Configure Nginx server block to use PHP Processor

# vi /etc/nginx/sites-available/default

note: the server block file name might be different as shown above.

The default server block of the Ngnix might look like:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

server_name localhost;

location / {
try_files $uri $uri/ =404;

  • Here we need to add an index.php index directive to allow PHP files to be served when a directory is requested.
  • Modify the server_name directive. Replace localhost with your FQDN domain name or public IP address.
  • Add a try_files directive to make sure Nginx doesn’t pass bad requests to our PHP processor.

The server block looks like:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.php index.html index.htm;

server_name server_domain_name_or_IP;

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_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

Configure like above changes, now save and exit the file.

Restart Ngnix to make the necessary changes:

# systemctl restart nginx

Now, create a PHP test file:

# vi /usr/share/nginx/html/info.php

Add valid PHP code that return formatted information about our server:


Save and exit.

That’s it we have done required configuration as well as modifications.

Finally, let’s access the info.php file, that we have created earlier:


You should see a web page that has been generated by PHP with information about your server.

If you see a PHP information page generated by PHP, you’ve set up PHP processing with Nginx successfully.

For now, remove the file by typing:

sudo rm /usr/share/nginx/html/info.php

In this article, we have seen how our support engineers configures Nginx for the PHP page.

[Need assistance to fix this error or install tools? We’ll help you.]

Related Articles