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.
short_open_tag = On
LoadModule php5_module "C:/php/php5apache2_2.dll" AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps PHPIniDir "C:/php"
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
Also check in
/var/log/apache2/error.log to see if you have any other errors.
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
Read more... Read less...
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/
open the file
#LoadModule php5_module libexec/apache2/libphp5.so
LoadModule php5_module libexec/apache2/libphp5.so
So just uncoment the PHP module load in
A different answer that worked for me. Is that, to install
[sudo] apt-get install libapache2-mod-php5.X
Alright if you've tried what you've been told above or earlier(which are possible reasons) and it still displays the code instead of executing it then there is one thing which you are doing wrong that hasn't been addressed. The url you used to access your php code; some people try to execute their php code by just dragging the .php file into the web browser. this is wrong practice and could lead to this kind of problem. if you have saved a file as "test.php" in the C://wamp/www folder then you must access this file this way: localhost://test.php. this kind of mistake will arise when you access it this way: localhost://wamp/www/test.php
Hope I helped someone out there. o/ ~Daniel