php showing source code in localhost

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
    

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


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