'PHP not running on apache server

I am new to PHP, and relatively new to Apache. For my server, I am using a raspberry pi running Ubuntu MATE, which (as far as I know) is just Ubuntu with a different desktop environment. I have PHP installed, and the file I'm serving is a .php file, NOT html. I am trying to execute PHP code on my test page, which is just served locally. I know that I need to edit the config file for Apache in order for it to run, but I don't know how; all of the various answers and articles I have looked at the past few days are either for Windows or for a much older version of Apache. I have tried many of them, and they all either result in nothing changing or an error when trying to restart Apache. The PHP code I'm using looks like this:

<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello!";
?>
</body>
</html>

Looking at the inspector in Firefox, the PHP code is commented out, a clear indication that it is not being executed. Thank you for your help.



Solution 1:[1]

It is possible that,

  • you have either not installed libapache2-mod-php7.x module
  • OR not enabled module.
  • OR you installed module but not reloaded apache services

Below will help you

# verify whether module activated if not follow below
# if there were no module you will get No module matches php7.x
a2query -m php7.x

# install version of your php example 7.4
sudo apt-get install libapache2-mod-php7.x  

# activate module
sudo a2enmod php7.x

# restart or reload apache 
sudo service apache2 restart

Example in my case

root@ideapad:~#  a2query -m php7.4
php7.4 (enabled by site administrator)

# if modules were loaded properly then you will see like below
root@ideapad:~#  ls /etc/apache2/mods-enabled/php* -1
/etc/apache2/mods-enabled/php7.4.conf
/etc/apache2/mods-enabled/php7.4.load

Solution 2:[2]

I had this same problem,Eventhough my apache was running it just rendered raw codes on the browser; after debugging it manually and searching through the internet I finally got it solved. About two simple things fixed mine "though they weren't simple before I did them:)".

Btw I am running running PHP v7.4.3 on Ubuntu 20.04.

  1. sudo apt update
  2. sudo apt install libapache2-mod-php

If you haven't installed PHP or Apache on your machine; you can check out this site for how to go about it for PHP version 7.4.3 Linuxhint

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dlaw