6

i have apache2 (Server version: Apache/2.4.7 (Ubuntu) Server built: Mar 10 2015 13:05:59), mysql and phpmyadmin.

While accessing php file by http://localhost/ or http://ipaddress

It shows up source code instead of executing.

Ubuntu 14.04 LTS

enter image description here

  • 2
    Possible duplicate of PHP script not executing on Apache server – muru Mar 24 '17 at 9:55
  • @muru that question is different from this. This displays the source code while that question specifies that file is downloaded instead of executing. – Anuj TBE Mar 25 '17 at 6:30
  • 1
    that just depends on the media type set for the file, the core problem is the same. – muru Mar 25 '17 at 6:31
12

I can think of 3 possibilities:

  1. You don't have PHP installed. In that case run:

    sudo apt-get install php php-mysql
    
  2. You have PHP installed but not enabled. You can try running:

    sudo a2enmod php7  
    sudo service apache2 restart  
    
  3. You have PHP installed and enabled but you haven't restarted apache.

    sudo service apache2 restart
    
  • none of them worked for me. Here are the results: 1 . E: Unable to locate pachage php I tried with php5 which gives php5 is already the newest version. 2 . ERROR: Module php5 does not exist!. I tried with php which gives same error. 3 The apache2 configtest failed. – Anuj TBE Jun 8 '15 at 9:20
  • It seems you don't have modphp installed... Try the following sudo apt-get install libapache2-mod-php5 and then restart apache... if it still doesn't work try running sudo a2enmod php5 again. – Sledge Hammer Jun 8 '15 at 10:25
  • I didn't helped me. Now I am left with one solution and that is to reinstall Ubuntu. But, if php starts working prior to this, it will be life saver to me. – Anuj TBE Jun 9 '15 at 18:58
  • In tat case you might want to completely reinstall apache, php & mysql. In terminal do the following: sudo apt-get purge apache2 php5 mysql-server This will completely remove the packages including their configuration files so if you have made any important changes better make a backup of /etc/apache2 , /etc/mysql and /etc/php5 first. After that do the following: sudo apt-get install apache2 php5 mysql-server . Note that it is important to install them all at once because otherwise modphp doesn't always get installed. – Sledge Hammer Jun 9 '15 at 19:10
  • 1
    You are missing the package which installs the PHP5 module used by Apache. The package is called libapache2-mod-php5. – Dan Apr 6 '16 at 16:57
5

you need to install libapache2-mod-php sudo apt-get install libapache2-mod-php it is no longer pulled by other packages as a dependency in 14.04 it named libapache2-mod-php5, in 16.04 they dropped the number at the end for a meta package

2

After Upgrading to Ubuntu 17.10 this morning my answer was a combination of Sledge Hammer's and GM-Script-Writer-62850's answer. The exact commands I needed were:

sudo apt-get install libapache2-mod-php
sudo a2enmod php7.1
systemctl restart apache2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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