Fix Index.php or info.php starts download instead of opening in NGINX?

PHP

Here is Solution For how to fix Index.php or info.php starts to download instead of opening and we can see in this tutorial.

Check the file vi /etc/php.ini

What we are looking for in this file is the parameter that sets cgi.fix_pathinfo. This will be commented out with a semi-colon (;) and set to “1” by default.We will change both of these conditions by uncommenting the line and setting it to “0” like this:

cgi.fix_pathinfo=0

Next, open the php-fpm configuration file www.conf:

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

 

Find the line that specifies the listen parameter, and change it so it looks like the following:

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

 

Next, find the lines that set the 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:

sudo systemctl start php-fpm

 

This will implement the change that we made.

Next, enable php-fpm to start on boot:

sudo systemctl enable php-fpm

 

We do this on the server block level (server blocks are similar to Apache’s virtual hosts). Open the default Nginx server block configuration file by typing:

sudo vi /etc/nginx/conf.d/default.conf

 

 

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;
    }
}

When you’ve made the above changes, you can save and close the file.

Restart Nginx to make the necessary changes:

sudo systemctl restart nginx

 

Test PHP Processing on your Web Server

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

 

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:

Test PHP Script

<?php phpinfo(); ?>

When you are finished, save and close the file.

Thats it Enjoy…

 

Leave a Reply

Your email address will not be published. Required fields are marked *