15

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.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
Mark
  • 409
  • 2
  • 6
  • 17
  • do you have php installed on that server? And if you create a page `phpinfo.php` with `phpinfo()` in it, do you get any results? – Wouter J Jul 21 '12 at 21:10
  • http://ec2-184-72-69-40.compute-1.amazonaws.com/phpinfo.php <-- requested page. No, I do not. – Mark Jul 21 '12 at 21:25

7 Answers7

12

You can install libapache2-mod-php5 using

apt-get install libapache2-mod-php5

Worked for me.

Nvan
  • 1,126
  • 16
  • 23
11

Are you sure you have php installed? If it is you need to make sure that apache is associating .php files with the php handler. Look for an entry similar to the following in /etc/apache/apache.conf

 LoadModule php5_module modules/libphp5.so

and

 application/x-httpd-php        php php5

upon changing the file you will need to restart apache via sudo service httpd restart

secretformula
  • 6,414
  • 3
  • 33
  • 56
  • I don't have a directory /etc/apache/apache.conf, is it possible that this file is under a different name? I ran the following command : grep root apache.conf and the result was no such file or directory. – Mark Jul 21 '12 at 21:20
  • grep is only going to search the root directory. The directory is going to depend on your OS but for ubuntu server its `/etc/apache2/conf.d/apache.conf` sorry – secretformula Jul 21 '12 at 21:30
  • still no dice. Could it be this file:/etc/httpd/conf/httpd.conf? – Mark Jul 21 '12 at 21:47
  • Yes thats it, it really depends on the distribution – secretformula Jul 21 '12 at 23:00
  • For those who are looking at this question down the road: amazon ec2 does not have php installed on it. You have to make sure it's installed. Try "which php" to see if you do. – Mark Jul 24 '12 at 06:03
5

If you are using php7 make sure you installed this module.

sudo apt-get install libapache2-mod-php7.0

Replace 7.0 with the version of php you are using.

To find version of php, use

php -v 
Agnel Vishal
  • 564
  • 6
  • 13
4

You probably need addHandler or addType in either the .htaccess file or Apache config itself: e.g. AddType application/x-httpd-php .php

Arcatica
  • 69
  • 2
1

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/
Edoardo Vignati
  • 433
  • 5
  • 14
0

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.

Vlastimil Burián
  • 3,024
  • 2
  • 31
  • 52
0

Adding the line:

SetHandler application/x-httpd-php 

in the configuration file worked for me

Rain
  • 3,416
  • 3
  • 24
  • 40
marwaneL
  • 31
  • 2