Linux – php file not running in localhost

apache-http-serverlinuxPHP

I installed the apache2 and then I tried to run a php file with no success. I placed the testphp.php file in /var/www/html. When I try to access http://localhost/testphp.php it shows only the content of the file. I'm using Linux Mint 17.2. I tried to restart apache2 and the result was the same. Apache is installed and running without problems.
Thank you for your time.

Best Answer

If apache is rendering as text your php file, then apache is not configured to use php.

You need to find your httpd.conf file. Unfortuneately, different installs put this in different locations. Start looking in /etc/httpd directory structure. Other places might be /usr/local/etc/httpd. For the below, I'm going to assume /etc/httpd.

When you find httpd.conf, look for ServerRoot. This may be /etc/httpd. There should be a "modules" directory or symbolic link to a directory.

In this directory should be the php module if it was installed, say libphp5.so. First confirm you have a php module in the "modules" directory.

Next, we need to tell apache to use php.

Apache loads a default config file, httpd.conf, and additional config files typically in "extra" or "conf.d" directories. See if you have any of those, and within those should be a php.conf file.

If there is no php.conf file, that could be your issue. Here's a simple one:

LoadModule php5_module modules/libphp5.so
AddHandler php5-script .php
AddType text/html .php
DirectoryIndex index.php

What this does is tell apache to load the libphp5.so module, associate the .php extension to it, expect the output of the php script to be html, and if an index.php file exists use it when a user goes to a directory instead of specific url.

Related Question