Question

PHP Files are downloading instead of Exexcuting

Posted June 10, 2020 201 views
NginxPHP Frameworks

Hi All,

I have a problem and I am stuck since the last 10days. I have followed every tutorial published on digital ocean and not able to figure out. I am trying to run PHP file on my Nginx web server and since I am not able to execute file instead of its downloading.

My conf file looks like this:

server {

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self-signed certs generated by the SSL-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/joshiviral.com/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.html index.nginx-debian.html;  

    server_name www.joshiviral.com joshiviral.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }


    }



    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #include snippets/fastcgi-php.conf;

    # With php-fpm (or other unix sockets):
    #fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }

    # deny access to .htaccess files if Apache's document root
    # concurs with Nginx's one
    #
    #location ~ /\.ht {
    #deny all;
    #}

    #listen [::]:443 ssl ipv6only=on; # managed by Certbot
    #listen 443 ssl; # managed by Certbot
    #ssl_certificate /etc/letsencrypt/live/joshiviral.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/joshiviral.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot







# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.

server {
listen 80;
listen [::]:80;

    server_name www.joshiviral.com joshiviral.com;

root /var/www/joshiviral.com/html;
index index.php index.html index.html index.nginx-debian.html;

    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/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    }


server {
    if ($host = www.joshiviral.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = joshiviral.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name www.joshiviral.com joshiviral.com;
  return 404; # managed by Certbot

}

I have also uncommented CGI path=‘0’ and also tried clearing my cache but since now not working. Can anyone let me know how I can figure it out? I have also followed tutorials

https://www.digitalocean.com/community/questions/php-files-are-downloading-instead-of-executing-on-nginx

https://stackoverflow.com/questions/25591040/nginx-serves-php-files-as-downloads-instead-of-executing-them

https://www.digitalocean.com/community/questions/php-files-are-downloading-instead-of-executing-on-nginx

But though not working. There is a weird thing I have installed php7.4.6. And while routing on the 7.4.6 folder, there is an Apache folder. Can anybody let me know how I can solve it?

Thanks in Advance.

edited by AHA

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.

×
1 answer

Hi there @viraljayeshjoshi94,

If you have Apache already installed this might be causing the issue for you. If you don’t need it, you could stop it by running the following commands:

  • sudo systemctl stop apache2
  • sudo systemctl disable apache2

After that make sure that your Nginx service is running:

  • sudo systemctl status nginx

If it is not running, you can start it with:

  • sudo systemctl start nginx

Let me know how it goes!
Regards,
Bobby

Submit an Answer