OS X Mountain Lion 10.8 Set Apache and PHP Web-Server

by on August 25, 2013 · 3 comments· LAST UPDATED August 25, 2013

in , ,

I am using Apple OS X Mountain Lion 10.8.4 on my Macbook pro. For my school project I need to write an app in PHP 5 and Apache. However, I am unable to find the Web Sharing option from the Sharing panel in System Preferences. How do I install Apache and PHP5 in OS X version 10.8.4? How do I enable HTTPD and test my web app?

Tutorial details
DifficultyAdvanced (rss)
Root privilegesYes
RequirementsOS X 10.8.x+/Terminal
Estimated completion time15m
Apple removed the web-sharing option from the Sharing panel in System Preferences in OS X version 10.8.x. But worry not, Apple still ships Apache+PHP5 and it is disabled by default for security reasons. In this tutorial, you will learn how to enable and run Apache server with PHP 5.

Configuration files

  1. Apache configuration file - /etc/apache2/httpd.conf
  2. PHP 5 configuration file - /etc/php.ini

Finding default Apache DocumentRoot

Open the Terminal and type the following command:

 
grep --color DocumentRoot /etc/apache2/httpd.conf
 

OR

 
grep --color '^DocumentRoot' /etc/apache2/httpd.conf
 

Sample outputs:

DocumentRoot "/Library/WebServer/Documents"

You need to store or create php/html/images/js/css app files at DocumentRoot i.e. /Library/WebServer/Documents directory.

How do I start / stop / restart Apache server?

Type the following command to start / stop / restart apache web-server in OS X:

 
sudo apachectl start
sudo apachectl stop
sudo apachectl restart
sudo apachectl graceful
 

Test your setup

Fire a browser and type url:
http://127.0.0.1/
OR
http://localhost/
OR
http://ip-address-of-your-mac-os-x/
Sample outputs:

Fig.01: Running Apache for the first time

Fig.01: Running Apache for the first time

How do I change the default index.html?

Type the following command to create or edit the index.html file:
sudo vi /Library/WebServer/Documents/index.html
Sample code:

 
<html>
<head>
	<title>My first html page</title>
</head>
<body>
<h1>Hi, User</h2>
<p>This is a test web-page. It was created on OS X 10.8 Mountain Lion using the following:</p>
<ol>
	<li>Vim text editor</li>
	<li>Apache 2.x</li>
	<li>PHP 5.x</li>
	<li>Gimp</li>
</ol>
<hr>
<center>
	<small>&copy; 2013 nixCraft.</small>
</center>
</body>
</html>
 

Save and close the file. Fire a browser and type url:
http://127.0.0.1/
Sample outputs:

Fig.02: Updated index.html file

Fig.02: Updated index.html file

Configure and Turn on PHP 5 support for Apache

Edit the file /etc/apache2/httpd.conf
sudo vi /etc/apache2/httpd.conf
OR better try vi/vim syntax to open file and go to line called php5_module:
sudo vi +/php5_module /etc/apache2/httpd.conf
Find and make sure the following line is uncommented:

 
LoadModule php5_module libexec/apache2/libphp5.so
 

Sample outputs:

Fig.03: Enabling php5

Fig.03: Enabling php5


Type the following command to restart apache web-server in OS X:
sudo apachectl -k restart
OR
sudo apachectl restart
Create a test php script as follows:
sudo vi /Library/WebServer/Documents/phpinfo.php
Add the following code:

<?php
 //Display the configuration info, line, php.ini location, build date, Web Server, System, Version and more.
 phpinfo(INFO_GENERAL);
?>
 

Type the following url:
http://localhost/phpinfo.php
Sample outputs:

Fig.04: Testing PHP 5 in OS X v10.8.x

Fig.04: Testing PHP 5 in OS X v10.8.x

How do I start/reload Apache on boot?

Make sure Apache+PHP5 get reloaded when you reboot the system:
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

And, there you have it, the Apache and PHP 5 installed, configured and working correctly on the latest version of Mac OS X 10.8.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 mudfly August 25, 2013 at 5:09 pm

The better option is to install a virtualization product and run your development code there. I would check out virtualbox and vagrant.

Reply

2 shanky November 29, 2013 at 3:11 pm

Thank You very much for sharing this Info …. Works perfectly ….

Reply

3 Manuel January 5, 2014 at 6:15 pm

Hi, I have been struggling for a while trying to configure my MAMP, I’ve work with Linux before and I manage to configure my LAMP pretty well. But MAC is ginving me a hard time. What I’ve could do so far is starting the apache serve although i dont know if the apache server I started is the one that i am seeing on my browser, that because when i go to localhost i see the It Works! message, but if i change the index.html in /Library/WebSever/Documents/ and reload the browser or open another browser and search the address localhost, I am still getting the same It Works message and not the one i’d change. I have try different browsers, clearing cache, incognito windows. I getting crazy here. I would appreciate some help

Reply

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: