
I’ve upgraded both my Macs to Snow Leopard, and a few things have changed with the built in Apache webserver.
As a web developer it’s important for me to have a fully functional development environment so the first thing I had to do with the new install is get Apache set up and running as I need it.
This post shows how to set up PHP, mySql, Virtual Hosts & .htaccess
PHP
By default PHP is disabled so let’s turn that on.
In the finder navigate to the /etc folder, this houses all the files we need to configure. This is a hidden system level folder so to access it press “apple shift G” and type /etc.

Find the folder Apache 2 and open httpd.conf
This is the main config file for Apache, I use textMate to edit these files but you can use any text editor.
Go to line #115 and uncomment (remove the starting #) “LoadModule php5_module”
Save the file, you will be asked to enter your password.
After every change you make to the Apache or PHP config files you will need to restart the webserver. This can be done in the Terminal with “sudo apachectl graceful” or go to System Preferences and untick then tick Web Sharing in the Sharing Pane.
Now PHP is running.
Error Messages when using date() function
PHP has been upgraded to V5.3 and the Snow Leopard version doesn’t have a timezone set, this will give you errors when you use the date() function. To fix this go back to the “/etc” folder and open “PHP.ini.default”
Go to Line #997 and enter your timezone, here in the UK it’s Europe/London find your timezone.
Remove the semicolon and the line should read “date.timezone = yourtimezone”
Save this file as PHP.ini in the etc folder, restart the webserver and PHP is now all good to go.
mySQL
mySQL doesn’t come as standard on OS X so we need to install it luckily this is nice and easy because we can get a dmg file. I choose the 64bit version, head over and grab it scroll down to “Mac OS X (TAR packages) downloads” and choose “Mac OS X 10.5 (x86_64)”.
Open the dmg and run the installer. I also installed the prefPane, double click the “MySQL.prefPane” It’s a good idea to use this as you can use System Preferences to start/stop mySQL.
Setting the password
The mySQL install doesn’t set a password so we need to set it.
For this task we need to use the Terminal. Open Terminal located in the “Applications/Utilities” folder and type “/usr/local/mysql/bin/mysqladmin -u root password yourpassword” (where yourpassword is the password you want.) hit enter and the password is set.

Fixing the PHP socket
Before we can use mySQL we need to point PHP to the correct place. Open the PHP.ini file as before and change line #1213 to mysql.default_socket = /tmp/mysql.sock
Save this file and restart the server.
phpMyAdmin
I use phpMyAdmin to manage mySQL. So here’s how to do that.
Head over to the phpMyAdmin Site and download the latest version.
Unzip the downloaded file and place in either your Sites folder or the servers root (Library/WebServer/Documents) I use the root as I class this as a management tool and like to keep my Sites folder for projects.
This needs configuring too. It does come with a documentation file so you could go through that and set it up as you like. I keep mine nice and simple and have it so I don’t need to log in each time.
Open “config.sample.inc.php” and add after “$i++” the lines:
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'yourpassword';
$cfg['Servers'][$i]['auth_type'] = 'config';
Remove the line “$cfg['Servers'][$i]['auth_type'] = ‘cookie’;”
Save this file as config.inc.php.
In a browser go to “http://localhost/phpMyAdmin/” if you moved the folder to the root as I did or if you placed into your Sites folder go to “http://localhost/~yourusername/phpMyAdmin”.
Virtual Hosts
Open the “httpd.conf” and uncomment line #461 which reads “Include /private/etc/apache2/extra/httpd-vhosts.conf”, then save.
Now open “/etc/apache2/extra/httpd-vhost.conf”
I commented out each VirtualHost block and made my own underneath, You really do need this one:
<VirtualHost *:80>
DocumentRoot /Library/WebServer/Documents
ServerName localhost
</VirtualHost>
After this block you can add your own
<VirtualHost *:80>
DocumentRoot /Users/youruser/Sites/yoursitefolder
ServerName yoursitesname
</VirtualHost>
You will need to add a new VirtualHost block for each VirtualHost you wish to set up.
Save this file and we’re almost done. Open “/etc/hosts” and add “127.0.0.1 yoursitename” you will need to add a new line for each Virtual Host you have set up where “yoursitename” matches the name chosen name in the VitrualHost block in the previous step.
Save this file and restart the webserver. Now in a browser type the VirtualHost name and you will see the corresponding site. A link to “/” within this site will take you to the root of that site as if it was on a remote server.
.htaccess
Open the “httpd.conf” file and change line #210 to read “AllowOverride All”.
Open “/etc/Apache2/Users/youruser.conf”
Change AllowOverride to All
Add a new Directory Block
<Directory "/Users/youruser/Sites/*/">
Options Indexes MultiViews FollowSymLinks
</Directory>
Save and restart the server. Everything should now be all set up and you can use .htaccess and mod_rewrite.

53 Comments »
Fantastic tips. I was almost giving up setting phpMyAdmin and I was going back to XP but I followed your instructions and everything worked smoothly.
Great post!
To have the date.timezone affect the server I also had to rename “PHP.ini.default” into “PHP.ini”
Hi Marco thanks for your comments, glad I was of some help. Yes you are right you do need to change php.ini.default to php.ini.
appreciate this. was struggling with .htaccess rewriting.
Thanks – another tip for some – make sure the extension is mysql, not mysqli.
$cfg['Servers'][$i]['extension'] = ‘mysql’;
Marco,
For some reason I cannot save my httpd.conf file. It does not prompt me for a password, but just says that the file cannot be saved. I’m using TextEdit. Suggestions?
Thanks from me too.
Marco,
You can’t save the file because it is owned by root and you are not allowed to edit. The way I get around this is to edit in the terminal using vi (a text editor) but this may not be a little old fashioned. NOt sure how this is achieved in the more standard applications – is there a run as option?
In answer to Damon, I had the same problem with overwriting the httpd.conf file from TextEdit. But I found a work-around.
Open up the httpd.conf file and make the edit to it, then ‘Save as’ to another location like your desktop (make sure you add the .conf extension) Then drag and drop the edited httpd.conf file from your desktop into the apache2 folder. It will complain about something and then ask for a password, but it will copy over.
Hope it helps.
How come when i call localhost in browser everything is ok, but when from my iphone or other comps in network type the IP adress (10.0.1.11 <— is the webserver) nothing shows up, blank page.
What am i doing wrong??
ty for help
Hi Herman
You are right, you do need to type the ip address on another machine/iPhone. Make sure all devices are on the same network. You can get the IP address from system prefs > sharing > web sharing it will normally be something like http://192.168.0.2. If you are using the sites folder you will need to put /~yourusername. Hope this helps.
An outstanding tutorial and very nicely formatted, nice work.
As a longtime Linux developer I recently switched to using Snow Leopard as a staging server. This saved me a lot of rooting around.
I upgraded from Leopard to Snow Leopard on my early 2009 Mac mini. I tried getting virtual hosts setup using a number of other tutorials and none of them worked. I came across this one and was getting closer. The problem I am running into is that when I hit “http://mysite.local”, it simply shows me the default index.html.en page under /Library/WebServer/Documents/
I’ve checked and double checked all of these files as per the instructions in this article:
/private/etc/apache2/httpd.conf
/private/etc/apache2/extra/httpd-vhosts.conf
/private/etc/hosts
When I look at my /private/var/log/apache2/error_log file, I don’t see any errors showing up.
My best guess is that the httpd-vhosts.conf file is not being loaded even though I have uncommented the line to include it. I even tried adding some english sentences to trigger an error on apache restart and it doesn’t complain.
Can someone point out something I might be missing?
Thanks
Thanks for the clear tutorial, Kev. One small detail – going to the MySQL page, you go to the “package format” section to download the .DMG with the installer file, not the “TAR packages”, which uncompress into a bunch of files and folders rather than an installer.
Also – there’s a 64-bit pref pane for starting and stopping MySQL at http://www.swoon.net. Otherwise System Prefs has to relaunch in “32-bit mode” every time you want to start or stop MySQL from the pref pane.
Thanks for this tutorial, was very helpful. One detail – I’m working on new iMac 21.5″ snow leopard and I did everything you write is working find if my file are in webserver document but if i try on site I have this error:
“Forbidden
You don’t have permission to access /~Imac/index.php on this server.”
index.php is your phpinfo.
Did I miss something??
Damon:
the file do not get saved because it need root privileges.
>sudo vi httpd.conf
password: {put your own admin password}
Edit the file using vi.. check in the web for a small tutorial.
I’m new to mac so excuse me if ther may be a simpler way but for me this was pretty simple. i had the same issue of not being able to save, and the resolve i found was simple for me. all i did was:
1. Go to the folder that houses the file you are trying to save
2. click and go to get info
3. Go to sharing and permissions
4. Go to the bottom where you see the lock icon and unlock, at this point you will get prompted for your password, once you’ve done that
5. You can either change the permission for the “everyone” Profile to read/write or add your profile (which should come up automatically) to the list by clicking on the + icon on the bottom left
6. Once done you should be able to save your file. don’t forget to revert back to the read only profile if you need to.
Note the PHP file is in the etc folder which is greyed out. and i couldn’t change permission so i had to go to show original which then brought me directly to the etc folder where i was able to carry out the above.
Hope this helps out
Kev, thank you immensely for this clear and helpful tutorial. But..I have run into what I suppose is a password problem with mySQL that I do not know how to solve.
I got mySQL installed and running, then I set my PW (let’s say it was “zygote”) in Terminal by entering: “/usr/local/mysql/bin/mysqladmin -u root password zygote” and got no complaint…just the usual prompt for the next command.
Next, I downloaded and installed phpMyAdmin folder in Library/Webserver/Documents (changing the name of the folder from “phpMyAdmin-3.2.5-all-languages” to “phpMyAdmin”), and added these lines to saved config.inc.php file:
$cfg['Servers'][$i]['user'] = ‘root’;
$cfg['Servers'][$i]['password'] = ‘zygote’;
$cfg['Servers'][$i]['auth_type'] = ‘config’;
Restarted the web server. But…when I entered “http://localhost/phpMyAdmin/” in Safari, I got this error:
“Welcome to phpMyAdmin
Error
MySQL said:
#1045 – Access denied for user ‘root’@'localhost’ (using password: YES)
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.”
Any suggestions would be most welcome!
This is a great post!! Nice and Clean! Great work – thanks for the info!!!
This is a great post, very clear and intuitive..! Thank you
For Robert: Total newbie at all this but after many many hours I tried ‘mysql’ as the user, and no password (so just leave the quotes like this ”) and it worked. Nothing to do with root at all!
Now I find phpmyadmin wont let me make a new database so I’ll read more on users!
Thanks for a great tute. Was impressed that I managed to get mine going first time.
I’ll admit that configuring to run a localhost has always been a mystery to me, although I’ve had them working in the past. After this tutorial it makes a bit more sense now. Just a bit, mind, but it’s working a treat!
Hello. Everything was very clear and I performed all the steps but the last one. There is no way I can save changes to myusername.conf even thou I have set permissions to myself to read and write.
What can I do? I tried saving as on the desktop and dragging it to the folder… which worked but then my pro didn’t recognize my username and had to reinstall Snow Leopard after calling AppleCare.
Also, after reinstalling SL my user name came with a number 1 in front of it… like this: “Username 1″ but on the Web Sharing it comes like “http://10.0.1.200/~Username/” without the number 1. Does that affect the above configuration when adding the directories to the hosts file?
Hi, very good tutorial.
Unfortunately I cannot rename the php.ini.default to php.ini because I’m not authorized to write into the /etc directory…
Any hint about this, please?
Great tutorial.. Its working great.. Im first time Mac user and full convert..
Thanks again for the great article.
Hi All,
I dont have experience in Mac OS . I am facing some wired problem . In my Mac os i have apache installed in directory /private/etc/apache2 i want to configure virtual host but in my machine there is no folder /private/etc/apache2/extra i have serached for httpd-vhost.conf file , but there is no such file . Even in httpd.conf file there is no line which include httpd-vhost.conf file .
Version of apache installed – Apacahe 2.2.11
Version of MAC OS – 10.6
Please advise on this issue
Thanks
Vandana
Hi thanks for the tutorial.
When I log into phpMyAdmin, I get “Cannot load mcrypt extension. Please check your PHP configuration”. Any ideas?
Thanks.
@Matteo,
open the Terminal application found in Application/Utilities folder. Reach your php.ini.default file by changing directory to its location and then type the following :
>sudo cp php.ini.defaul php.ini
password: {put your own admin password}
sudo command is going to run the Copy (cp) command after you provide the administrative password of you account (login account). Your login account need to be set with administrative privileges which is usually the case unless someone else is configuring your computer.
I truly hate having to run out and get yet another app for every little thing on Mac, considering HTTP services on the PC side are very easy and clean. But if you want the fast way around all write permission problems with the files in this set up process, go get TextWrangler.
It runs the necessary terminal commands on the fly to let you edit these files locked down by the “root” (the REAL administrative account in a Mac). The most you’ll have to do is answer a few dialogs and use your login password. It will save you minutes or even hours of struggling with editing these text files. And its line count and various go-to functions will also speed up your process. The application is FREE and very well constructed.
Kev: Thank you for a rather painless tutorial! I’ve got a virtual host running on my Macbook Pro, along with PHP!
Wow! tnx for all the great tips… I was about tog go back to Xampp or MAMP
thanks
Hey this worked perfectly. Thanks for the tutorial.
Nice post ! Thanks a lot !!
excellent for new mac user!
Thanks for the advice, but it doesn’t work for me. Can anyone explain this .htaccess error:
[Fri Jun 04 11:38:51 2010] [alert] [client 99.250.28.33] /Library/WebServer/Documents/.htaccess: Invalid command ‘\xef\xbb\xbfAuthType’, perhaps misspelled or defined by a module not included in the server configuration
I had a major problem getting Drupal 6.17 to see my MySQL database during setup, I finally determined that the file …somesite.dev/sites/default/settings.php contains the following line
$db_url = ‘mysql://username:password@localhost/databasename’;
The line above needs to be modified, e.g.
$db_url = ‘mysql://mrwonderful:teddybearsarescary@localhost/drupaldatabaseofdoom’;
I tried something similar with a Drupal 7 (alpha) settings.php file (it’s a little tricky since Drupal 7 appears to use an array rather than a string to hold this info) but I got an error upon refresh that included the line
No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)
which indicates that the problems that I’ve had and that others have reported elsewhere are caused by the fact that the OS X .dmg from MySQL puts mysql.sock in a different directory than var.
Thanks. Good info. That said, your title says ‘set up Apache in OSX’ but the article is actually assuming you already have Apache up and running in OSX.
Thanks for the information! But when I’m in Terminal trying to set the MySQL password it gives me the error ‘/usr/local/mysql/bin/mysqladmin: connect to server at ‘localhost’ failed
error: ‘Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2) ‘. I’ve changed the path as you stated.
I’m also having a problem accessing the PhpMyAdmin folder – http://localhost/~myusername/phpmyadmin .
I can get to my test file if I just type in http://localhost/~myusername/test.php, but nothing inside the folder will show up..not even if I move the test.php into the phpmyadmin folder. It seems like a permissions problem, but I believe I’ve set them appropriately.
Any suggestions are much appreciated!
Mac OS X 10.6, PHP and MySQL » Wolfgang Reutz's Blog said on February 10, 2011 @ 13:15
[...] Chapman has a good documentation about how Setting up Apache on Snow Leopard. It’s worth a read and has all you may need to sucessfully setup apache on Mac OS X 10.6.x [...]
New to mac. Very new. Just installed XAMPP (coz it’s what I always do with windows). It was a one-click install for php, mysql and apache. So why go this route?
PLEEEAAASSEE. HELP! I used the administrator account and tried using textwrangler and it wouldnt let save the php.ini.default as php.ini it said:
This operation could not be completed, because an error occured.
You do not have sufficient privileges to perform this operation (MacOS Error code: -5000)
Mac OS X 10.6.6
In terminal type:
su vi /etc/apache2/httpd.conf
You’ll be asked to enter your admin pass and can save the file after the required changes have been made.
Hello,
I’m trying to block the access to certain files with a .htaccess file and a .htpasswd file. I changed all the setting in the httpd.conf file (allowoverride set to all), and username.conf file but i get an Internal Server error.
Anyone knows how to fix this ?
Hi, for some reason when i try to enter the mysqladmin, in the bin folder, it cant be found
“-bash: mysqladmin: command not found”
did i miss anything??
I tried to get clean urls so many ineffective ways and finally got it thanks to your last box:
Options Indexes MultiViews FollowSymLinks
Thank you thank you thank you
I followed all given hints, but as soon as i activated virtual host in httpd.conf (line 461), I can’t access http://localhost/ no more, nor phpmyadmin. I get the message 403 forbidden: You don’t have permission to access…
I’m a little desperate
and grateful for help!
Hi Tanner. Try checking your config for errors. Terminal: apachectl configtest. Also make sure that you still have: 127.0.0.1 localhost in your hosts file (it should be the first line after the #comments).
Hi Kev. Quite simply, you are a legend. I’ve just wiped clean and re-installed my entire system, which has been made a whole lot easier by following your step-by-step guide. Nice one
I don’t think localhost and 127.0.0.1 will make any differences. The problem of connection is mainly the location of mysql.sock if mysql shell is working and port number has not been changed.
Once I corrected the mysql.sock path in php.ini, restart apache should make apache+php+mysql working
Hi Kev,
Your info has been really helpful. HOWEVER, I am having issues! I set everything up as you described for my Sites folder. In my sites folder I have /sites-name/ and then inside that a WordPress site. The homepage works great, but all other pages and posts give me a 500 Internal Server Error. I’ve noticed when I turn AllowOverride back to “None” I then get a 404 error instead.
I also have another site in progress in another folder under Sites – haven’t found this problem so far.
Any ideas what is causing this?! Is it WordPress? There is no .htaccess file in my WordPress folders, not sure whether that makes a difference.
Thanks
Thank you very much for putting this together. I’ve been working on getting rid of MAMP and moving everything over to the native OSX. It was confusing because with MAMP I had two PHP’s and two MySQL’s installed. It was tricky to get everything straight. Your article was the most helpful, accurate and clearly-written of anything I’ve seen on the web in the last couple of days.
RSS feed for comments on this post. TrackBack URL
Leave a comment