Your shiny Mac comes preinstalled with a web server and php but you need to set it up to get going. Starting the web server is as easy as enabling “Web Sharing” under networks settings. I did this but the server did not respond at http://localhost. So, what was wrong?
Well, I recently upgraded my hard drive to a 500GB version and I used Time Machine to restore my operating system. The problem is that this is not a clone of my previous setup but a copy and it leaves out certain files that Time Machine ignores to save space. One of those folders is the apache web server log folder which is required by apache at startup. Unfortunately OS X doesn’t tell you anything about this error when you enable Web Sharing, it just doesn’t work.
Anyhow, simply create the folder:
sudo mkdir /private/var/log/apache2
and you’re good to go. You might have to disable and enable Web Sharing again to restart the process.
So, now the web server is running. What about php? Edit the apache config file located at /etc/apache2/httpd.conf and remove the ‘#’ (comment) from the line that says:
Now you can test that it works by adding a file called helloworld.php to the folder /Library/WebServer/Documents. Add the following code snippet to the file:
< ?php echo 'Hello World!'; ?>
And point a browser to http://localhost/helloworld.php. Now three things can happen. 1) no response. Did you restart the server? 2) The exact content of the file is shown in the browser. This means the PHP module is not loaded. Make sure the httpd.conf file has the comment character removed as described above and then restart the web server by disabling and then enabling web sharing again. 3) Hello World! (and nothing else) is printed in the browser. Success!