Recently I started a project of PHP. I’ve learnt a little about PHP with Evan serveral monthes ago, and it turned out that I’m really easy to give up while Evan is really an intelligent person with hardworking. Anyway, I returned back to PHP again, and I am much more ambiguous now since I finally have a project to work on and I’m quite free in these days.
I can not give an acurate defination for PHP since I’m also a newbee to it, but what I’m sure is that it is a kind of scripting language which is used to develop dynamic webpages, runs at server-side, just like ASP.NET and JSP. Its homepage is http://www.php.net/. PHP always appear with Apache, MySQL, sometimes even other scripting languages like Python or Perl, so there is also packages that wraps them up, such as AMPPS and MAMP.
When it comes to Mac OS X, you feel just so cool since Mac OS has already installed Apache and PHP, but you have to turn PHP on since it’s disabled by default (Apache is always enabled so that you “Sites” can works properly). You should have a concept that PHP (command line “php”) is kind of a module in Apache (command line “httpd” or “apachectl”). Their versions in Mac OS X Lion 10.7.2 are PHP 5.3.6 and Apache/2.2.20 respectively.
Modify config file “httpd.conf” of Apache to turn PHP on. It’s location is:
drag it to your Text Edit and find a commented line:
#LoadModule php5_module libexec/apache2/libphp5.so
uncomment it turn PHP on (if you do not have permission to modify this file, just resave it to another proper location, modify that new one, then drag the new one back and click “Authenticate” when prompted to replace the protected one), then you have to restart you Apache to take effect:
sudo httpd -k restart
Installing MySQL is also very easy since there is a binary for Mac OS X in http://dev.mysql.com/downloads/, all you need to install are MySQL Community Server and an optional MySQL Workbench (GUI Tool) which is quite helpful and easy to use for a beginer.
Create you webpages in your home’s “Sites” folder (“~/Sites”). For example a .php file “1.php”, use Text Edit or any other good text editors to create a new file and type:
$link = mysql_connect(“localhost”, “root”, “123456”);
echo “Connecting failed.”;
save it to “1.php”, turn on Safari and type “http://localhost/~YourHomeFolderName/1.php”, you will see the result.
If there are warnings, like on my Mac, it’s because the default location for mysql.sock (“/tmp/mysql.sock”) is different to that php thinks (“/var/mysql/mysql.sock”), modify it here:
same trick as you did to modify httpd.conf of Apache, “resave and drag back”, find:
and replace it to:
For now everything will be fine, then you can develop PHP webpages now. After all, expert for these configurations of Apache and PHP which won’t be concerned again afterwards, developing with PHP is really easy and quick.
Here are also some pages you may refer to:
Enabling PHP in Mac OS X 10.5
Stopping and Restarting – Apache HTTP Server
Mac OS X Server 10.4: Issues connecting PHP to MySQL