5

I'm using an iMac which I've just updated to macOS Sierra. Prior to this it was running El Capitan.

Under my El Capitan set up I had Apache, PHP and MySQL configured as per this guide. This worked without any problems.

When I updated to Sierra it overwrote a load of config files. So I went back through the guide and configured everything as I had under El Capitan.

http://localhost is working as I'd expect and I can see files in my webroot. However, if I open a PHP page, it just shows the raw PHP code rather than executing it.

I'm using the following:

Apache

  • Server version: Apache/2.4.23 (Unix)
  • Server built: Aug 8 2016 16:31:34

PHP

  • PHP 7.0.10 (cli) (built: Aug 31 2016 10:25:51) ( NTS )

macOS

  • Sierra 10.12
Giacomo1968
  • 55,001
Andy
  • 163
  • Try looking for error messages in an apache log file, probably in /private/var/log/apache2/ – meuh Sep 29 '16 at 18:10

5 Answers5

23

Sierra comes with PHP 5, not PHP 7. Anyway, check if the module is loaded in httpd.conf:

LoadModule php5_module libexec/apache2/libphp5.so

If this doesn't work, probably you didn't told Apache to recognize *.php files as php executables. To do so, locate and edit httpd.conf and add the following lines (if not already present):

<FilesMatch .php$>
    SetHandler application/x-httpd-php
</FilesMatch>

and edit the DirectoryIndex parameter:

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

Another common solution is to activate the short open tags (if you have any in your PHP code). Locate php.ini, search for the string short_open_tag = Off and change it to short_open_tag = On.

Restart Apache.

pietrop
  • 356
  • In my case,

    it was showing plain php code through virtual host. after uncommented the specific module "LoadModule php7_module libexec/apache2/libphp7.so"

    in /etc/apache2/httpd.conf path the problem is gone.

    – sh6210 Oct 03 '18 at 04:22
3

Your apache isn't processing php files any more. There is line

LoadModule **php5_module** libexec/apache2/**libphp5.so**

in your config file /etc/apache2/httpd.conf

But you have installed PHP7. Change it to:

LoadModule **php7_module** libexec/apache2/**libphp7.so**

or maybe modules/mod_php.so or whenever php module is located.

  • 1
    That doesn't seem to work. Syntax error on line 170 of /private/etc/apache2/httpd.conf: Cannot load libexec/apache2/libphp7.so into server: dlopen(/usr/libexec/apache2/libphp7.so, 10): image not found – Andy Oct 03 '16 at 08:09
  • Hmm this is working for me, surprised, how PHP version updated to 5-7 in my mac, thanks – Sandeep P Dec 13 '18 at 08:30
1

Ensure the #LoadModule php5_module libexec/apache2/libphp5.so line is uncommented in the main httpd.conf file. Don't forget to restart Apache sudo apachectl restart.

  • I tried this already. That line is definitely uncommented and I restarted Apache. Also did a 'configtest' and it's saying the Syntax is OK. – Andy Oct 03 '16 at 08:07
  • 1
    Gets even stranger but if I add "AddType application/x-httpd-php .php" to /etc/apache2/httpd.conf and restart Apache it processes PHP. But it's using PHP 5.x instead of 7.x (??!) – Andy Oct 03 '16 at 08:24
  • The config line in Andy's comment fixed the problem for me. – tinkerr Nov 02 '16 at 14:48
-1

I had this problem as well. Check your /private/ect/apache2/extra/htttpd-vhosts.conf to see if still has your virtual hosts settings. My original one was renamed to httpd-vhosts.conf~previous.

curt
  • 119
  • 4
-1

You cannot open the .php file using open file from the browsers File menu, You have to type localhost/info.php(or whatever file and path you're trying to open) in your browsers address bar... This works for me at least. Hope you work it out.

  • In my case,

    it was showing plain php code through virtual host. after uncommented the specific module "LoadModule php7_module libexec/apache2/libphp7.so"

    in /etc/apache2/httpd.conf path the problem is gone.

    – sh6210 Oct 03 '18 at 04:20