How to Install PHP on Mac


Enable Apache on OS X
Before you can enable PHP on a Mac, you first have to enable Apache.This process is extremely easy.
  1. Click the 'System Preferences' icon in the Dock.
  2. Click the 'Sharing' icon in the Internet & Network section of the System Preferences window.
  3. Place a check mark in the ‘Web Sharing’ box.

That's it! If you want to test if it worked, point your browser to http://localhost/ and you should see the standard Apache test page.

Enabling the PHP Module on OS X
For this next part you are going to need a plain text editor that will allow you to open hidden files. TextWrangler because it's free and it gets the job done. These next steps might be slightly different if you are using a different editor, but the principle is still the same.
  1.  We want to open the file located at HD:private:etc:apache2:httpd.conf. In TextWrangler we do this by choosing "Open Hidden" from the File menu.
  2. Since it is a root file, we need to unlock it before we can edit. In TextWrangler this is done by clicking the pencil button in the top left hand corner. You will get a pop-up confirming that you want to do this.
  3. Go to line 114, the line should read # LoadModule php5_module libexec/apache2/libphp5.so
  4.  Delete the #, the line should now read LoadModule php5_module libexec/apache2/libphp5.so
  5.  Save the file. You will be prompted for your password if you are using TextWrangler.

PHP Config
The next thing we want to do is make a copy of php.ini.default and save it as php.ini. Once we have done this we can edit our PHP preferences.
  1.  First open up Terminal. It is located inside your Systems folder which is located inside your Applications folder. 
  2. Enter the following lines, one at a time:
    cd /private/etc
    
    sudo cp php.ini.default php.ini 
    
    
  3. You may now open your new php.ini file using TextWrangler or another editing programing and change the default settings if you wish. It will be located in Macintosh HD:private:etc



Testing PHP
Finally we need to run a test to make sure that PHP is running.

  1.  First create a simple test file. A good thing to include is phpinfo().Sometimes it is necessary to find out more information about the version of PHP your server is running.
  2. You can find out the PHP version, as well as many specific settings just by running 
    
    a very simple one line program from your browser.
    The phpinfo(); command will bring up a full profile of the PHP you are currently 
    
    running, and display it in categorized tables for you to view.
    
  3. If your PHP is working your test program should display your PHP's information. If it displays your code, you need to restart Apache. 

You can restart Apache by entering the following line in Terminal:


sudo apachectl restart


Restarting will also require your password.
Congratulations, you have now activated PHP on your Mac, enjoy!

1 comment:

Unknown said...

I have learnt a lot from here which I was completely unaware of. I liked every single point mentioned above.Thanks.
web design courses