Motivated by this question at StackOverflow: RegExp PHP get text between multiple span tags, I decided to help.
Recently I got a Mac mini. I still hadn’t played with PHP on Mac and to debug my answer to that question I needed a way to test the code. So I thought: why not also give PHP a try on Mac OS since its my main OS today? Oh, good idea, go learn something new… :D
The first thing I did obviously was recurring to Google and searching for something that could help me get there.
I hit a pretty good tutorial to enable PHP on Mac at About.com written by Angela Bradley that gets to the point: How to Install PHP on a Mac. Along the way I had to solve only one minor thing described in the caveat section at the end of this post.
You see that the title of this post has the word installing (well I thought I had to install it – that was my first reaction), but in fact it could be the word enabling because PHP is an integral part of Mac OS X Snow Leopard and we just need to enable it as you’ll see soon.
Here we go. Follow these steps:
1 - Enabling the Web Server
PHP works hand in hand with a webserver. Mac OS already comes with Apache web server and so we just need to enable it. To do so, open System Preferences in the Dock. Then click the 'Sharing' icon in the Internet & Network section. Check the ‘Web Sharing’ box.
Figure 1 - Web Sharing option under the Sharing configuration in System Preferences
Now type this address in your browser: http://localhost/. You should get a message that reads: It works!
2 - Enabling PHP
PHP also comes bundled in Mac OS, but it’s disabled by default. To enable it we need to edit a hidden system file located in this path: /private/etc/apache2/httpd.conf.
I used BBEdit text editor to edit such a file (note that I marked the box Show hidden items in the screenshot below):
Figure 2 - Editing the hidden system file httpd.conf with BBEdit
Now within that file search for:
libexec/apache2/libphp5.so
Delete the character # in the start of the line so that that entire line should now read:
LoadModule php5_module libexec/apache2/libphp5.so
Save the file.
3 - Testing the installation
There’s nothing better to test the PHP installation than using its own information. To accomplish this, write a one liner simple .php file named test.php with this content:
<?php phpinfo() ?>
Place this file inside your personal website folder. Mine is located in this path:
/Users/leniel/Sites/test.php
Now let’s test this page by typing its address in the browser:
http://192.168.1.103/~leniel/test.php
As you see, the address points to my personal website as seen in Figure 1.
When you run this simple .php page you should get something like this:
Figure 3 - Testing PHP installation with its own configuration’s information
If your PHP installation is OK, the test page will display PHP's information. If it displays the page code, you need to restart the Apache server.
You can restart Apache by entering the following in Terminal:
sudo apachectl restart
Try to reload the page again. Everything should work.
Well done!. Now you can play with PHP on your Mac computer and write some neat codez. :)
Caveat
When I tried to restart Apache server I got the following error:
ulimit: open files: cannot modify limit: Invalid argument
I resorted to this solution: Mac OS X 10.6.5 broke my apachectl