Home > Backend Development > PHP Tutorial > Why is Nginx Downloading My .php Files Instead of Executing Them?

Why is Nginx Downloading My .php Files Instead of Executing Them?

Mary-Kate Olsen
Release: 2024-12-02 08:59:09
Original
313 people have browsed it

Why is Nginx Downloading My .php Files Instead of Executing Them?

Nginx Servers .php Files as Downloads, Rather Than Executing Them

When configuring Nginx with PHP, it's essential to ensure that .php files are executed as scripts instead of being treated as downloads. Here's a detailed explanation of the issue and steps to resolve it:

The issue arises when Nginx does not properly recognize .php files as PHP scripts. This occurs due to an incorrect configuration in the Nginx site configuration file. To address this, follow the steps below:

  1. Edit the Default Site Configuration File:
    Navigate to /etc/nginx/sites-available/default.
  2. Uncomment Listen Lines:
    Uncomment both listen lines to enable Nginx to listen on both IPv4 and IPv6 on port 80.
  3. Leave Server Name:
    Do not modify the server name.
  4. Add index.php to Index Line:
    In the root directive, add index.php to the list of index files.
  5. Uncomment PHP Location Block:
    Uncomment the location block that begins with ~ .php$.
  6. Set cgi.fix_pathinfo to 0:
    Edit /etc/php5/fpm/php.ini and set cgi.fix_pathinfo to 0.
  7. Restart Services:
    Restart Nginx and php5-fpm using sudo service nginx restart && sudo service php5-fpm restart.

These modifications ensure that Nginx correctly identifies .php files as PHP scripts and executes them accordingly, rather than downloading them as files.

The above is the detailed content of Why is Nginx Downloading My .php Files 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template