70

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 suggested adding a 'Load Module' line to my httpd.conf but I don't have that file.

7 Answers 7

139

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.x (tested on Ubuntu 16.04, 16.10, 18.04, 18.10)

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

Install:

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

Verify:

a2query -m php7.x

Load:

sudo a2enmod php7.x

Restart apache:

sudo service apache2 restart
10
  • 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, 2014 at 2:17
  • @jkt123 you're right. thanks for the advice, i edit my answer.
    – girardengo
    Apr 21, 2014 at 8:20
  • In my case, it says "ERROR:module php5 does not exist!" it is installed Mar 8, 2015 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, 2016 at 10:24
  • For ubuntu 14.04 : # a2query -m php7.0 php7.0 (enabled by site administrator) Still php code is not executing Oct 25, 2016 at 12:57
21

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)

18

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.

7

I ran into the same problem, but my particular problem was unique. I was running in a user directory (using mod_userdir) - meaning you access my webpage with http://host.name.com/~username/example.php

This is disabled by default in php*.conf in my mods-enabled directory. I had to modify it to comment it out:

#<IfModule mod_userdir.c>
#    <Directory /home/*/public_html>
#        php_admin_flag engine Off
#    </Directory>
#</IfModule>

That was a pain to figure out..

Well, I doubt anybody had THIS particular problem, but I figured I'd add it because it was perplexing as crud to me and MAYBE somebody is doing the same thing. Nobody uses ~/user anymore I guess, except me.

You have to restart apache after this of course.

2
  • Not so unique after all :p
    – Cyrille
    Apr 29, 2022 at 15:46
  • Awesome find, thank you!
    – deep64blue
    Aug 28, 2023 at 16:05
6

For people who have found this post from Google almost 6 years in the future (and beyond!), you may run into this problem with Apache 2 and PHP 7 while also using the UserDir module.

Another possible cause of this problem could be that you are trying to run the script in a "user directory" from the the UserDir module. Running PHP scripts in user directories is disabled by default. You will run into this problem if the script is in the public_html directory in your home folder and you are trying to access it from http://localhost/~your_username.

To fix this, open up /etc/apache2/mods-enabled/php7.2.conf. You must comment or delete the tag block at the bottom that reads

<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>
1

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
1
  • 2
    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 [...] May 17, 2018 at 3:39
1

My case was a little different. I upgraded from Ubuntu 19.04 to 19.10. While upgration it installed php 7.3 (where as php 7.2 was already installed). With 2 versions installed a2query -m php7.3 return No module matches php7.3.

Solution I found was from how-to-install-php-7-3-on-ubuntu.

To set PHP 7.0 as the default, run

update-alternatives --set php /usr/bin/php7.0

To set PHP 7.2 as the default, run

update-alternatives --set php /usr/bin/php7.2

To set PHP 7.3 as the default, run

update-alternatives --set php /usr/bin/php7.3

Before we can configure Apache to use PHP 7.3, we need to disable the old version of PHP 7.0 by typing

a2dismod php7.2

Now enable the newly installed PHP 7.3 version with the following command:

a2enmod php7.3

Restart the Apache web server for the changes to take effect:

sudo systemctl restart apache2
1
  • Thx my friend, it works perfectly for me. I upgrade my system from 18.04 to 19.10 and php stoped to work. Your tips worked to new version 7.3.
    – Zanoldor
    Mar 6, 2020 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .