PHP on Mac OS X: shows PHP code when opening a page

The PHP source code is displayed when I open the PHP page in my localhost. It seems strange to me. I am using PHP 5.3.1 on Mac OS X Snow Leopard.

$ php --version PHP 5.3.1 (cli) (built: Feb 28 2010 15:02:51) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies 

I am running the default httpd web server on my machine. The file / etc / apache 2 / httpd.conf has the following line for loading the php module:

 LoadModule php5_module libexec/apache2/libphp5.so 

The syntax of the httpd.conf file is fine.

 $ httpd -t Syntax OK 

Am I missing something? Please suggest!

Thanks!

+6
php apache macos
source share
3 answers

You need to add AddType to your server configuration:

 AddType x-httpd-php .php 

This can be added to the httpd.conf file or even to the .htaccess file.

+7
source share

For OS X Yosemite 10.10, you must add:

 AddType application/x-httpd-php .php 
+6
source share

One of the possible reasons why this happens to you is that you use short PHP tags <? instead of <?php

Either change that in all php files included or just enable short PHP tags by adding short_open_tag=On in php.ini and then restart the Apache server.

+4
source share

All Articles