Geek & Mild by Sean Sperte


POWERED by FUSION

December 7, 2008 Setting Up a Killer, Local Web Development Environment on a Mac With MAMP and VirtualHostX

With a keen sense of internet awareness (Powered by Twitter™), I’ve observed a large amount of Switchers whose day job is to build the web. Because of this, I thought I’d provide a bit of a welcome gift, in the form of a small tutorial on setting up a killer work environment.

The setup I’m going to describe isn’t novel, nor is it for everyone. My workflow allows me to have a great deal of flexibility with how I develop (since I’m basically a one-man-department), so keep that in mind as you read. You may need to “season to taste”, so to speak.

Let’s get started.

MAMP

The heart and soul of the environment we’re going to build comes from MAMP — Mac, Apache, MySQL, PHP. The free version of MAMP contains everything most developers need. Simply download, install and run it as you would any normal application. We’re going to make a couple of easy changes in the MAMP preferences.

The first is to change the ports that MAMP uses. Click on the “Ports” tab and set the Apache and MySQL ports to their defaults (80 and 3306, respectively). You’ll be prompted for your admin password.

MAMP port settings

Secondly, I like to uncheck all of the options in the “Start/Stop” section. This comes with some potential performance and security hits since it means you’ll have to manually stop the servers if/when you’re not using them.

Don’t worry about any other options. We’re going to circumvent MAMP’s default document root with virtual hosts.

VirtualHostX

vhx-icon2.jpgNow that we’re serving Apache+PHP, we can access whatever document root we have setup in the MAMP preference via a web browser with the URL http://localhost — but that sucks. Instead, we’re going to use virtual hosts, allowing any URL to any document root we want. That’s right. Any URL to any directory.

To do this, we can edit the Apache httpd.conf file manually, or use Tyler Hall’s fabulous VirtualHostX, a $19 utility that provides a clean and simple GUI for adding virtual hosts.

On first launch, VHX will ask you to create a backup of the current configuration — which you should do. It also provides an option for installing MAMP support.

Creating a virtual host is simple:

  • Enter the domain name you’d like to use
  • Choose a “website folder” to be the document root for the domain
  • Apply changes

vhx2.jpg

That’s it! (You can add advanced, custom directives if you’re into that sort of thing.)

One quick note: anytime you update the virtual hosts you should restart Apache (httpd) by stopping and starting the servers in VirtualHostX (command+shift+R).

VMware Fusion

At this point we have a near-perfect local development environment — except for testing. Sure this works great for testing in all the browsers you have available on your Mac (just type in the domain you chose in VHX in any browser you have installed). But what about testing how your sites look on a PC?

That’s where VMware Fusion (or another virtualization program) comes into play. Most of us (developers) already use virtualization in some form for testing, but in order to take advantage of this particular environment — MAMP with virtual hosts — we need to update the hosts file on the virtual machine so that it resolves the domains to the Mac’s IP.

One catch: if you’re on a latop, chances are the Mac’s IP will change often. That’s why we need to get the secret IP the VM uses to talk to the Mac. (Yeah, I didn’t know such a thing existed either.) Here’s how we find it:

Type ifconfig vmnet1 into a Terminal window. You should get a return like this:

vmnet1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    inet 192.168.115.1 netmask 0xffffff00 broadcast 192.168.115.255
    ether 00:50:56:c0:00:01

The “inet” number is your “secret” IP (in my case, 192.168.115.1).

Now that we have that number, we can edit the hosts file on the VM. We find it in: C:/WINDOWS/system32/drivers/etc. Just open the host file with Notepad, and add each virtual host (domain) on it’s own line at the end of the document, like so:

192.168.115.1  dev.triumphofgrace.com

… and save. Afterwards refresh the VM’s DNS cache by typing ipconfig /flushdns in a command line window.

(Do this for each virtual host you added in VHX.)

Voila! Now you should be able to access the custom virtual hosts you created in VHX on your PC using that wonderful browser we all love, Internet Explorer.

Recap

So here’s what we did, in a nutshell:

  1. Download and launch MAMP
  2. Change default ports MAMP uses
  3. Download and launch VirtualHostX
  4. Backup settings (using VHX’s startup guide)
  5. Add the virtual hosts we want to use
  6. Restart Apache
  7. Add any virtual hosts we created in VHX to the PC hosts file
  8. Rock on!

There you have it. A killer, local development platform on your Mac. Once you have everything setup, you can now install and run your choice CMS (such as ExpressionEngine) right on your Mac. How cool is that?

Go forth and make the web a better place.


Comments

Sweet! Thanks for posting this! I think this will come in handy, for sure. Always looking for ways to speed up the work flow.

I wish someone would do this for the PC as well. Thanks again!

Spencer Barfuss
December 7, 2008

Sean.

You just rocked my world. I had about 95% of this, but the vmware secret IP just shaved a solid 30 minutes of ifconfig checking out of my work week.

I owe you an insane number of beers.

eli
December 7, 2008

Yo Sean! Well done sir. I’ve always just typically manually edited the host and conf files needed, but I’ll check out Virtual Host X.

Can you explain why you change the Apache and MySQL ports? I’ve never done and and it’s been no big deal for me.

Also, regarding changing the windows host file: for those who fear the terminal, you can just give it your local IP address (find it in the network preference pane). But know you’ll have to change it if you move to a new network or get a new IP address for any reason. As for me, I’m going to do that ifconfig terminal trick, I didn’t know about that before. Good article bro!

Brian Warren
December 7, 2008

Can you explain why you change the Apache and MySQL ports? I’ve never done and and it’s been no big deal for me.

Good question Brian. The main reason I set to default Apache and MySQL ports is so I don’t have to specify ports in certain applications. Particularly, it helps to use the default port for MySQL.

Another reason is that I’ve found MAMP to be finicky when dealing with virtual hosts that specify a port different than the default set in httpd.conf.

And I guess the lamest reason I use the regular ports is so I don’t have to type “localhost:8000/MAMP” in the browser when I want to get to the MAMP start page. :)

Sean
December 7, 2008

I realize it’s a slightly more expensive solution, but have you looked at MAMP Pro? It seems to combine the regular functionality of MAMP with VHX’s virtual host editing prowess. I’m a fan of having to use as few apps as possible, but I’d love to hear reasons to do it this way. Not sure how well MAMP Pro ties into VMWare, however.

On another note, anybody using Parallels instead of VMWare Fusion? Just curious if this can be set up there as well, as I’m a Parallels user. Cheers!

Jeff Byrnes
December 7, 2008

Thank you for this post. It looks like a great way to setup a local Web environment without tampering with OS X’s underpinnings. As information, VMware® is spelled with a lowercase w.

Kev Rodgers
December 8, 2008

Great write up. For those working with EE in a dev environment here is “the trick” to add to Sean’s post.

The settings in EE’s config.php will override the db settings. So keep two copies of config.php, one for the dev site, one for the live site. This greatly simplifies the process of moving between the two.

Leslie
December 8, 2008

This is perfect. Worked like a charm. Thank you so much for this post. Just one note, in case anyone ran into this, I was not able to list multiple vhosts on the same line, like so:

1.1.1.1 host1 host2 host3

When I moved them out to separate lines, it worked….like so:

1.1.1.1 host1
1.1.1.1 host2
1.1.1.1 host3

Dalton
December 8, 2008

The settings in EE’s config.php will override the db settings. So keep two copies of config.php, one for the dev site, one for the live site.

Right. Thanks for the tip. (I’ve been meaning to ask you guys if there is a good way to keep the MySQL in sync without overriding local server paths. Any ideas?)

I was not able to list multiple vhosts on the same line

Yes, I should have clarified that I had tried the same thing without success. Best to keep each virtual host on its own line.

Sean
December 8, 2008

@Leslie, could you explain in a bit more detail what you mean about overriding the db settings. Apologies, but I don’t usually do local dev, and generally work on a live EE install.

Richard Angstmann
December 9, 2008

@Richard – I don’t want to derail Sean’s thread. There is more info in the EE Wiki (search for config.php) and the EE forums. But the basic idea is to use config.php to set things independently of the db to make it easier to switch between a local dev environment to a live server.

Leslie
December 9, 2008

Apologies Sean for hijacking the comments, not something I do often, please excuse me.

Thanks Leslie, I will take a look in the places you suggest.

Richard Angstmann
December 9, 2008

I don’t consider this type of discussion hijacking at all. In fact, I welcome it so that others who have the same questions or issues can find the answers they’re looking for.

Sean
December 9, 2008

Is there a tutorial where I can use the mamp pro? Instead of getting the virtual pro and what not.

Kevin
December 16, 2008

@Kevin – Not sure if the makers of MAMP Pro have a tutorial, but the built-in help is pretty good, and it’s pretty simple to set up, honestly. Just give it a whirl!

Jeff Byrnes
December 16, 2008

Thanks for the post. I am having a few problems. When I change the port numbers on MAMP, Apache and mySQL do not restart. So I change ports back to 8888 and 8889 and they restart, create a new virtual host using VirtualHostX. But when i surf to my virtual host it just show me the root level of my Sites directory.

To see the site i have to add :8888 at the end of the url. Im new at apache but trying to learn how it all works. Thanks for any help!

abel
December 23, 2008

abel, make sure Web Sharing isn’t running (check in the Sharing preference pane in System Preferences).

Sean
December 23, 2008

Sean, I unchecked web sharing and it worked for sites not using a database. When i changed the ports to 80 and 3306, mySQL did not restart. isn’t there some kind of mysql.sock issue with Leopard? What might cause mySQL to not restart?
thanks so much!

abel
December 23, 2008

Hi Sean,

I think your post is fantastic, I am Very new to this and over the past 2 days have been scouring through forums on the net looking for a simple, easy to understand way of doing just this.

I went through all the steps up to VM Fusion (as I currently don’t have that much money to spend on the program). Only when I reconfigured MAMP as shown my Apache Server has gone from green to red.

I felt sure I may have overlooked something so I reset to original settings and I checked that web sharing was off. Still the same though.

Alex
December 30, 2008

OK, Silly me!!

My apologies Sean, problem solved, I feel like a twit, my servers were stopped. I neglected to realise the button saying “Start Servers” directly on the right. Hit that and all seems fine.

Alex
December 30, 2008

not sure if anybody resolved this yet or not, but i just hooked up MAMP Pro to VMware Fusion without using VirtualHostX. MAMP Pro creates virtual hosts in the same way as VirtualHostX.

@Jeff Byrnes – I used to use Parallels, and can imagine you can probably rig the same kind of system up as with Fusion.

@sean – thank you so much!

alex
January 8, 2009

hi,
does some1 can explain me a such phenomenon. i’ve proceeded as sean suggested. and…the second project (project2) was “canibalized” by the first one (project1). i’ve removed the project1 one from Sites folder. same thing…all i can see now is the default joomla temlate with some articles from the project1. where is my second (allmost finished) project with completely different template and look? is it possible to recover it? or this is a question of MySQL database file, which wasn’t (unfornutaley) backed-up?

robertas
January 15, 2009

hmmm… this looks exactly the right solution for me, been using MAMP and just grabbed VirtualHostX.

But… when it comes to the terminal… the command

ifconfig vmnet1

returns

ifconfig: interface vmnet1 does not exist

I did a complete ifconfig and it gave a number of options, I tried all of these, in Windows hosts file, flushing the DNS each time. None worked.

So, as a recent swticher and n00b, I’m stuck. Any ideas?

I’m using Parallels… anyone got it working with Paralleles?

Michael Rose
January 18, 2009

does this work with java?

davy
February 11, 2009

There’s a great FREE alternative to VMWare, Sun’s VirtualBox. Check out http://www.10voltmedia.com/blog/2008/12/screencast-install-internet-explorer-on-osx-using-virtualbox/

It’s awesome!

cancel bubble
March 15, 2009

This is great, thanks! I’m having two small issues though, hopefully someone has some insight:

First, I can’t seem to get this to work with a local Wordpress install. Seems to work fine if I choose a root directory anywhere else on my machine, but the Wordpress install (which was living in /Applications/MAMP/htdocs/wordpress) wont load.

Second, I’m having a lot of trouble with the VMware portion of this. Followed the directions to the letter and no luck. I just get “Cannot Find Server” errors. I do get the “mystery IP” from Terminal, as intended, and have updated the hosts file in Windows but to no avail.

Anyone else had similar issues?

Thanks in advance!

Clint
March 17, 2009

Oh… so scratch the second part of that. VMware is working now. Fwiw, if others have the same issue, make sure to (a) run the flushdns command in an actual Command Prompt and not as a Run command in the Start Menu, and (b) make sure to restart afterwards. This combo fixed the issue for me.

But, still having trouble with my local Wordpress install inside the MAMP folder. Anyone else have luck with this?

Clint
March 17, 2009

Clint, you might have to change some of your settings in wordpress to match the new urls. There may a broken path in there.

I’m actually having the same issue you were having in parallels, where IE finds the correct ip address but can’t load the page. I tried restarting, and did the refresh in the command prompt. Any other ideas?

Liz
April 3, 2009

I actually sorted out the Wordpress problem. I forgot that Wordpress stores the URL of your blog in the database and uses that as its own “root” value, so you have to go in and manually edit the blog’s URL in a few spots.

As for the IE issue, I’m using VMWare, but the two steps I noted (flushing the DNS and restarting) fixed everything for me. I’ve heard some Parallels horror stories though so it might be something to do with whether its set up to share the Network connection with your host machine or have its own. That sort of network stuff is over my head though!

Clint
April 3, 2009

no one has gotten this to work in parallels have they…

Liz
April 3, 2009

Mamp is good but no longer update since last year.
My question is, is it possible VirtualHostX act like Mamp?

Run VirtualHostX with additional others we need on Mac, as sample : – Install VirtualHostX – Install PHP – Install phpMyAdmin – Install mySql – or and install other software that we need for the development, ect. – Then RUN VirtualHostX

Will it be possible?
VirtualHostX running with local mac software installed side by side?

The reasons is, we can decided what we need to instal and or upgrade the software version with freedom when its needs..

Any advice? Thank you.

:) Maykruezy

Mayk Ruez Y
April 3, 2009

And then there was sweetness ;-)
Thanks for taking the time to post your findings. After numerous attempts to get my G4 to behave with virtual hosts (the long way) Mamp, VirtualHostX and Sean Sperte made my day… now off to mirror my live site for ongoing development!

Tim
April 22, 2009

Bitnami offers a much nicer solution. Even with Postgres for people who’ve moved on from MySQL.

Shanx
May 8, 2009

For those of you who have been asking how to use this set-up with Parallels instead of VMWare, here’s how I did it:

http://www.hyperarts.com/blog/using-mamp-and-virtualhostx-with-parallels/

Daniel
May 15, 2009

Mamp is good but no longer update since last year.
My question is, is it possible VirtualHostX act like Mamp?

Amanda
May 24, 2009

Great tutoria indeed!

Sean (and anyone else), I got a question about the local path to the directory where the websites are stored.

The VHX screenshot shows a setup like: /Users/Sean/Projects/“site”

Now, as far as I know, MAMP doesn’t allow you to have such configuration, but in order to work, you are OBLIGED to have your site folder in the MAMP folder.

As so:
/Applications/MAMP/“site”

Or is there a workaround to that, that I am not aware of?

Thanks for clearing this up to me.
;-)

Gianfranco
May 29, 2009

My question is, is it possible VirtualHostX act like Mamp?

Dissertation
June 13, 2009

I dont know what Im doing wrong but I cant get my php to run right trying to install a wordpress but all I get is php coding in my broswers

Chris
June 24, 2009

Great post! I researched this several month ago. At that time I used MAMP Pro and run into many problems which let me to set up my Mac dev environs with Entropy PHP, mySQL, Eclipse and XDebug. If anybody is interested in that info you can read about it here: http://www.theglinkacompany.com/blog

werner glinka
July 14, 2009

Now, as far as I know, MAMP doesn’t allow you to have such configuration, but in order to work, you are OBLIGED to have your site folder in the MAMP folder.

Mmo
July 14, 2009

Mamp Pro already does this, no need for virtual host app.

Lla
August 7, 2009

Install Bonjour for Windows in VMWare Fusion to get your-computer.local working. I don’t know if any of the other domains would work, but the primary .local domain works for me.

Michael
August 16, 2009

Works great for me. Virtual Host is $40 less than MAMP Pro & it works fine with MAMP. Thanks so much for this—I was going nuts trying to set this up…

Carrie Johnson
August 26, 2009

I have followed the steps but I keep getting 403 errors. This is the first time I have ever attempted setting up a development environment, so please bare with me. I am using MAMP (1.7.2) and VirtualHostX (2.0.4). Please Help!

Matt Coughlin
August 31, 2009

@Clint. I was going nuts trying to make this work with my WP installation. Should have read the comments first ;) Now everything is working fine. Great tutorial, thank you so much.

Dani
September 2, 2009

Does anyone have clear instructions about how to set the environment for Virtual Box?

If I have 2 dev sites in VirtualHostX I can only access one of the sites with when I am in Virtual Box but not the other (I enter my laptop’s ip address into the URL).

Simon
September 28, 2009

Wow, this post is incredibly old, and the comment thread seems to have been abandoned, but I’ll ask anyway.

Is there really any need to use an app to create virtual hosts? I mean, all it is doing is providing a GUI for editing the config file. Wouldn’t this be something that you could use Applescript for. I have literally zero experience in using/writing Applescript, but couldn’t you create a script that launches a text editor with the config file open, and adds a the common lines in so all you need to do is enter in the relevant information?

Unrelated: Maybe you should close comments on old posts, as getting hijacked by spam really sucks. Automating this is pretty much the only good thing about TXP in my opinion

Dean
November 17, 2009

Dean, you can do most operations on the Mac without the help of a GUI utility, but adding virtual hosts involves more than just editing the hosts file — especially if you use MAMP.

The comments will automatically be disabled after a period of time. I want to leave them open for now, though, because of potentially helpful comments/updates.

Sean
November 17, 2009

Thanks Sean, I’ve never bothered with virtual hosts before, and have only had a cursory look, and didn’t realise there was more to it than editing the hosts file.

Guess I’ll give trying to script this a miss then.

Cheers

Dean
November 17, 2009

Does all this work on snow leopard 10.6.2? Would be really useful before I go into trying to set this up.

Also, what advantage is there of using VirtualHostX over MAMP Pro?

Simon
November 18, 2009

Hi, I am hoping someone might be able to help me…

First up, Sean thank you so much for providing these instructions! I have followed them to the letter (I think) and have everything working correctly except for one tiny thing… I can’t get my wordpress sites to view properly in Fusion! I found another site that suggested using bonjour which helped a little as now I can see my sites that are in the early stages of development and not yet converted to word but the wordpress sites just display as text – it does not appear to be linking to the database information.

Any suggestions on what I might be doing wrong with this would be appreciated. I have spent hours trying to work out why this is not working. I am sure it is something silly and simple that I have overlooked.

I am using MAMP 1.8.3, virtualhostx 2.1, fusion 3 with xp and vista (just trying to get this to work on xp/ie7 at the moment). I thought I had changed the setting to bridge to try but I see that ‘somehow’ they are now back to NAT.

Lisa

lisa
December 2, 2009

Dude, You ARE the BOMB !! Thanks so much. Way cool. I will be posting a recommendation to this link on my blog.

Paul
December 15, 2009

Is there any available VirtualHostX version for Tiger(10.4) users?
Thanks!

nestor
December 22, 2009

Love this post and refer to it often… thanks!

I’ve recently moved to Snow Leopard and Fusion 3 and this isn’t working for me anymore. Any updates on this set up? Virtual Machines are running in bridged mode, hosts file on the VM is using the proper inet number, everything else is set as described and formerly worked flawlessly. Anyone?

Jason
December 30, 2009

Jason,

I happen to set up on VM Ware Version 2.0.5 (173382) by following the above and it worked great. ~ However ~
I ALSO set up my designer’s sites and we ran into the same problem as you because he has the latest upgraded VM Ware v3.
OUR SOLUTION:
The way I got it working was by NOT using the standard ports. That is to say we DID USE MAMP default PORTS of 8888 & 8889. Of course this means you will have to use the :8888 in your URL (which i do not mind).

I hope this was helpful.

Paul Leasure
December 31, 2009

Just to follow up for anybody else having problems since moving to Snow Leopard. I have found a way to access my sites without reverting to the MAMP ports. This allows me to keep my clean name-based virtual hosts while browser testing in virtual machines—because I just can’t stand using port numbers for some reason!

What is working for me (today at least) is rather than using the inet IP in the hosts file, use your machines local network IP—in my case, 10.0.x.x. This is letting me view my dev sites on the virtual machine at their assigned domain name from VirtualHostX. SO the only problem here is that I’ll probably need to update the hosts file each time I need to test as I’m assigned a different IP on the network—no big deal using Quicksilver’s Get IP script.

So, off to spend some quality time with IE again. Thanks for this continuing thread.

Jason
January 4, 2010

I had no problem getting this environment (with default ports) to work in Snow Leopard. I started from scratch, so maybe that’s why?

One note: I used the vmnet8 interface IP rather than the vmnet1. I’m not sure if that makes any difference.

Sean
January 5, 2010

Very cool. “secret” things are fun.

Do you know if there’s any equivalent to a secret IP that is created by VirtualBox? If you know or have any intuition about an equivalent or alternative process using the same setup with Virtual Box substituted for Vmware, I would be SOOOO happy to hear it.

Thanx for contributing solutions:)

Jonah Goldstein
January 13, 2010

Add a Comment

Guide:

*strong/bold*
_emphasis/italics_
bq. blockquote
"link":http://...
Numbered list: #
Unordered list: *