Tuesday, December 02, 2008

How to expose and use preinstalled Apache with PHP on Mac OS X

There are no traces about this but the matter of the fact is Apache and PHP comes preinstalled on your OS X. Now the question is, "Where is apache on your OS X partition and how to use it, how to use PHP?".

As always, I would try to share my experience of finding and running Apache and PHP on OS X.

To start Apache Web Server, perform following steps:
  1. Open "System Preferences". You can do that from Apple Menu or can open it from Applications.
  2. Locate icon labeled 'Sharing". Click on it.
  3. Locate "Web Sharing" and select it. This should start up Web Sharing on your machine, which starts the Apache Web Server on your machine. Once the Apache Web Server is started, you can see URLs for "Your computer's web site" and "Your personal web site".
  4. Your Apache Web Server is running now.
  5. Start up your browser and open the URL (http://your-ip-address or http://localhost).
  6. If everything is OK, your web browser should show you Apache default page.
You can access the pages of your web directory at /Library/WebServer/Documents directory.

To access your personal web pages, type following URL in your browser:
http://localhost/~username/

You can access your personal web pages at /Users/user-name/Sites/ directory. You can add/delete/modify web pages in this directory to set your personal web-site.

Now that you have your Apache Web Server running, running a PHP script should look trivial, you might have done it before. But it is not so easy on OS X.

By default, along with other features, PHP is disabled on Apache. You will have to enable PHP support in Apache. Follow these steps to enable PHP in your Apache:


  1. We need to locate httpd.conf file to enable PHP with Apache. Go to /etc/apache2/. Open httpd.conf by typing sudo vi httpd.conf (you need super user privilege to modify this file).
  2. Type the root password and you should be able to modify httpd.conf file.
  3. Locate the line LoadModule php4_module or LoadModule php5_module.
  4. Uncomment the line by removing the hash (#) from the begining of the line.
  5. Warning! do not relocate the lines in this file.
  6. Save the file.
  7. By uncommenting the line LoadModule, you will load PHP module and allow Apache to serve any PHP file.
  8. Restart Apache by deselcting and selecting "Web Sharing" on System Preferences->Sharing.
If everything goes well, you should be able to execute PHP files on your server. You can test your PHP with Apache. Create a file called "phpinfo.php" with following contents and place it in /Users/your-name-/Sites/ folder.



< ? php
// Show all information, defaults to INFO_ALL
phpinfo();

?>



Type the following URL in your web browser to check PHP.

http://localhost/~user-name/phpinfo.php

It should display large page with all PHP statistics.


If everything goes as described, you are good to use PHP with your Apache Web Server on your OS X. Remember, we have nothing to install here. Everything is preinstalled on your OS X. What we did is enabled the Web Server and PHP on your machine.


Have a nice day!









No comments: