PHP not interpreted, showing in view source

Viewed 43664

I'm hoping that this will be a useful page for getting started running php code as well as solve the current problem I'm having some very simple code as follows:

<html>
<head>
<title> Practice</title></head>
<body>
This is HTML
<?php
echo "This is PHP";
?>
</body>
<html>

This is uploaded on an ec2 website which has apache running. The code isn't interpreted, and when you view source of the page it shows the php code.

You can see the page.

Any ideas? The php code is so basic that I think it might have to do with the apache configuration. Please let me know any additional information you need and I'll provide it, hopefully tell me how to get it to.

7 Answers

In my case I had the /var/www/ folder with wrong permissions.

I had to run:

sudo chown -R www-data /var/www/
sudo chgrp -R www-data /var/www/

If you have the contents of the web page in user directory like:

/home/*/public_html

Then you need to enable executing those, it's disabled by default:

# Running PHP scripts in user directories is disabled by default
# 
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>

Just comment out this piece of code located in the file:

/etc/apache2/mods-enabled/php7.3.conf

Adjust the path and file name to your system, PHP version, etc.

Adding the line:

SetHandler application/x-httpd-php 

in the configuration file worked for me

Related