How to setup a PHP/MySQL development environment on OS X

903 views, 0 replies
Tagged in: apple os-x web-development php 
+1
0 / 1

Congratulations, OS X comes bundled with Apache and PHP, so you're actually half way there already!

Open System Preferences > Sharing, and enable Web Sharing to start the built in Apache web server. You should then be able to browse to localhost and be greeted by an "It works!" page. You can also try browsing to localhost/~YourAccountName to be greeted by your OS X account's personal homepage.

By checking and unchecking the Web Sharing option, you are effectively starting and stopping the Apache web server. You'll need to stop/start Apache throughout the configuration process for new settings to take effect, so remember the above process. Alternatively, you can restart Apache from the command line using sudo apachectl restart.

You now have Apache started, but PHP support is not enabled by default.

To enable PHP, you'll need to modify /etc/apache2/httpd.conf. To do so, you'll need a text editor such as TextMate, TextWrangler, vi or nano. TextMate is excellent, but it's also paid commercial software. TextWrangler is free (as in beer) and available from barebones.com/products/textwrangler ... if you opt to use vi or nano, I'll assume you know what you're doing.

In TextWrangler, go to File > Open File by Name... and paste in /etc/apache2/httpd.conf. Find the line that reads:

#LoadModule php5_module libexec/apache2/libphp5.so

... and remove the # to uncomment the LoadModule instruction. The line should then read:

LoadModule php5_module libexec/apache2/libphp5.so

Save the file.

You'll also need to prepare a copy of php.ini to use. A default copy is provided, but you must rename it for it to take effect (or more appropriately, copy it). Type the following at the command line:

cp /private/etc/php.default.ini /private/etc/php.ini

Save the file and restart Apache using the procedure mentioned at the beginning of this article. PHP is now enabled.

If you don't want to install a database (MySQL, PostgreSQL, etc), you can jump into writing PHP. You can replace the "It works!" page by editing the files located in Library/WebServer/Documents/. Alternatively, and perhaps preferably, you can replace your personal account page by editing the files in Users/[YourAccountName]/Sites/.

If you're ready to setup MySQL, continue on to this article: tagg.im/thread/117/how-to-setup-mysql-on-mac-os-x

To use MySQL via a UNIX socket, you'll also have to fix an issue with the default copy of php.ini. Open your new /private/etc/php.ini file in your text editor, and update the following line:

mysql.default_socket = /var/mysql/mysql.sock

... such that it reads:

mysql.default_socket = /tmp/mysql/mysql.sock

At some point in OS X's past, mysql.sock was moved from /var to /tmp, perhaps for security reasons, but this hasn't been reflected in the provided PHP ini file. This tweak will fix issues such as the following error message thrown by Symfony/Doctrine:

Warning: PDO::__construct(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in /Applications/MAMP/htdocs/www/jobeet/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Connection.php on line 470

PDO Connection Error: SQLSTATE[HY000] [2002] No such file or directory  

As for development tools available on OS X, the consensus seems to be that NetBeans PHP is currently leading the pack of IDE's. It's available on Windows, Linux, and of course, OS X: netbeans.org

Edited 1 year ago Flag this comment as innapropriate
Posted 1 year ago by Dolph (120)
Post a reply anonymously: