Quantcast
Channel: Superfancy Industries » Coding
Viewing all articles
Browse latest Browse all 4

Setting up PHP, MySQL, and Apache in Mac OSX Leopard

$
0
0

One of the best features for web developers in Mac OSX Leopard is the inclusion of PHP and Apache. By default both are inactive and require you to get them ready for business. I will outline how to do this as well as get MySQL up and running so you can have a local development setup right on your Mac!

It’s all pretty easy to do but requires a little attention to detail in parts. So lets get started.


Most of the files we’ll be working with are actually hidden from Finder. If you use a text editor like Coda, BBedit, Text Wrangler, or TextMate you can choose to have hidden files “shown” so you can find them in your file structure. I personally use Coda so all that you have to do is click on the “View” menu and choose Show Invisible Files, as illustrated below:

Coda: Show invisible Files

You can also run this simple Terminal command to “show” all hidden folders in Finder:

defaults write com.apple.finder AppleShowAllFiles TRUE

Then restart Finder by typing the following:

killall Finder

You should now be able to see all hidden files.

PHP

To enable PHP browse to Apache’s http.conf file located in /etc/apache2/httpd.conf

Find this line:

#LoadModule php5_module libexec/apache2/libphp5.so

All you need to do is uncomment the line by removing the hash # symbol.

LoadModule php5_module libexec/apache2/libphp5.so

Now save http.conf. Don’t be alarmed if it asks for your system password… this is normal.

PHP 5.2.6 is the version shipped with Leopard. All the most popular extensions are already activated so you should be good to go. However if you need some of PHP’s extended features you may want to consider rebuilding PHP from source code as I’ve read the version that ships with Leopard has been problematic for some. I’ve never had any problems myself but it’s something to look out for. All relevant PHP settings are in the php.ini file. I won’t be covering any tweaks to that file in this article but you should be aware that it exists. By default, Leopard has an empty configuration file but provides a file which can be used as a template. From a new terminal window, type:

sudo cp /etc/php.ini.default /etc/php.ini

This creates your php.ini file, which is located in /etc/php.ini for your future reference.

You’ve now enabled PHP so lets move on to Apache.

Apache

Our friends at Apple were kind enough to ship a very modern version of Apache 2.2.6 for our development work. Kudos! To start up Apache click on the “Sharing” preference pane in System Preferences and enable “Web Sharing” like so:

Enable Web Sharing

You can also use the following command the start Apache from Terminal:

sudo apachectl start

You’ll be prompted for your system password. Type it in and press enter.

If you need to restart Apache, this will do the trick:

sudo apachectl restart

Keep in mind any editing you do to the http.conf or to the php.ini will require a restart of Apache. You can use the above command or simply restart “Web Sharing” in System Preferences… it’s up to you.

Testing PHP

So now hopefully if everything has gone according to plan, we’ll see some fruits of our labor. Go back to the “Sharing” Preference Pane in System Preferences and click on the URL/ IP Address below “Your computer’s website.” If all goes well you’ll see a page that says “Test Page for Apache Installation.” You can also type http://localhost/ in your browser which will take you to the same place.

Now in your text editor create a PHP file and type the following code:

<?php phpinfo(); ?>

Save it in /Library/WebServer/Documents/ (start from the top level directory of your hard drive, not the Library directory in your home directory) with the name test.php.
Now type http://localhost/test.php in your browser and you should see something like this:

PHP Info

Great success!

Setting up a personal website and virtual hosting

If you’re the curious type and tried to click on “Your personal website” in the Web Sharing panel you may have noticed that you were greeted with a “Forbidden” message. So lets go ahead and set it up so you can serve up files from your “Sites” folder. Open up your text editor and create a file named after your “Home” folder. In my case it would be StevieBenge.conf. This file will live in /etc/apache2/users/. You’ll need to replace StevieBenge with the name of your “Home” directory. I’ve heard reports that this file is created automatically on new installs of Leopard but I had to create mine manually. Add the following code to the .conf file:

<directory "/Users/StevieBenge/Sites/*/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</directory>

Like above, replace StevieBenge with the name of your “Home” directory. Save the file. This will allow Apache to serve files out of your “Sites” folder. Sweet! You’ll also need to restart Apache for this to take effect.

Moving right along, lets set up Apache for virtual hosts. Virtual hosts are essential if you want to serve multiple websites from your “Sites” folder. You can choose pretty much any “domain” name you want or mirror any production sites you have hosted on a live server. I like to name all of my local development sites with the extension .dev to keep easy track of things. Getting virtual hosts to work in Leopard was a huge stumbling block for me because of a lot of misinformation I read around the internet. I was was really happy to finally get this working. And as you’ll see it’s pretty straightforward.

So to get started, browse to /etc/hosts and add the following to the end of the file:

# Local aliases
127.0.0.1 steviebenge.dev

Remember to change steviebenge.dev to the name of a site in your “Sites” folder. 127.0.0.1 is an IP Address that tells your Mac that your site is hosted locally. The site will only be accessible on your Mac. Now we’ll need to reopen the.conf file we created earlier in /etc/apache2/users/:

<directory "/Users/StevieBenge/Sites/*/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</directory>
 
NameVirtualHost *:80
 
<virtualhost *:80>
    DocumentRoot /Users/StevieBenge/Sites/steviebenge.dev
    ServerName steviebenge.dev
</virtualhost>

Again make sure to change the directory structure above to reflect your “Home” folder and the folder within your “Sites” folder you want to serve files out of. Restart Apache for the changes to take effect. So to test it out and make sure everything’s working properly, go ahead and move the test.php created earlier into your new directory in your “Sites” folder. If it’s successful you’ll again see:

PHP Info

Too add more virtual hosts run through the above steps again. Simple as that!

MySQL

Now here is where the fun begins. It’s odd that Apple didn’t include MySQL with Leopard as it’s pretty much synonymous with PHP. Anyway, you have a couple options for installing MySQL on Leopard. You can roll your own and compile and install from source code or use MySQL’s package installer for Leopard. I won’t discuss how to compile MySQL here but I did find an excellent tutorial on how to do this over at Dan Benjamin’s Hivelogic site. I came pretty close to giving this a shot but I opted for the MySQL package installer. Dan brings up many salient points on why it’s beneficial to compile your own install in his tutorial, so I’d highly recommend checking it out.

So head over to the MySQL downloads page and grab a copy of the Mac OS X 10.5 (x86) installer (currently 5.0.67). You can also optionally download the MySQL GUI Tools, which I would recommend as that is how I prefer to administrate MySQL. The Mac Installer is labeled MAC OSX 10.4 (Universal binaries) but, in my experience, it will work without issue on Leopard.

Install MySQL and the GUI Tools. There’s also a Preference Pane that you can install that allows MySQL to be stopped and started from System Preferences. Go ahead and install that as well.

After you install the MySQL Preference Pane, find it in System Preferences and start MySQL like so:

MySQL Preference Pane

If you prefer starting MySQL from Terminal, you can do so with this command:

sudo /usr/local/mysql/support-files/mysql.server start

And you can use this command to stop MySQL:

sudo /usr/local/mysql/support-files/mysql.server stop

Now we’ll need to put the mysql.sock in the proper directory so MySQL can communicate with PHP.

Getting MySQL and PHP to communicate

The default location of mysql.sock is in the /tmp/mysql.sock directory. We’ll need to change this to /var/mysql/mysql.sock as this is where PHP will look for it.
So first off, create a my.conf (or in this case my.cnf) file in your text editor and save it as my.cnf in the /etc folder with the following code:

[client]
socket = /var/mysql/mysql.sock
 
[mysqld]
socket = /var/mysql/mysql.sock

Next move mysql.sock to it’s new directory by typing the following code in a Terminal window:

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Believe it or not, that’s pretty much it. One thing to keep in mind is the MySQL GUI Tools will still look for the mysql.sock in the old location so when you log in to MySQL Administrator enter /var/mysql/mysql.sock in the “Connect Using Socket” box under More Options:

MySQL Administrator

Now you’re ready to create your first MySQL Database. The MySQL Administrator makes this really easy, which I will cover in a future article. You can also administer your MySQL databases from Terminal. As I stated earlier, I prefer the GUI Tools but learning how to do things from Terminal is a useful skill to have. It’s totally up to you.

Bonus Information

I’ve read that you can also deal with the mysql.sock issue by editing the mysql.default_socket = line in your php.ini file to reflect the location of mysql.sock. I chose not to do this, but it may work for others

You may want to to change php.ini to report all PHP errors while you are in development. This can be done locating the following line:

error_reporting = E_ALL & ~E_NOTICE

And changing it to:

error_reporting = E_ALL

This is fine in your local development setup but of course not advised for a production server.

At the beginning of this article I illustrated how to “show” hidden files in Finder. Here’s that Terminal command again:

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

You can also “hide” those same hidden files with this command:

defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

Another great resource for an all-in-one development setup is MAMP. Using MAMP negates the need to do any of the above tweaking as it has its own versions of Apache, PHP, and MySQL. It’s perfect for those that want to get up and running quickly with a minimum of fuss.

Conclusion

Well this should get you started with PHP, MySQL, and Apache in Mac OSX Leopard. My inspiration for writing this article was to combine a lot of the information I read from other sites while getting my MacBook Pro set up. Many of the articles I came across were written around the time Leopard was released last Fall so some of the information was a bit out of date. Not that any of the above methods are terribly original on my part, but I’d like to credit some of the sources that helped me:

procata.com: This was the best resource I found and well worth checking out as it shows you how to install the PEAR extension. Has some great tips that I didn’t cover here.

Installing MySQL on Mac OS X: MySQL’s own how-to guide.

adactio.com: Jeremy Keith’s guide.

foundationphp.com: Excellent screen shots with this PHP and Apache tutorial.

Thanks for reading and check back as I’ll be writing more articles about working with PHP, MySQL, and Apache. Feel free to keep the discussion going in the Comments below.


Viewing all articles
Browse latest Browse all 4

Trending Articles