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)
@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?
[...] post on Configuring Apache with virtual hosts, for in my case wordpress theme editing! Thanks [...]
Thanks for this. Your mention about the /etc/hosts file is really important and helped me a lot.
[...] 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 [...]
to set the mysql variable for 10.5 in .profile issue the following command:
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.profile
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?
@Jack - username: root, password blank. To connect try:
mysql -uroot
It will connect to the local host by default.
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!
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.
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
@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?
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?!?!?!!!
[...] 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 [...]
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
The name "My" in swedish is prononced "Mooi" in english... FYI
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
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.
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"