# Nginx Serves .Php Files as Downloads, Instead of Executing Them

When Nginx serves `.php` files as downloads instead of executing them, it usually indicates that PHP processing is not correctly configured. Unlike Apache, which can handle PHP directly through its modules, Nginx relies on external processors like PHP-FPM (FastCGI Process Manager) to handle PHP files. Here's a step-by-step guide to resolve this issue:

### **1. Install PHP and PHP-FPM**

Make sure that PHP and PHP-FPM are installed on your server. You can install them using the package manager for your distribution.

**For Ubuntu/Debian:**

```bash
sudo apt update
sudo apt install php-fpm
```

**For CentOS/RHEL:**

```bash
sudo yum install php-fpm
```

### **2. Configure PHP-FPM**

Ensure that PHP-FPM is correctly configured and running. The default configuration usually works, but you can verify and adjust settings in the PHP-FPM configuration file.

**Check PHP-FPM status:**

```bash
sudo systemctl status php-fpm
```

**Configuration file location (common paths):**

- **Ubuntu/Debian:** `/etc/php/7.x/fpm/php-fpm.conf`
- **CentOS/RHEL:** `/etc/php-fpm.conf`

### **3. Configure Nginx to Pass Requests to PHP-FPM**

You need to set up Nginx to pass `.php` requests to PHP-FPM. Here’s a basic configuration example for Nginx to handle PHP files:

**Example Nginx Server Block:**

```
server {
    listen 80;
    server_name example.com;
    root /var/www/html;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.x-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\\.ht {
        deny all;
    }
}

```

**Explanation:**

- **`try_files $uri $uri/ =404;`**: Tries to serve the requested file or directory. If not found, returns a 404 error.
- **`location ~ \\.php$`**: Matches requests for `.php` files.
- **`include snippets/fastcgi-php.conf;`**: Includes a configuration snippet for FastCGI parameters.
- **`fastcgi_pass unix:/var/run/php/php7.x-fpm.sock;`**: Passes requests to PHP-FPM via a Unix socket. Adjust the path if necessary.
- **`fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;`**: Sets the SCRIPT_FILENAME parameter, required by PHP-FPM to execute scripts.

### **4. Create the `fastcgi-php.conf` Snippet**

The `fastcgi-php.conf` snippet is typically included by default but can be customized. Ensure it exists at the specified path or create it if necessary.

**Example `fastcgi-php.conf`:**

```
# /etc/nginx/snippets/fastcgi-php.conf or a similar path

fastcgi_split_path_info ^(.+\\.php)(/.+)$;
fastcgi_index index.php;
include fastcgi_params;

```

### **5. Verify PHP-FPM Socket Path**

Ensure that the `fastcgi_pass` directive points to the correct PHP-FPM socket or TCP address. The socket path can be found in the PHP-FPM configuration.

**Check PHP-FPM configuration file (common paths):**

- **Ubuntu/Debian:** `/etc/php/7.x/fpm/pool.d/www.conf`
- **CentOS/RHEL:** `/etc/php-fpm.d/www.conf`

Look for the `listen` directive:

```
listen = /var/run/php/php7.x-fpm.soc
```

### **6. Restart Nginx and PHP-FPM**

After making configuration changes, restart both Nginx and PHP-FPM to apply them.

**Restart Nginx:**

```bash
sudo systemctl restart nginx
```

**Restart PHP-FPM:**

```bash
sudo systemctl restart php-fpm
```

### **7. Check File Permissions**

Ensure that the web server user (e.g., `www-data`, `nginx`, or `apache`) has read and execute permissions for PHP files and directories.

**Set Permissions:**

```bash
sudo chown -R www-data:www-data /var/www/html
sudo find /var/www/html -type d -exec chmod 755 {} \\;
sudo find /var/www/html -type f -exec chmod 644 {} \\;

```

### **8. Test PHP Execution**

Create a test PHP file to verify that PHP is executed correctly.

**Example Test PHP File:**

```php
<?php
phpinfo();
?>
```

**Save this as `info.php` in your web root (`/var/www/html`) and access it via your browser:** `http://yourdomain.com/info.php`

You should see the PHP information page if PHP is configured correctly.

### **Summary**

To resolve the issue of Nginx serving `.php` files as downloads:

1. **Ensure PHP and PHP-FPM are Installed and Running.**
2. **Configure Nginx to Pass PHP Requests to PHP-FPM.**
3. **Verify the `fastcgi_pass` Path and PHP-FPM Socket Configuration.**
4. **Create or Verify `fastcgi-php.conf` Configuration Snippet.**
5. **Restart Nginx and PHP-FPM Services.**
6. **Check File Permissions.**
7. **Test PHP Execution with a Sample PHP File.**

By following these steps, you should be able to configure Nginx to handle PHP files properly and execute them instead of serving them as downloads.