How to setup Apache and PHP on Mac OS X Snow Leopard
Now it’s time to continue with setting up Apache and PHP. If you’re coming here through search engine then you probably want to check our previous posts on [intlink id=”how-to-create-a-wordpress-development-environment-on-mac-os-x-snow-leopard” type=”post”]how to install and configure WordPress on Mac OS X Snow Leopard[/intlink].
Fortunately Snow Leopard comes with the latest PHP version (version 5.3.0 at the time of this post) so that’s covered. Please note that PHP is not by default turned on in Snow Leopard. To make sure that you’re configuring everything correctly it’s best to make Web Sharing active (System Preferences » Sharing » Web Sharing). Once that’s done we’re free to go ahead and configure setup PHP and configure Apache.
Lets setup PHP first
In order to do so copy php.ini.default to php.ini cp /private/etc/php.ini.default /private/etc/php.ini
. This way we’ll keep the original php.ini.default just in the right place for any future reference. If you know what you’re doing and need any special stuff then go ahead and modify the php.ini as you feel fit.
Configure Apache web server
If you have the default Apache installation (and I assume you do) then you find your httpd.conf under /private/etc/apache2/httpd.conf
. I expect that you have the technical ability of a software developer familiar with Linux commands. Now edit the httpd.conf file using root access with the sudo command and your favorite text editor. I personally prefer vim if I need to get something done on Terminal but you can also use TextMate or any other text editor.
Enable PHP5 module
Search for the PHP module (/php5
. Then it would be good idea to remove the comment from the following line in order to enable PHP to run with Apache: # LoadModule php5_module
. The line should look like that after you’re done: LoadModule php5_module
.
Enable mod_rewrite for permalinks
If you like beautiful URLs (permalinks) then make sure that you have mod_rewrite Apache module enabled. It should look something like that in your httpd.conf: LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Change the user Apache runs as
I prefer not to use Mac OS X Snow Leopards default location for sites (the Sites folder in your Home folder). On my case I have a folder called “Development” in my Home folder that has all the development projects organized in there. So in this case it’s also good idea to run Apache under your own user in development environment (which your Snow Leopard installation most probably is). In your Apache configuration you’ll find that both user and group that Apache is run with is _www.
Find and change
Group _www
to (replace Martin with your own users name)
Group Staff
Final check for virtual hosts
Make sure that you have these two lines at the end of your httpd.conf. Otherwise you’re most probably going to find out soon that your virtual hosts simply doesn’t work.
Include /private/etc/apache2/other/*.conf
Restart Apache
Now lets start (sudo apachectl start
) or restart (sudo apachectl restart
) the Apache web server and test if PHP is running by creating a file in your Web directory containing a call to the function phpinfo()
.
It works!
Final push – setup virtual hosts for your sites
At the time of writing this post I have 3 WordPress themes in development plus quite a few that I’m still maintaining. This kind of setup could easily become a mess and I really wouldn’t like that, now would I. Fortunately there’s a fix: create a virtual host for each WordPress installation.
Custom hostnames for your virtual server
I’m using a simple convention for my WordPress projects – projectname.wordpress.dev. Fortunately this keeps all WordPress installations in one clean convention which is also easy to deploy to production server (we’ll get to that later on).
To create your hostname just open up the hosts file with your text editor:
Write something like that at the end of the hosts file.
127.0.0.1 www.perfectline.wordpress.dev
Done. You now have a hostname for your virtual host named perfectline.wordpress.dev which you can also access by www.perfectline.wordpress.dev if you like.
Configure a virtual host
Now that we are sure that we have PHP turned on, Apache working properly and even hostnames in place it’s time to create the actual virtual host that will be serving content for your WordPress installation.
First you need to create the necessary folders. In my case I’ve created Development folder where all my projects live plus one level to specify the type of the project (WordPress, Ruby, etc) and an actual WordPress project name:
mkdir ~/Development/Wordpress
mkdir ~/Development/Wordpress/default
Create a new configuration file for your WordPress related virtual hosts:
Paste these lines into the newly created wordpress.conf. You need to change the file paths as you’ve set up the folders on your own system.
ServerName wordpress.dev
ServerAlias www.wordpress.dev
DocumentRoot "/Volumes/Macintosh HD/Users/Martin/Development/Wordpress"
<directory "/Volumes/Macintosh HD/Users/Martin/Development/Wordpress">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</directory>
</virtualhost>
<virtualhost *:80>
ServerName default.wordpress.dev
ServerAlias www.default.wordpress.dev
DocumentRoot "/Volumes/Macintosh HD/Users/Martin/Development/Wordpress/default"
<directory "/Volumes/Macintosh HD/Users/Martin/Development/Wordpress/default">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</directory>
</virtualhost>
Now it’s time to restart Apache for one more time and we are set. Enjoy your newly installed WordPress development platform.
19 Comments
Thanks for writing this up. It helped!
Thanks for this article, really interesting.
Could you tell me your process for transferring to the live server? I always find this a hassle and somewhat stressful!
Hello… how do I completely remove the default apache and php installations from Snow Leopard?
To be honest, Felix, I have absolutely no idea why anyone would like to do that (except installing latest and greatest version of either). From my own experience Apache and PHP bundled with Mac OS X Snow Leopard is more than enough for any development work you might want to do.
You can of course disable Web Sharing from Mac OS X System Preferences (that will stop Apache) and install a newer version of Apache (or Nginx) from Macports.
I can get an info. php file that contains : to work in the /Library/WebServer/Documents folder; however, when I place it in my wp-admin folder within my sites folder my browser keeps asking me where I want to save the file.
Any help would be appreciated.
You are using wrong path in your browser to se the file.
That should work, try :
127.0.0.1/info.php
————————————————-
(/Library/WebServer/Documents/info.php will NOT work)
Thanks for the tutorial!
One suggestion: copying php.ini.default to php.ini requires Administrator privileges, so type ‘sudo’ before that command if permission is denied. I’m quite new to Mac and just learned that :)
Thanks for the great guide. I’m using 10.6.8, and found a little issue:
Your Step:
Create a new configuration file for your WordPress related virtual hosts:
sudo vim /etc/private/apache2/other/wordpress.conf
the path should be:
/etc/apache2/other/wordpress.conf (leave out the ‘private’)
Thanks for pointing to that Bob.
However, in Mac OS X,
/private/etc/apache2/other/wordpress.conf
is the real location of the file and/etc/apache2/...
is just a symlink to it.Are we sure that it is actually safe to change user Apache runs as?
For those that have just upgraded to 10.7 Lion and are wanting to use the same setup.
I have found that the “staff” group has to be in lower case
Thanks for that tip – exactly the detail I was missing!
This very helpful and the steps were explained well. Thanks very much.
Can I use jboss instead of apache?
This is a important submit! Thanks for it! Along with best wishes Luke aka couchgool.
just a quick one, how would you set up sql databases or manage them using this technique?
Martin, great tutorial and just what I was looking for! I had minor issues with a couple of steps, and am wondering if anybody else had the same issues:
When changing the group Apache runs as, I had to use staff (lower-case s) instead of Staff as in the example. It would not work the other way.
Also, on my system, the commands to start/restart Apache using apachectl require the -k option. I had to type sudo /usr/sbin/apachectl -k restart. It would not work without the -k.
Best regards
Ben
Thanks so much, “Change the user Apache runs as” saved my life :)