Question

PHP with Nginx downloads instead of renders

Posted August 18, 2021 92 views
NginxPHPUbuntu 16.04

I’m sure this is kind of a duplicate question, but I’ve tried googling this to no avail. I’m trying to make a site that will just run a php script on a button press. I’m trying to get php to work right, so I have an info.php file here:

    <?php
    phpinfo();
    ?>

Now, whenever I try to go to this file in my webpage: jarofmilk.com/info.php it just tries to download it instead of render it. I feel like I’ve tried everything under the sun to fix this, but I have no clue whats going wrong with it.

Here’s my default nginx conf:

    server {
        listen 8000 default_server;
        listen [::]:8000 default_server;
        server_name jarofmilk.com www.jarofmilk.com;

        root /var/www/jarofmilk.com/public_html;
        index index.php index.html index.htm index.nginx-debian.html;


        location / {
                try_files $uri $uri/ /index.html;
        }
        location ~ \.php$ {
                try_files $uri =404;
                include snippets/fastcgi-php.conf;

                # With php5-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                #fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                #fastcgi_index index.php;
                include fastcgi_params;
            }
        location ~ /\.ht {
                deny all;
            }
    }


PHP Version:

php --version
PHP 7.0.33-0ubuntu0.16.04.16 (cli) ( NTS )

Nginx Version:

    $ nginx -v
    nginx version: nginx/1.10.3 (Ubuntu)

Ubuntu Version:


    Ubuntu 16.04.6 LTS

I appreciate any insight. I’ve already uninstalled and reinstalled php.
I’m sure this is a repeat question, but I’ve looked over most of the other stackoverflow results, and nothing worked for me.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

The configuration looks good actually. I could suggest a couple of things:

  • Make sure that the PHP-FPM service is up and running:
systemctl status php7.0-fpm.service
  • Make sure that the listen address is set to the socket file that you’ve specified in your Nginx config. You can do that by viewing the following file:
/etc/php/7.0/fpm/pool.d/www.conf

And search for the following line

listen = /run/php/php7.0-fpm.sock

If those things are all good, I would recommend clearing your browser’s cache and also checking your Nginx error log:

tail -100 /var/log/nginx/error.log

Regards,
Bobby