Blog

03/06/2019

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



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?

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

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

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

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

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.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....