-
Jun 11, 2008
Local Development with MAMP
I've recently been playing around with MAMP and setting up all the sites I work on locally. I also wanted to test htaccess modrewrite (post on this to come) but am not ready to pony up for MAMP Pro at this time for the easy Virtual Host support. I accomplished this with the help of Eli Horne and he had the same idea about writing it up (which coincidentally went up soon after I posted a tweet about prepping this post). Eli's post is pretty straight forward for the tech savvy, but I wanted something simpler but also more obvious of how to set up multiple local development sites, which results in lots of screen shots and a much longer post. I feel the two posts compliment each other.
As Eli stated, the instructions may vary, given your particular setup. I'm running 10.4.11 on an Intel Mac.
Maybe I went overboard. Maybe someone will find it more helpful since they may not be comfortable using Terminal. Anyway, on to the step by step.
Go to MAMP.info and download the application and install it in your Applications directory.
Mount the DMG file and drag MAMP into your Applications folder.
htdocs folder is where PHP will run
Create a folder in MAMPs htdoc folder with where you would like you're files for you new local development site. Here I chose have_wordpress.dev, since I was in the process of installing wordpress for some testing. I am using .dev, as it is not a real TLD.
I have previously set up a localhost.dev alias for the root of the htdocs folder which you will see though-out the post.
Open MAMP and set your preferences as follows:
Apache Port: 80
mySQL Port: 3306
Start Page URL: /MAMP/
PHP 5
Document Root: /Applications/MAMP/htdocs
MAMP Widget
Be sure to install the MAMP Widget, for easy starting and stopping of your new local server!
Using Terminal to show hidden files
I prefer to alway show hidden files since it makes easier when editing .htaccess files for sites locally. Some people aren't comfortable with this so I'll post at the end how to re-hide the files in case you fall into this audience.
Some people are also not too comfortable using Terminal, but I promise, this will be easy.
Open Terminal at the following location:
Applications/Utilities/TerminalShow hidden files
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder
click return.You can hide them again when you're done, but I prefer to keep them visible, as it makes it easier to work with locally backed up .htaccess files for this multiple site development set-up.
Editing host file to create multiple local domain aliases
Now that hidden files are displayed, you can open up you favorite text editor. I use BBEdit, but there are definitely other options out there. (This file will ask you unlock the file. It's fine.)
Below is what my host file looks like, as I have one .dev domain for the root of the htdocs folder, and one for our new .dev site for our upcoming wordpress install.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost localhost.dev
127.0.0.1 localhost have_wordpress.dev
255.255.255.255 broadcasthost
::1 localhostThis is one of two spots you have to edit in order to map separate directories to different fake domain names.
There's no place like 127.0.0.1.
You will be prompted for your password to save this file
You'll need Admin privileges to do all this.
Next up, is editing the httpd.conf file
We now have to create a reference in the Apache httpd.conf file to tell the new Virtual Host where the files will be on your system.
httpd.conf file
In order to have multiple local development sites running simultaneously, you will have to map different directories to different folders within MAMPs htdocs folder.
Once you open the httpd.conf file, scroll all the way to the bottom and you will see the following:
#
# Use name-based virtual hosting.
#
Just after that you want to insert the following code:NameVirtualHost *
<VirtualHost *>
DocumentRoot /Applications/MAMP/htdocs
ServerName localhost.dev
</VirtualHost>
<VirtualHost *>
DocumentRoot /Applications/MAMP/htdocs/have_wordpress.dev
ServerName have_wordpress.dev
</VirtualHost>
The Virtual Host tags are what you can create to map the document root to the Servername you created locally.Once again, your password is needed to edit this file.
Now you can start the server by using the widget I mentioned earlier, and call up the htdocs index file in a browser by typing http://localhost/ or in my case, localhost.dev.
Mamp start page
The MAMP start page links to MAMPs install of PHPMyAdmin, which we will use to set up new databases later. The Start page explains MAMP in more detail and is linked from the widget.
It does set a root user with a password or root for accessing your local database, but I prefer to set up the identical users taht we will be creating for the live site, so as not to have to go back to your config files to change the db user and password.
I can also access my new directory Applications/MAMP/htdocs/have_wordpress.dev by going to have_wordpress.dev. We haven't created an index file in there yet, so lets make that next, and make a hello world script to make sure everything is working.
Create a new html document
This is the BBEdit New html document window.
Our new html document.
Create Hello World Script
We could easily just echo out "Hello World", but since I've already gone overboard, I'll write an unnecessary class to return hello world on our page.
<?php class have_world
{
function whos_world_is_this()
{
$its_yours = "The world is yours!";
return $its_yours;
}
} ?>We can then place this in the body of the page:
<?php $the_world = new have_world();
echo $the_world->whos_world_is_this(); ?>Save your index.php file
Our new index.php files location
Applications/MAMP/htdocs/have_wordpress.dev
Hello World on your new local development site
It's Mine, It's Mine, It's Mine. Who's World is This?
And finally, hide your hidden files if you wish
Fire up terminal and run the following commands:
defaults write com.apple.finder AppleShowAllFiles FALSE
killall FinderIn case you aren't comfortable displaying your hidden files all the time, you can hide the again with Terminal using the command above.
That about does it!
So, that wasn't so bad, now was it? Follow-up posts include ModRewrite with .htaccess, and setting up your database and user for your new Virtual Host that you can now run locally. I currently have about 22 sites backed up and running locally. Follow these steps whenever you want to set up a new local site.
Posted by Haveboard on 06/11/2008 4:14 PM in Awesome, Geekery
Comments
Eli
06/11/2008
nicely done! that is extensive.
Sean
06/11/2008
I agree, good post. Thinking about dropping this on my home machine and getting my hands a little dirty. :)
Eli
06/12/2008
hahah, that will teach you to tweet about your works in progress. SUCKER!
Mark
08/04/2008
I would also change this line in your php.ini: memory_limit = 8M; to memory_limit 90M; or something a little higher than 8M because it can sometimes make your pages not load correctly.