How to setup your Mac web development environment

Following my accident a couple of weeks ago, I’ve now been blessed with a new MacBook Pro which needs setting up again.

So, I thought I’d document setting up MySQL, PHP5 and Apache with multiple virtual hosts.

Tasks

  1. Install PHP5
  2. Install MySQL
  3. Configure MySQL
  4. Configure Apache with virtual hosts

Notes

  • I’m not upgrading Apache to version 2 – I don’t see the point since 1.3x is perfectly good.
  • I prefer to use PHP5 because I started making more use of the XML functions (though I’ve never coded in PHP4, I’ve been told to watch out for the ‘<? ?>’ vs. ‘<?php ?>’).
  • I am assuming you can open Terminal and can cut and paste.

Quick Disclaimer

I’m going to document my installation. Make sure you backup where ever necessary, and please don’t blame me if it all goes to hell. Don’t get me wrong, if you drop a comment, I’ll be more than happy to help where I can, but backup, backup and RTFM.

Step 1 – installations

PHP

Download PHP5 from Marc Liyanage’s PHP web site and follow the simple install instructions.

Note that Marc explicitly states to NOT USE stuffit expander, and rather use Apple’s BOMArchiveHelper or the command line.

Via the command line (via Terminal or your preferred app), if the file is still compressed – i.e. ends with .gz:

tar -zxvf entropy-php-5.x.tar.gz

Or if it was uncompressed through Safari’s download:

tar -xvf entropy-php-5.x.tar

Install the package that you just untarred, and test PHP5 is fully installed (following Marc’s test.php instructions).

MySQL

Download MySQL – I’m assuming since you’re setting up a development environment on your Mac, you’re a confident user – so I’m choosing the MySQL Community Server.

Select the right Mac OS X MySQL installation that suits your machine (Intel based machines are the x86 installs). I chose to install the ‘standard’ install because it suits my needs.

Note that you don’t particularly have to put in the details on the download page, you can skip it. I did because I’ve filled it in before, and letting them know how you plan to use their product may actually contribute to a better MySQL.

Double click the MySQL package (rather than the startup packge), and install away.

If you want MySQL to start automatically, install the startup package that also came in the .dmg.

Note the following from the MySQL readme.txt:

After the installation, you can start up MySQL by running the following commands in a terminal window. You must have administrator privileges to perform this task.

If you have installed the Startup Item, use this command:

shell> sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
(ENTER YOUR PASSWORD, IF NECESSARY)
(PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL)

If you don’t use the Startup Item, enter the following command sequence:

shell> cd /usr/local/mysql
shell> sudo ./bin/mysqld_safe
(ENTER YOUR PASSWORD, IF NECESSARY)
(PRESS CONTROL-Z)
shell> bg
(PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL)

Give it a little test:

$ /usr/local/mysql/bin/mysql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)

A quick break for some useless knowledge…

I was once told by a MySQL engineer that MySQL was named after the creator’s daughter (I say creator because it was in a pub and I can’t remember who he said!), and being Swedish she was called ‘My’, pronounced: ‘Mee’.

So, MySQL, is supposed to be pronounced: Mee-S-Q-L.

Ah…back to work.

Step 2 – configure

MySQL

Let’s add MySQL to our path so we can just type ‘mysql’ from the prompt.

Edit (or create) .profile (that’s prefixed with a dot) in your home directory, and add the following line:

export PATH=$PATH:/usr/local/mysql/bin

If you’re not sure, from the command line run this command (it will add the appropriate line):

printf "\nexport PATH=\$PATH:/usr/local/mysql/bin" >> ~/.profile

If you open a new Terminal session (or run ‘source ~/.profile’), you should now be able to run MySQL from the prompt (i.e. just type ‘mysql’).

Note that if you want password protect the administration of mysql, you will need to enter the following command (here NEWPASSWORD is your own password):

sudo mysqladmin password NEWPASSWORD

I don’t personally both, because it’s an offline environment and secured on my laptop.

Apache and multiple offline domains

Personally, since I work on multiple websites, so I have to set up offline versions, such as: http://apple.dev (why no www?) (and then at a later date upload to the production web site…though thankfully not apple.com!)

1. Set up directories

In your home directory you will find a ‘Sites’ folder.

Create a new directory, in my case I’m creating ‘apple.dev’. Inside of that directory I am adding one further directory: htdocs.

I am also going to create a logs folder in /var/logs/httpd/ called apple.dev. This is so I keep all my logs in a consistent directory (and I can use the console app to view them at a later date if I please). You can do this from the command line using this command:

sudo mkdir /var/logs/httpd/apple.dev

Now I create a symbolic link to the logs directory (assuming I am in the /Users/USERNAME/Sites/apple.dev directory):

ln -sf /var/logs/httpd/apple.dev logs

My directory structure now looks like this:

ls -ltr /Users/remy/Sites/apple.dev
total 8
drwxr-xr-x   2 remy  remy  68 Jan  6 15:04 htdocs
lrwxr-xr-x   1 remy  remy  24 Jan  6 15:05 logs -> /var/log/httpd/apple.dev

2. Add a host entry

Edit /etc/hosts with your favourite text editor and add the following (obviously replacing apple.dev with your own offline domain):

127.0.0.1 apple.dev

You can use TextEdit to make the changes, but you probably won’t be able to navigate to the /etc/ directory. In which case, press command + shift + g and enter ‘/etc/’, then you will be able to pick the file out to edit.

You’ll be prompted for the password to change protected files, go ahead and stick it in.

Depending on whether your domain exists in the real world you may have to run the following command from Terminal (though it won’t hurt if you run it regardless):

lookupd -flushcache

This will clear the DNS cache on your machine, meaning that the DNS lookup daemon process will re-read /etc/hosts for the domain in question.

3. Add virtual host entry

Again in your editor, you will need to edit /private/etc/httpd/users/${USER}.conf (in my case the file is called remy.conf).

In this file, add the following (changing USERNAME to your home directory username)

NameVirtualHost *:80
<VirtualHost *:80>
  DocumentRoot /Users/USERNAME/Sites/apple.dev/htdocs
  ServerName apple.dev
  ErrorLog /Users/USERNAME/Sites/apple.dev/logs/error_log
  CustomLog /Users/USERNAME/Sites/apple.dev/logs/access_log common
</VirtualHost>

Note that if you add further domains, you only need the line ‘NameVirtualHost *:80′ once.

4. Restart Apache and test

Now everything is place, we should be able to restart and test. First things first, test our config changes:

sudo /usr/sbin/apachectl configtest

This should say everything is fine. Now for a restart.

sudo /usr/sbin/apachectl restart

If this doesn’t work, do a ‘apachectl stop’, then ‘apachectl start’.

Now in your browser go to your offline dev web site, in my case: http://apple.dev. You should see an empty directory.

Drop in a dummy index file to truly test the pages are being served up. You should find you have no problems (well…I didn’t!).

Finishing up

So, there you have it. You should now have a multi-domain offline development environment.

You can start putting PHP files in the htdocs directory and connecting MySQL and all that jazz.

Please let me know if there were any glaring errors or if you found this tutorial super useful!

38 Responses to “How to setup your Mac web development environment”

  1. Interesting post – and relevant to me. I am currently putting money aside to switch camps at home from Windows to Mac, and will be looking at this kind of thing (although I hear VMWare are working on a Mac version, so may end up using Linux in a virtual machine for dev work).

    Great blog btw.

  2. Blog Author remy January 6th, 2007 at 6:24 pm

    @Jonathan – I didn’t know VMWare were up to that – equally, it looks like they’re producing a similar app to Parallels (which useful for Win IE testing).

    @Dane, well that knocked the wind out my sails! :-) Though I’m always keen to know what’s going on under the bonnet, but MAMP does look interesting.

    Also – I just remembered if you want .htaccess to work (in particular for mod_rewrite), you’ll need to edit the /private/etc/httpd/users/${USER}.conf file and change:

    AllowOverride none

    to:

    AllowOverride all

  3. I’ll Agree with Dane here – MAMP is easy as hell to install, there’s also xampp –

    http://www.living-e.de/en/products/The-MAMP/download/download.php

    http://www.apachefriends.org/en/xampp-macosx.html

    • Liam
  4. It might just be me, but my Firefox is not displaying the above text inside of <code> tags…works fine in Safari though :/

  5. Hmmmmmm ok that should read, not displaying inside <.code> tags

  6. Hi Remy,

    Excellent tutorial got it all working fine and this is just what I have been looking for! :)

  7. Great tutorial. I’m new to modifying apache files… so this was very friendly.

    Does anyone know how would one access this local site from another computer on the same local network?

  8. @Steve – it’s pretty easy (or the quick and dirty way is!):

    Say you have computer_a is your local site and you want to access it from computer_b.

    Add the local site’s domain to /etc/hosts on computer_b, e.g.

    computer_a’s IP on my network is 192.168.0.10, so I add this to computer_b’s /etc/hosts file:

    192.168.0.10 mygreatsite.dev

    That’s it. Safari (or your browser of choice) will now direct requests to the machine hosting your offline copy of the web site.

    Hope that helps.

  9. Fantastic! Thanks for the quick reply; it worked like a charm.

  10. Should be: sudo mkdir /var/logs/httpd/apple.dev, no?

  11. [...] Lacking a lot of online resources, here’s a good step-by-step guide to setting up your Mac (version 10.4.x) to a good LAMP dev enviroment on your Mac. [...]

  12. I am incurring a glitch.

    When I activate the .htaccess capability with

    AllowOverride all

    it causes a 403 error when accessing my test.site.com

    When I revert back to

    AllowOverride none

    Everythine works fine and able to access test.site.com

    I use rewirte rules in my site, and need the .htaccess capability.

    Advice.

    David

  13. @D Padilla, this is what I have in my /private/etc/httpd/users/remy.conf

    <Directory "/Users/remy/Sites/">
    Options Indexes MultiViews
    AllowOverride all
    Order allow,deny
    Allow from all
    </Directory>
    NameVirtualHost *:80
    <VirtualHost *:80>
    DocumentRoot /Users/remy/Sites/test.com/htdocs
    ServerName offline.test.com
    ErrorLog /Users/remy/Sites/test.com/logs/error_log
    CustomLog /Users/remy/Sites/test.com/logs/access_log common
    </VirtualHost>

  14. this was really helpful. thank you so much.

  15. hi remy,

    i had a problem with adding the virtual host entry… i’m new-er at this, but can confirm that my apache, mysql, and php are installed correctly. everything went through the configtest correctly, but got the error:

    /usr/sbin/apachectl restart: httpd not running, trying to start
    Processing config directory: /private/etc/httpd/users/*.conf
    Processing config file: /private/etc/httpd/users/ entropy-php.conf
    Processing config file: /private/etc/httpd/users/leah.conf
    /usr/sbin/apachectl restart: httpd could not be started

    i would really love to use the shortcut… any thoughts?

  16. @Leah – I’ve seen this problem when the log directories aren’t set up correctly.

    For example – the line in the virtual hosts:

    CustomLog /Users/remy/Sites/test.com/logs/access_log

    I usually forget to create the logs directory in the test.com directory – or in my particular case, create the directory in /var/httpd/logs/test.com and symlink it in the test.com directory – so it looks like this:

    test.com $ ls -l
    total 8
    drwxr-xr-x 57 remy remy 1938 Mar 26 16:22 htdocs
    lrwxr-xr-x 1 remy remy 23 Jan 8 08:57 logs -> /var/log/httpd/test.com

    If that folder isn’t set up – the config test passes, but it can’t start apache. You may have a similar problem – or that apache can’t read from a particular directory.

  17. yup that did it!

  18. [...] 1. This guy presents dead-on tutorial on how to install Apache, MySQL, & PHP. [...]

  19. Rather than creating loads of entries in your httpd.conf, you can setup a local name server on your mac, this adds the benefit of being able to access it from outside machines, like Steve asked for. Good tutorial here:

    http://blog.georgesteel.net/2006/10/23/setting-up-a-local-nameserver-on-mac-os-x-104x/

    Then all you have to do is add the record to your virtualhost file (normally httpd.conf)

  20. @Mike – thanks for the link – that’s definitely better than having to manually add each host entry in to the remote machines.

    However, the amount of entries needed in httpd.conf or the virtualhost file remains the same, doesn’t it?

  21. [...] post on Configuring Apache with virtual hosts, for in my case wordpress theme editing! Thanks [...]

  22. Thanks for this. Your mention about the /etc/hosts file is really important and helped me a lot.

  23. [...] my how to setup your mac web development environment, and today moving to Leopard, my entire offline environment is broken – this is entirely due to the [...]

  24. to set the mysql variable for 10.5 in .profile issue the following command:

    echo ‘export PATH=$PATH:/usr/local/mysql/bin’ >> ~/.profile

  25. I stopped following the instructions after MySQL was configured. However, I can’t connect to my database…what are the login strings you need for Mac os X? Are we connecting to localhost…but what username and password is required?

  26. @Jack – username: root, password blank. To connect try:

    mysql -uroot

    It will connect to the local host by default.

  27. Remy – this is terrific. My only question: if I have the Apache server running, how would I not have “/private/etc/httpd/” directory?

    I’ve looked at a bunch of spots on the Intertube, and none of them say “Don’t see httpd, do x” so I think I must be doing something wrong.

    In any case, nice work, Remy – and great ideas/links in the comments, people – thanks!

  28. we moved over to mac apache php5 and can’t get our forms to work now. we had php5 on a windows server before and everything was fine. We get Notice: Undefined variable:
    Could it be settings in php? I know the script worked before…thanks for your help.

  29. I get an error when I try to use sudo mkdir /var/logs/httpd/sitename. Is this a directory that is already there or is it a directory that needs to be created? The error is as follows mkdir: /var/logs/httpd: No such file or directory

  30. @OD – it’s probably because the /var/log/httpd doesn’t exist. A simple work around for this is actually put the logs directory in /Users/USERNAME/Sites/mysite.com/logs.

    This gets round the whole permissions issue. It means the logs aren’t all in one place, but then again, who honestly looks at all the longs at once?

  31. So what if I try to access my /etc/my.cnf and my permission is denied? How do I go about that? I’ve got a nice little “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)” when I try to type in /usr/local/mysql/bin/mysql

    So I tried to access my /etc/my.cnf file and my permission is denied. What gives?!?!?!!!

  32. [...] created to make adding a new local domain easier. Before you get too deep it may be worth reading Remy’s blog which was the inpiration behind this script. My installation is slightly different as I’m [...]

  33. Hey Remy

    Great article. I’d be interested in your opinion on development tools and these seems a good place to ask. What tools (ff plugins, IDEs, utilities, etc.) do you use on a regular basis when doing web dev (php, javascript, especially jQuery, CSS, XHTML)? I watched a screen cast you did a while back for a tutorial and I didn’t recognize the IDE you were in so I thought I would ask. Are you a fan of open source or products like Adobe Dreamweaver?

    Thanks

  34. The name “My” in swedish is prononced “Mooi” in english… FYI :)

  35. I am using MAMP and learning some lessons on PHP with my MAC texteditor. Everything was working fine when all of a sudden my PHP code was no longer reading after I saved and tried to view it with my browser. Any suggestions would be aprreciative cause I’m at a loss. The .PHP files created from my text editor were once working (functioning on my browser) and then it stopped reading any files. I went in to try change some preferrences but no such luck. Thanks for any help…

    Robert

  36. My is definitely not “Mooo”, nor is it “Mee”. The “y” is pronounced just like “y” in “yes”. Kinda like “Myyyes!”, minus the “es” of course.

  37. Great tutorial. A couple of fixes to paths (apple might have changed these, I ‘m using Snow Leopard Mac OS X 10.6.2)

    “sudo mkdir /var/logs/httpd/apple.dev” should be “sudo mkdir /var/log/httpd/apple.dev”

    “/private/etc/httpd/users/${USER}.conf” should be “/private/etc/apache2/users/${USER}.conf”

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight