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:
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 } }
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
Additional Considerations:
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!