2

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.

6
  • Do you think apache is not properly configure? Commented May 24, 2020 at 20:39
  • Your problem is obviously not code related. It is a server issue. Try a search for "php ubuntu raspberry pi". Fix the server first. Commented May 24, 2020 at 20:55
  • @gbengawale Yes, that is definitely the issue. The problem is that I don’t know how to do that Commented May 24, 2020 at 21:15
  • @theratkeeper To check that Apache is up and running on your Raspberry Pi, you can enter the Raspberry Pi’s IP address into a web browser. The server should return a webpage with some simple text on it. Commented May 24, 2020 at 22:22
  • If you don’t know the IP, you can enter the following command into the terminal to retrieve it with this command hostname -I Commented May 24, 2020 at 22:26

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

Oh, I think I may need to update things. I am using PHP 7.2 for some reason (I installed it Saturday)
I also don’t know if I have it enabled, but I have restarted Apache (a lot of times, actually, which is how I know I kept breaking it.
@theratkeeper please check
So it turned out that while I had enabled PHP previously, I forgot to re-enable it after and uninstalled and reinstalled Apache after I broke it trying to change the page from index.html to index.php. I enabled it and it worked, but I had to disable mpm_event first.
1

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

Comments

1

A more direct and seamless approach will be to use vscode. vscode provides two extensions that can get you up and running:

  1. PHP Server PHP Server
  2. PHP Intelephense PHP Intelephense

However, you must configure your PHP settings on vscode setting by adjusting this PATH PATH.

For more details: https://code.visualstudio.com/docs/languages/php

2 Comments

The question is not mentioning vs code. Make sure your answer is relevant to the OP's use case.
Thanks @niid . I will take a more extended look. I ran into a similar problem. I may have confused my problem with this one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.