Search code examples
macosapachemampphp

phpinfo() not working on mac OS-X Capitan apache2 server


I am using OS-X Capitan and Apache/2.4.18 (Unix). I have below phpinfo.php file

 <?php phpinfo(); ?>

when I run the command php phpinfo.php it will show phpinfo() correctly. This is not showing correctly from browser but instead shows the code as below

 <?php phpinfo(); ?>

I have made following changes to /etc/apache2/httpd.conf

  1. uncommented below lines

    LoadModule rewrite_module libexec/apache2/mod_rewrite.so LoadModule php5_module libexec/apache2/libphp5.so

  2. Added MIME Type inside <IfModule mime_module> tag to above httpd.conf file

    AddType application/x-httpd-php .php

  3. Made sure that libexec/apache2/mod_rewrite.so exists

  4. php -v gives below result

PHP: parse error in /etc/php.ini on line 107 PHP 5.5.31 (cli) (built: Feb 20 2016 20:33:10) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

  1. sudo cp /etc/php.ini.default /etc/php.ini

  2. Restarted apache2 using command

    sudo apachectl restart

Still it shows the php code <?php phpinfo(); ?> when accessed from browser using url http://localhost/phpinfo.php

I have even tried enclosing the code above inside <html><body> in which case the browser screen was blank.


Solution

  • I am having the directory defined for the host as below

    DocumentRoot "/var/www/html" <Directory "/var/www/html"> Options Indexes FollowSymLinks Order allow,deny Allow from all AllowOverride All </Directory>

    The problem was solved by enabling and configuring virtual host, however I still think there was no absolute need for virtual host for making php work. Thoughts welcome on this.

    I then realised that I must enable and configure virtual host as well. I followed steps below

    1. Uncomment below lines from /etc/apache2/httpd.conf

    LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

    This will enable to define virtual hosts in apache2

    1. Edit file /private/etc/apache2/extra/httpd-vhosts.conf and declare virtual host as below, comment any default virtual host declaration

      <VirtualHost *:80> DocumentRoot "/var/www/html" ServerName myserver ServerAlias myserver-pro ErrorLog "/private/var/log/apache2/myserver-error_log" CustomLog "/private/var/log/apache2/myserver-access_log" common </VirtualHost>

    know that this file is included by default in /etc/apache2/httpd.conf using line below.

    Include /private/etc/apache2/extra/httpd-vhosts.conf

    and localhost/phpinfo.php from browser just works awesome.