Why is PHP script downloaded instead of executed

I have a local LAMP server set up on my Ubuntu laptop for testing various PHP scripts.

Sometimes, when I do something wrong, instead of getting an error, the script I'm testing is offered for download. Why is that and how can it be fixed?


Solution 1:

The server does not recognize the script as something to execute locally, and as such it offers it as a file instead.

These lines should be present in your Apache config. Note that the path may need to be changed for the .so modules, depending on your configuration:

# -- if you're using PHP 5, uncomment this line to activate it
#LoadModule php5_module libexec/libphp5.so

# -- if you're using PHP 4, uncomment this line to activate it
#LoadModule php4_module libexec/libphp4.so

AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

Follow up with a restart of the Apache service and php should load into Apache to run just fine.

Solution 2:

Some reasons I have found that this happens:

  • if php is not configured correctly on the server
  • if the file doesn't have the correct .php extension
  • if it is in a directory that does not allow execution
  • if the script takes too long to execute.

One test I use is to make a file in the same directory (verify.php) that contains:

<?php
  phpinfo();
?>

If that file executes in my browser, it the first 3 points are ok