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
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!
You should follow me on Twitter here I'll tweet about JavaScript, HTML 5 and other such gems (amongst usual tweet-splurges)
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.
Or the quick way - http://www.living-e.de/en/products/The-MAMP/download/download.php
@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 noneto:
AllowOverride allI'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
It might just be me, but my Firefox is not displaying the above text inside of <code> tags...works fine in Safari though :/
Hmmmmmm ok that should read, not displaying inside <.code> tags
Hi Remy,
Excellent tutorial got it all working fine and this is just what I have been looking for!
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?
@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.
Fantastic! Thanks for the quick reply; it worked like a charm.
Should be: sudo mkdir /var/logs/httpd/apple.dev, no?
[...] 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. [...]
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
@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>
this was really helpful. thank you so much.
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?
@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_logI 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 -ltotal 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.
yup that did it!
[...] 1. This guy presents dead-on tutorial on how to install Apache, MySQL, & PHP. [...]
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)