Search code examples
apacheubuntu

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

  • 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