Home > Backend Development > PHP Tutorial > Why is Nginx Serving My PHP Files as Downloads Instead of Executing Them?

Why is Nginx Serving My PHP Files as Downloads Instead of Executing Them?

DDD
Release: 2024-12-02 19:04:10
Original
194 people have browsed it

Why is Nginx Serving My PHP Files as Downloads Instead of Executing Them?

Nginx Serving PHP Files as Downloads: Resolving the Execution Issue

When encountering situations where Nginx serves PHP files as downloads instead of executing them, it's essential to diagnose the underlying cause.

Checking Configuration Files:

  • /etc/nginx/sites-available/default: Ensure that both IPv4 and IPv6 listen directives are uncommented. Also, verify that index.php is included in the index line.
  • PHP Settings: Check /etc/php5/fpm/php.ini and confirm that cgi.fix_pathinfo is set to 0. This parameter prevents the web server from appending the file extension to the FastCGI URI.

Configuration Example:

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

        root /var/www/html;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

               location ~ \.php$ {
                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    ## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #               # With php5-cgi alone:
    #               fastcgi_pass 127.0.0.1:9000;
    #               # With php5-fpm:
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
  

              location / {
                    
                    try_files $uri $uri/ =404;
                    # Uncomment to enable naxsi on this location
                    # include /etc/nginx/naxsi.rules
            }
}
Copy after login

Restart Services:

After making the necessary changes, restart both Nginx and php5-fpm using the following commands:

sudo service nginx restart
sudo service php5-fpm restart
Copy after login

Additional Considerations:

  • Ensure that PHP is installed and configured correctly on the server.
  • If the issue persists, check the error logs for any additional clues.

The above is the detailed content of Why is Nginx Serving My PHP Files as Downloads Instead of Executing Them?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template