Report this

What is the reason for this report?

PHP files downloading instead of executing

Posted on September 26, 2020

Connected Tutorial(This question is a follow-up to this tutorial):

How To Configure Nginx as a Web Server and Reverse Proxy for Apache on One Ubuntu 20.04 Server

Hello, I installed NGINX as a reverse proxy for my Apache and when I’m trying to access the web, It starts downloading the index.php file (with no name). I have completely same files as in this tutorial. (Only domain changed). I need help with this.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Hi there @ondrejnovy,

This usually happens due to misconfigured PHP-FPM settings.

Does this also happen if you try to access the Apache service directly on port 8080? For example if you create an info.php file and then access http://your_server_ip:8080/info.php do you get the content PHP info page?

Can you share your Apache Vhost config and the /etc/apache2/mods-enabled/fastcgi.conf file here?

Another thing that I could suggest is also checking your Apache error log:

  1. tail -100 /var/log/apache2/error.log

Also, do you have a .htaccess file inside your document root? If so I could suggest temporary disabling it to test if it is causing the issue for you.

Regards, Bobby

When PHP files are downloaded instead of being executed by the server, it usually indicates an issue with the configuration of the server that’s supposed to process PHP files—in your case, Apache, which is behind NGINX as a reverse proxy. Here’s a step-by-step guide to troubleshoot and fix the issue:

Step 1: Check NGINX Configuration

First, ensure that NGINX is correctly configured to pass PHP requests to Apache. Here’s a typical configuration example where NGINX acts as a reverse proxy:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8080;  # Ensure Apache listens on this port
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~ \.php$ {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

This configuration assumes that Apache is listening on port 8080. Make sure your Apache server is configured to listen on the port specified in the proxy_pass directive.

Step 2: Check Apache Configuration

Next, verify that Apache is set up to handle PHP files correctly. Ensure that the mod_php module is enabled and correctly configured:

  1. Check if mod_php is enabled:
sudo a2enmod php8.x  # Replace "8.x" with your PHP version
  1. Apache Virtual Host Configuration: Make sure your Apache Virtual Host configuration is set to handle PHP files. An example configuration might look like this:
<VirtualHost *:8080>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options +FollowSymLinks
        AllowOverride All
        Require all granted
        AddType application/x-httpd-php .php
    </Directory>

    DirectoryIndex index.php index.html
</VirtualHost>

The AddType directive should not be necessary when using mod_php, but it’s good to check everything related to PHP handling. 3. ** Don’t Forget to restart both Nginx and Apache**

Step 3: Ensure PHP is Configured Correctly

Verify that PHP is installed and configured correctly. You can check your PHP installation by creating a info.php file in your document root with the following content:

<?php phpinfo(); ?>

Try accessing http://yourdomain.com/info.php from your browser. If PHP is configured correctly, this should display the PHP information page.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.