When developing and testing a production Magento store locally it’s easy and convenient to set up an exact replica of the production environment. This can be achieved using Apache virtual hosting and your operating system hosts file.
This post will walk you through setting up a local testing Magento install for MAMP Leopard to include a virtual hosting configuration and how to configure your operating system to send requests for your production site to your local testing environment.
This post will not cover installing Magento, copying an existing production Magento – it is assumed this will have already been done and we are focusing only on how to actually set up an Apache virtual host and the operating system hosts file. If you need help installing MAMP you can read my step by step guide to install MAMP on Leopard.
Step 1: Configure Apache for Virtual Hosting
The default MAMP Apache install does not offer pre-configured virtual hosting but it can be easily added. First thing to do is back up your existing MAMP Apache configuration files.
cd /Applications/MAMP/conf/apache/ cp httpd.conf httpd.conf.bak |
Now you need to edit the httpd.conf
file to add virtual hosting configuration. Open the file with your favourite text editor and add the following to the end:
Include /Applications/MAMP/conf/apache/sites |
This tells Apache to include another file called sites
which we obviously haven’t created yet.
We could include our virtual hosting configration in the httpd.conf file but it’ll get grubby if we start having many local sites. Likewise we could include an entire directory (say one called ‘sites-enabled’) and then have a symlink for each site within that directory pointing to a file in a directory called site-available! If you want to set up something more robust like that, you can. Before you do though, ask yourself how many production stores you’ll be running from your laptop…
Anyway back to the actual configuration. Now we create a file called sites
in the directory we told Apache it was (recall that was: /Applications/MAMP/conf/apache/)
vi /Applications/MAMP/conf/apache/sites |
And put the following configuration into the file:
NameVirtualHost *:80 <VirtualHost *:80> ServerName example.com DocumentRoot /Users/ashley/Documents/www/example.com </VirtualHost> |
Your DocumentRoot value should match up to where you have installed Magento. It helps your organization to have an entire directory dedicated to different virtual host Document Roots and to name each Document Root after the site it is the root of as in my example configuration.
You can add as many VirtualHost sections as you like in this sites file. Each one will be a new testing or development site.
After saving the sites
file you can restart MAMP and it should now support virtual hosting and have one virtual host running. If you open a browser and browse to example.com though, you will still see the production site.
This is because your local computer is still requesting DNS records for the domain name and it is still resolving the real IP address. Never fear though, we can instruct our local computer not to get the DNS record, by giving it a local name resolution through the hosts file.
Step 2: Setup your hosts file
I won’t tell you about a hosts file here, someone has already written up an excellent description of the hosts file, I’ll just explain how to add configuration for your local Apache testing virtual host.
Before going any further I’ll stress that if you set up a name exactly the same as your production server then you will end up confusing your production and testing sites at least once. If you want to avoid this and create a really clean separation then make the test environment a virtual subdomain such as test.example.com.
To proceed with the same name setup, then simple edit your /etc/hosts file as root.
sudo vi /etc/hosts |
and find the line for 127.0.0.1 then edit it to look like this:
127.0.0.1 localhost example.com |
You can add more and more sites too like:
127.0.0.1 localhost example.com anotherexample.com |
Note that all the entries need to be on one line, i.e. no line breaks.
Now when you visit example.com in your browser you will be able to see your local testing Magento installation running as a virtual host on MAMP Apache. if you can’t for some reason, let me know and I’ll help you with it.
To revert back to using DNS entries you just remove the site from the line and refresh in your browser.
I just can’t seem to get this working. No matter what I try it just gives me a server can’t be found error.
MAMP was working just fine so I’m not sure if that would be it or not. Is there any setting in the MAMP preferences I need to change?
Here’s what I have:
sites file:
NameVirtualHost *:80
ServerName localhost.com
DocumentRoot /Applications/MAMP/htdocs
hosts file:
##
# 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.com
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
I can usually figure this stuff out but this one is rather perplexing so I thought I’d leave a comment.
Thanks.
the Virtualhost brackets are in there as well but I noticed they didn’t show up in the comment.
Hi,
Thanks for commenting. For troubleshooting this configuration I’d suggest trying to first narrow it down to either MAMP or your hosts. The way to do this is to first try a very simple folder with just an index.html in it (e.g. containing ‘hello world’). You can test accessing just that simple file in the virtual host to see if it is loading. Also you can try pinging localhost.com from the terminal to confirm it is picking up the right IP address.
One other thing to confirm, have you got MAMP configured for the usual default ports as per my guide to install MAMP
Hi thanks for sharing some informations about installing Magento on mac os x. Does MAMP include all the specific PHP extensions required to run Magento ?
I’ve tried to make a local install with the native Leopard PHP install and I get errors when loading the file magento-check.php
Basically, Magento requires “pdo_mysql”, “mcrypt” and “GD” libraries that are not bundled into the default php version of Mac os x leopard.
Does install MAMP allowed you to run magento with complete faetures ?
Yes, MAMP has the required extensions, this makes it a great alternative to the standard Leopard 10.5 PHP which will require you to recompile with the extensions. I did a really basic MAMP installation tutorial which if you want to see how easy MAMP is to set up, will show you.
Hey Ashley,
Great site.. I love learning all these tricks for local development! Very cool….
I tried to implement this tutorial and it would seem something is going wrong on the MAMP end for me… I did try to be sneaky though and specify a different DocumentRoot than what was spec’d in ServerName..
NameVirtualHost *:80
ServerName dev.domain.com
DocumentRoot /Users/frosted/Sites/mag
Do these need to match up? Like…
ServerName dev.domain.com
DocumentRoot /Users/frosted/Sites/dev.domain.com
If so.. any tips on where to modify the magento paths in the DB via phpMyAdmin ? I can reinstall to a directory called dev.domain.com but would obviously need to correct those paths outside of the admin panel…
**figured you’ve done this before so thought I’d post up here[:
Peace from Vancouver BC!
Gonna go hit the slopes now!
Cheers,
J
The directories do not have to match, that is more just a convenient way to remember which folder is which domain. Otherwise you’ll be forever wondering if store1.com is mag1 or mag2 🙂
You need to narrow down what isn’t working. Try putting a a dummy file like index.html with just the world hello in the root directory of your virtual host. Then if you can browse to it at dev.domain.com/index.html then you know your virtual hosts are working.
Things to check for: ports – is MAMP’s Apache running on 80?
Is Magento the root of your virtual host or in a sub directory. By that I mean does /Users/frosted/Sites/mag/index.php exist or is it /Users/frosted/Sites/mag/magento/index.php?
Does your dev.domain.com actually resolve to localhost if you ping it on the command line?
This has all the hallmarks of a blog post…think I’ll do a ‘How to find out what is wrong with your MAMP virtual hosts config’ post tonight.
Nice to here from a Canadian, I hear it’s a lot like New Zealand.
ashley
Yep I have met lots of Kiwis out here, Whistler and Vancouver have quite a few[: Usually meet them on or near mountains!
I hear the mountain biking out in New Zealand is amazing as well…
I’ll check into those things and post back if I can get some solid figures. The odd thing is that magento loads, just without some files [mainly styling]
Cheers,
J
Ok Resolved it.. this is what ended up working. It was a problem on the Apache side.
NameVirtualHost *
ServerName localhost
DocumentRoot /Users/frosted/Sites/
ServerName http://www.domain.dev
DocumentRoot /Users/frosted/Sites/domain.dev
Great to hear it, and thanks for posting the solution back too!
Has anyone had a problem where after you install Magento and it gives you the option to login to the frontend or backend when you go to the backend it doesn’t let you in? I had heard it was related to a cookie/timezone and MAMP issue but I have not heard of a solution. Any ideas? My login page just reloads, if I put the wrong user/pass it says access denied but with the right stuff it just reloads.
You are right it sounds like a cookie problem. Have a look at what cookies are being placed by Magento, and check the ‘Expires’ portion is a date in the future. If not then check the timezone in Magento is set the same as the timezone on your MAMP server (which may well be your laptop time?).
To check cookie in Firefox, go to the options and on the privacy tab is a ‘show cookies’ button. Search in there using the search box for your domain/IP address.
@pete
your issue is most likely that there is no . [dot] in your domain name. This drove me crazy and lead me to this post for virtual hosting [as I know have a dot in the url…] . You can usually use your ip as well.
@ashley
dude, I was wondering if you had any idea how to implement this virtual hosts setup across a network.
I would like my colleague to be able to access my web server like any other. We are on the same network and I tried modding her hosts file to point to the dev path on my machine. It executes the php and works but the paths are incorrect causing styling to strip and relative links to break..
Basically I want to use mamp as a server for my network. I try: 192.168.1.1/project.dev/ 72.1.1.1 in the hosts file [on her machine] and it keeps putting me to the /sites directory instead of the target [192.168.1.1/project.dev/]
Any ideas on why this is happening? Ever used MAMP across a network?
Thanks again man,
Cheers,
J
Hi, Justin
Welcome back!
…snip…
Wow! That turned out to be longer than I had planned – I’ve made my reply a blog post on Apache virtual hosting in MAMP across a local network instead.
I got this when tring to access test.mysite.com/magento
——-
Whoops, it looks like you have an invalid PHP version.
Magento supports PHP 5.2.0 or newer. Find out how to install Magento using PHP-CGI as a work-around.
——-/
Most recent MAMP is installed and running, hmmmm….
Hi,
Please check your MAMP preferences you can select either PHP 4 or PHP5 , you possibly have PHP 4 selected as the recent MAMP’s use at least PHP 5.2.6. Let us know if that works.
Cheers,
Ashley
Hi Ashley,
Thank you for your reply and compiling this very useful information. The PHP version was the first thing I checked, all is good there. I am a little confused, after 6 hours of tweaking things on my fresh out of the box Macbook Pro, though less confused then when I started, thanks to you. I am not sure what is broken and I do have a few questions if you have a minute. Does the virtual host path have to point directly to /Users/ME/Documents/www/example.com/magento or will /Users/ME/Documents/www automatically give me use of MAMP in the sub all directories? Here is my setup at the moment and what I am experiencing. If I solve it, I will post my fix asap.
MAMP Setup:
-Ports 80 & 3306
-PHP 5
-Apache Document Root
/Users/ME/Documents/www
In my httpd.conf:
…
Include /Applications/MAMP/conf/apache/sites
In my ‘sites’ doc:
NameVirtualHost *:80
ServerName test.example.com
DocumentRoot /Users/ME/Documents/www/example.com
##
# 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 test.example.com
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
MAMP page works fine here:
http://localhost/MAMP/?language=English
Browser Results for: http://localhost/ and test.example.com
Index of /
[DIR] example.com/ –
Apache/2.0.59 (Unix) PHP/5.1.6 DAV/2 Server at localhost Port 80
Index.html does not load but it does here: http://test.example.com/example.com/
Browser Results for: http://test.example.com/example.com/magento
Whoops, it looks like you have an invalid PHP version.
Magento supports PHP 5.2.0 or newer. Find out how to install Magento using PHP-CGI as a work-around.
You see anything wrong here?
I just noticed the blog strips out the VirtualHost Tags. Are there any other tags that are missing from the tutorial. That was the only thing I put tags on. Thanks
Hi,
Thanks for spotting that, I have put the VirtualHost tags back in. Lesson: never forget to change back to plain HTML mode before updating a post!
Regarding your config it looks like the PHP your MAMP is running is 5.1.6 based on this line:
“Apache/2.0.59 (Unix) PHP/5.1.6 DAV/2 Server at localhost Port 80”
Do you definitely have the latest MAMP? You can confirm the MAMP php version by going to the MAMP ‘start page’ and clicking on the ‘phpInfo’ menu option. Mine tells me 5.2.6.
One other thing to check is for another php version. Open a terminal (Terminal.app), and type php -version. I wonder if you have an older php installed that is being picked up on the PATH by MAMP instead of it’s own one.
Your virtual host configuration looks fine (except the missing VirtualHost tag), once you put those tags in (and that’s totally my fault so sorry about that) it should work fine.
Hi Again,
With the tags posted in place as directed in your instructions I was able to correct a mistake and get my index loading. But my PHP is still not working properly. That was an interesting idea to look at the PHP version/s. Here is what I discovered:
Results for MAMP PHP.ini is PHP Version 5.1.6
Results for Terminal: Earth:~ ME$ php -version
—-Results START—-
PHP 5.2.6 (cli) (built: Sep 19 2008 11:28:54)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
—-Results END—-
So my MAMP PHP is older than yours which doesn’t make sense since I just downloaded MAMP yesterday. Though as expected the newer version trump over the older. And in fact my MAMP Zend engine is ‘off’ but Terminal says it is on… So? How to disable the extra PHP so MAMP PHP can take over? Where do I look for this mystery PHP?
I searched my drive but only found only 2 PHP.ini files, both were in MAMP for PHP 4 and 5. Hmmm, I was not totally convinced 2 PHP versions were running until I turned MAMP off and Terminal results were still PHP 5.2.6.
I checked /ect/apache2/httpd.conf and the PHP module is commented out, which makes sense since I never activated it. My user_me/Sites folder has no trace of it. I am very confused how this could be…. Any idea? Sorry, this seems to be moving off topic.
Hi,
I’ve started a new post with some possible tips for this PHP version problem with MAMP. Let’s move the discussion to there and keep this post focused on just Apache virtual hosting in MAMP.
Cheers,
Ashley
I tried to install Magento the way I did with wordpress, seemed to work except – I couldn’t logging into the admin. Is this because I didn’t setup a virtual server, or?
Hi,
I’ll need a bit more info, by ‘couldn’t login’, do you mean couldn’t get to the login page, or login was denied?
Hey. I got it to work by just installing it the simple way in MAMP. Just had to login via safari and change the baseurl to 127.0.0.1 instead of localhost
Had the same problem: No login in FF.
First I thought it was due to running on localhost so I set up a virtual server under http://localmagento. Funnily enough that didn’t cut it either. Only after I set up http://magento.local things started working. Looks like FF insists on a dot in the domain name…
Brilliant! Works perfectly. I’ve been trying to get multiple Magento stores setup in MAMP using symlinks with no luck. This makes it easy. THANK YOU!
gr8! using 127.0.0.1 interface worked for me 🙂 make sure using it in every step you take
This seems to work great, until I try to add a second virtual host to the sites file. Can you explain how to do that? When I add a second virtual host, I can not get the MAMP Apache server to start. Apache error (via log file): [Tue Sep 08 13:00:06 2009] [notice] caught SIGTERM, shutting down
Hi, Please flick me an email with your config in it, I’ll take a quick look, sounds like a problem with the config is causing Apache to fail at startup.
I found this to be very helpful, though in my case the DocumentRoot had to be in quotes. In other words:
DocumentRoot /Users/ashley/Documents/www/example.com
needs to be:
DocumentRoot ‘/Users/ashley/Documents/www/example.com’
It did not work without them.
After trying loads of tutorials yours finally provided a (almost) working solution, thanks! My problem is that all VirtualHosts are served from the same folder. So http://localhost/, http://leonderijke.local and http://fototest.local display the contents of the localhost DocumentRoot. If I add the DocumentRoot foldername to the url, then it serves the right folder: http://fototest.local/fototest
My files:
/etc/hosts:
127.0.0.1 localhost fototest.local leonderijke.local
httpd.conf:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
ServerName localhost
DocumentRoot ‘/Users/leonderijke/websites’
ServerName fototest.local
DocumentRoot ‘/Users/leonderijke/websites/fototest’
ServerName leonderijke.local
DocumentRoot ‘/Users/leonderijke/websites/leonderijke’
I’ve tried with and without quotes, but that doesn’t make a difference. Every VirtualHost gets served from ‘/Users/leonderijke/websites’. Any ideas? Thanks in advance!
Hi, Leon – can you try with either no localhost root or by moving the virtual hosts to directories outside of the localhost root? (just as a test)
Hi Ashley,
Without localhost root it serves both fototest.local and leonderijke.local from ‘/Users/leonderijke/websites/fototest’.
With localhost root, if I move the virtual hosts to directories outside of the localhost root it serves both virtual hosts from the localhost root.
I tested some more and I concluded that it serves every virtual host from the document root of the top virtual host (the first virtual host).
I’m staring at the virtual host config but I can’t find anything wrong with it. Do you?
You have got the
<VirtualHost *:80>
tags around each one right? I think it probably just got stripped from the comment?Yes, the VirtualHost tags are around each one.
Hmmm – I suggest back peddling a bit, to try and get a more minimal setup working. How about setting up a vhost for ‘blahblah.com’ and pointing it’s root at a directory somewhere that just has a single index.html file in it. Add blahblah.com to your hosts file pointing to 127… and see if that works (remove all other vhost config while testing it)
Ok, I deleted all virtual hosts from both the httpd.conf and the hosts file. I set up blahblah.com with just an index.html file in it. That worked. Then I added blahblah2.com with just an index.html file. That worked too!
Now I set up the other virtual hosts for my sites and they all worked! I really have no idea what went wrong (which frightens me a bit) but in any case: everything works fine now.
Thanks for your help Ashley!!
Glad to hear it Leon! I also hate it when you do that same thing and get different results. A programmer I used to work with called that ‘solar flares’ – seems like a good excuse to me!
Hi Ashley
I could really do with your help
I’m running MAMP 1.9 on OS X 10.5.8
I done a fresh install of Magento locally and it works, using default ports.
However, I have downloaded a remote site to make changes locally and am struggling to get this to work as it continues to go to the live site or brings up an error.
I have several questions.
1. Does Little Snitch affect the host file ?
2. If I add “Include /Applications/MAMP/conf/apache/sites” Apache server does not start (Web sharing is switched off in sharing preferences)
I appreciate any help you can offer
Thanks Matt 🙂
I finally solved my magento issue of taking a site from a remote host to a local host and I thought my notes may help someone else
1. Check .htaccess file for #RewriteBase – my previous programmer had added rules
2. Check MAMP Preferences – Mine was set to PHP 5.3 when it should have been PHP 5.2
It has taken me almost 12 hours to resolve this, 3 times longer than the actual job. But now I can get on with the design changes.
Hi, Matt – Sorry you solved this before I got a chance to offer some suggestions – glad to hear you got it sorted and thanks for stopping by to post your fixes.
thanks lot learn new thing from you…….
When I setup a virtual host domain on my local MAMP Magento site, the
homepage would show up fine, but none of the links from the homepage
would work – I would get server errors. I originally thought that the
problem was with my .htaccess files and Apache settings, but the
problem was actually in the /app/etc/local.xml config file. The line:
…needed to be updated to reflect the exact name of the virtual host:
I assumed that because the virtual host was local, and the mySQL
connection worked with localhost, this setting would be fine.
However, localhost won’t work – it needs to be the virtual host name.
A pretty obvious mistake, but one that is easy to make. Hope this
helps someone troubleshoot in the future……
Thanks for this clear write up!
Hi there,
I am trying to set up local environment for one of my client’s Magento sites.
I have copied their files and folders in my local folder and followed your instructions bt couldn’t set up a local environment.
I took the backup of httpd.conf file.
I also edited the file and added the following line at the end of the file
#Include /Applications/MAMP/conf/apache/sites
I created a text file called as sites in Applications/MAMP/conf/apache which has the following content
NameVirtualHost *:80
ServerName localhost
DocumentRoot /Applications/MAMP/htdocs/foldername
But don’t know how to edit etc/host and where it is located.
And have I done all the steps until here properly?
Please help !!
I’m a bit confused about the virtual subdomain in the hosts file…
If I already have Magento installed at example.com and I create a hosts entry for local.example.com, the Magento install example.com won’t run, will it? Wouldn’t I need to update the Magento database so it shows local.example.com as the site URL?
The issue I’m running into is with certain paid extensions you have to give them the URL you are going to use. Any deviation from that URL and the extension will not work. So doing local development, I need to keep the URL the same as the live site. That’s why I was thinking of using MAMP’s ports, but I can’t seem to get Magento configured to use those.
Any ideas?
So, I had Magento 1.7.0.2 CE running fine on MAMP using the http://localhost/magento/ URL, but had the problem with Chrome not being able to login. I read about the issue with cookies and extending the Cookie Lifetime to 86400, but also understand that Chrome wouldn’t accept any cookies not from a qualified domain, so it seemed like it was time to bite the bullet and implement this Virtual Server solution with MAMP. After some playing with various configs, it is working fine with multiple fake domains pointing to plain index.html sites.
So my problem is somewhere in Magento. Now when I goto the new domain, http://magdevsite.com it is redirecting straight away to http://localhost/magento/ where it was originally installed. I have checked:
– .htaccess has: RewriteBase /
– core_config_data has both unsecure and secure URL set to new domain (ej. http://magdevsite.com/ )
– pinging http://magdevsite.com/ resolves correctly to 127.0.0.1
Can’t figure out what is causing this! Any ideas maestro?
OMG!!! How embarrassing… as soon as I hit “Post Comment” I realized: clear cache! That did it. Basic— total rookie error. Sorry.
Third on the shortlist is the military-style. Shoes create
a arely there?aesthetic and could possibly pretty much with regard to
everything. These frequencies may vary from one starting point
another. Feel women can quickly embrace heels,
flats, flip flops also sandals! karen millen ebay outlet *Georgia*.