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:
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/)
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.