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?

  • 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.
    – Keith
    May 9, 2011 at 6:05
  • 2
    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.
    – Shadur
    May 9, 2011 at 7:13

2 Answers 2


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


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.


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

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.

  • 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.
    – penguin359
    May 9, 2011 at 17:46
  • Fair enough, I suppose.
    – Shadur
    May 9, 2011 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .