Search code examples
phpmacosapacheosx-mountain-lion

Running html-embedded php on Apache/Mountain Lion locally


With the Apache and php pre-installed on OS X Mountain Lion, here are the steps I thought sufficient to run a server locally—without installing MAMP (which would install duplicates):

  1. Uncomment the line LoadModule php5_module libexec/apache2/libphp5.so in /etc/apache2/httpd.conf.
  2. Add the appropriate /etc/apache2/users/calaf.conf.
  3. Save the php example above in ~/Sites/introductory-example.html (correction)
  4. Restart Apache by running sudo apachectl start (or sudo apachectl restart).
  5. Save a file containing <html><head><title>Example</title></head><body><php? echo "Hi"; ?></body></html> (or rather, be precise, this one) twice—first in /Library/WebServer/Documents/introductory-example.html and a duplicate in ~(whoami)/Sites/introductory-example.html).

I now expect that pointing to either http://localhost/introductory-example.html or to http://localhost/~calaf/introductory-example.html will give me a page with two features:

  1. The title of the page is "Example".
  2. The body of the page is "Hi."

Whether I am looking at the introductory-example.html in /Library/WebServer/Documents or in ~/Sites/ I get the first (the title is correct) but not the second (the body is empty)—why?


Solution

  • To my knowledge PHP is not activated system wide. It isn't in any system and/or combination I ever tried anyways. Apache is running PHP for files in a particular folder only (and its sub-folders). The standard apache folder should be in:

    /Library/WebServer/Documents/

    This guide should get you up and running although I haven't tried it myself.

    EDIT:

    You need to save a file which contain PHP code in .php, not .html. Rename or re-save the file to introductory-example.php

    The following URL do not work since the path does not exist.

     http://localhost/Users/calaf/Sites/introductory-example.html
    

    Use the following URL (you maybe need to remove the tilde ~)

    http://localhost/~calaf/introductory-example.php