Apache 2 and PHP 5 not working

11,486

Solution 1

I think you need to wrap your code in <?php ... ?>. Try this file:

<?php
phpinfo();
?>

By default, the contents of a PHP file are HTML that is sent untouched out to the browser. The markers <?php and ?> mark the start and end of PHP code that is interpreted by PHP.

Solution 2

As penguin359 pointed out, php won't interpret anything in a html page that isn't bracketed with <? ... ?> at the very least.

If it still doesn't work after you've tried that, the first thing you should probably do is check your server's access and error logs (by default in /var/log/apache2/access.log and /var/log/apache2/error.log unless your distribution does something wonky; check the config files in /etc/apache2/ for theAccessLogandErrorLog` directives), then continue troubleshooting depending on what exactly they say.

If the page was accessed and no error reported, it means that the server for some reason didn't find it necessary to fire up the php parser, most likely because of a config error.

There are several different ways for the php parser to be called by apache; the two most common ones are via the dynamically loaded php module, or via CGI.

If it's (supposed to be) loaded via mod_php then your config file should contain a directive looking something like

LoadModule php5_module /usr/lib/apache2/modules/libphp5.so
in it, as well as directives instructing it to let the php5 module handle .php pages, likely something like this:

<IfModule mod_php5.c>
  AddType application/x-httpd-php .php .phtml .php3 .php5
  AddType application/x-httpd-php-source .phps
  php_admin_flag register_globals off
</IfModule>

with slight variances possible depending on how your distribution handles apache2 configuration.

If it's called via cgi instead -- generally a better option because it allows for a threaded server as well as nifty stuff like SuEXEC -- then you should again check your apache config files and look for lines saying something like
AddHandler fcgid-script .php .php5

which tell the server how to handle files ending with .php or .php5.

Again, though, exactly what is wrong with your server -- assuming that it's more than just the original failure to bracket your .php file with <? ... ?> -- is hard to guess without a look at the access and error logs.

Share:
11,486

Related videos on Youtube

Rithiur
Author by

Rithiur

Updated on September 18, 2022

Comments

  • Rithiur
    Rithiur over 1 year

    I have a test.php file in the web server directory with php phpinfo(); content. When I try to execute php test.php at a shell then it executes correctly. But when I try to open the web page in a browser, it shows php phpinfo();. How can I troubleshoot Apache 2 and PHP 5 on my UNIX machine?

    • Keith
      Keith about 13 years
      You need to configure apache so that it executes the php file with the interperter, rather than serve it. I'm pretty sure there are instructions on the PHP web site about how to do that.
    • Shadur
      Shadur about 13 years
      Also, you might want to start accepting answers to your questions once you've received one that worked. It's regarded as the polite thing to do.
  • penguin359
    penguin359 about 13 years
    I would recommend against using short tags (i.e. using <? instead of <?php for opening PHP code) as it can cause problems especially with XML/XHTML and for that reason it's not always enabled in PHP offering even more reason to avoid them.
  • Shadur
    Shadur about 13 years
    Fair enough, I suppose.