php file not running in localhost

9,383

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.

Share:
9,383

Related videos on Youtube

rockability
Author by

rockability

Updated on September 18, 2022

Comments

  • rockability
    rockability over 1 year

    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.

    • Admin
      Admin over 7 years
      I can't publish more than 2 links now, so I will put the other link here. I was following this tutorial for the purpose of installing apache and php link
    • Admin
      Admin over 7 years
      Have you actually installed PHP sudo apt-get install php5?
    • Admin
      Admin over 7 years
      Yes @MrStatic , I run the following command sudo apt-get install php5 libapache2-mod-php5
    • Admin
      Admin over 7 years
      And you have restarted apache?
    • Admin
      Admin over 7 years
      Yes, and no success
    • Admin
      Admin over 7 years
      provide full output of service apache status and service httpd status via pastebin service of choice dropping link (s) here in a comment or as an edit.