Apache shows PHP code instead of executing it

I have recently been trying to install PHP and Apache on my computer. After many hours, they're installed. I have modified the httpd.conf and php.ini files like everyone says. I then created a simple PHP script:

<?php phpinfo(); ?>

But when I try to run it with http://127.0.0.1/phpinfo.php it just shows the source code instead of executing it. I am using Apache 2, PHP 5 and Windows Vista.

EDITED LINES:

PHP.INI:

short_open_tag = On

HTTPD.CONF

LoadModule php5_module "C:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
PHPIniDir "C:/php"

Solution 1:

You must enable php! Check the folder mods-enabled in the Apache directory (default: /etc/apache2/) to see if you find a file named php. I don't remember the extension but I think it's .so.

Also check in /var/log/apache2/error.log to see if you have any other errors.

Solution 2:

Wow, lots of solutions here! Here's what I did on Ubuntu 16.04:

sudo apt-get install php libapache2-mod-php
sudo a2enmod mpm_prefork && sudo a2enmod php7.0
sudo service apache2 restart

Solution 3:

For PHP 7 (May apply to previous versions as well), but I had to do this:

Add this to the bottom of /etc/apache2/apache2.conf

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

Run this from the terminal:

sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7

Then don't forget to restart Apache so it knows you changed stuff:

sudo service apache2 restart

This is a summary from: https://www.atlantic.net/community/howto/try-php7-lamp-ubuntu-14-04/

Solution 4:

open the file

/etc/apache2/httpd.conf

and change

#LoadModule php5_module libexec/apache2/libphp5.so

into

LoadModule php5_module libexec/apache2/libphp5.so

So just uncoment the PHP module load in httpd.conf