I have just reinstalled Apache and PHP5 but when I visit my doc root it just downloads a "download" file. And when I try documentroot/index.php it just downloads the PHP file.

I have PHP installed but Apache doesn't seem to want to execute it.
People said to add a 'Load Module' line to my httpd.conf but I dont have that file.

up vote 90 down vote accepted

Typing:

sudo apt-get install apache2 php5 libapache2-mod-php5

will install everything you need and will start the apache server with support for PHP.

To verify that the php module is loaded, type:

a2query -m php5

if not enabled, then load with:

sudo a2enmod php5

and restart apache:

sudo service apache2 restart

Update for php7.0, tested on Ubuntu 16.04 and 16.10

Thanks to the comments, I update the answer for php7.

Install:

sudo apt-get install apache2 php7.0 libapache2-mod-php7.0 

Verify:

a2query -m php7.0

Load:

sudo a2enmod php7.0

Restart apache:

sudo service apache2 restart
  • It would be better to use a2enmod than to copy the file manually between mods-available and mods-enabled. a2enmod creates a symlink and so you'll only have one copy of the configuration. – jkt123 Apr 21 '14 at 2:17
  • @jkt123 you're right. thanks for the advice, i edit my answer. – girardengo Apr 21 '14 at 8:20
  • In my case, it says "ERROR:module php5 does not exist!" it is installed – Marian Klühspies Mar 8 '15 at 2:07
  • 3
    for ubuntu 16.04 with php7 I have executed these commands in order apt-get install libapache2-mod-php, a2query -m php7.0, service apache2 restart – marlo Jun 9 '16 at 10:24
  • For ubuntu 14.04 : # a2query -m php7.0 php7.0 (enabled by site administrator) Still php code is not executing – Ashish Karpe Oct 25 '16 at 12:57

Another common cause is that php short tags <? are not enabled by the default php.ini configuration, so after an upgrade or reinstall, that setting may be turned off and php code may depend on it. Try replacing <? with <?php.

(Credit)

Ubuntu 16.04 has moved to PHP 7. This single command will do the job for you:

sudo apt-get install apache2 php libapache2-mod-php

Here libapache2-mod-php is a meta-package that installs libapache2-mod-php7.

Open php.ini with nano in terminal:

sudo nano /etc/php/php5.6/apache2/php.ini

Then change:

short_open_tag = Off

to

short_open_tag = On

Then save and then restart apache2:

sudo systemctl restart apache2
  • Or fix the PHP scripts :-) This directive determines whether or not PHP will recognize code between <? and ?> tags as PHP source which should be processed as such. It is generally recommended that <?php and ?> should be used and that this feature should be disabled [...] – Nicolas Raoul May 17 at 3:39

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.