A colleague of mine was having some trouble setting up virtual host's with his new installation of MAMP recently. So I decided to sit down and have a little look into it, and here follows a small guide to getting vhosts working locally on your mac using MAMP v2.0 and it's bundled apache.
I've chosen to use "mate" which is the terminal shorthand for textmate, which you can download here Macromates Textmate, but you can swap out my instance's of this with a more familiar editor like "nano" or "vim".
Firstly run the following command or open /Applications/MAMP/conf/apache/httpd.conf in your preferred editor.
dave$ mate /Applications/MAMP/conf/apache/httpd.conf
First thing I noticed was that MAMP now quietly includes a bunch of file's rather than just having it all appended at the bottom of httpd.conf, however all the usual files
Somewhere in amongst all of these files you will find the following:
# Virtual hosts # Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Simply uncomment it like so;
# Virtual hosts Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
I used a command from terminal like so:
dave$ mate /Applications/MAMP/conf/apache/httpd.conf
Next we need to set up a record in our /etc/hosts/ file, you will need to call on the power of "sudo" for this. Use the following command:
dave$ sudo mate /private/etc/hosts
You will of course be prompted for your password, which is your system password. Once open you should see something like this:
## # 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 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost
Now add this line to the bottom of this file, replacing "nameserver" with whatever you wish to call yours.
Save and Quit.
Now you need to, create your virtual host in the vhosts file. So remember that line we uncommented in the httpd.conf file? The file that points to is now where we're heading so.
dave$ mate /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Once open you should see something like this:
# # Virtual Hosts # # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration # # Use name-based virtual hosting. NameVirtualHost *:80 # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any block. # ServerAdmin firstname.lastname@example.org DocumentRoot "/Applications/MAMP/htdocs/playme/http/" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common # ServerAdmin email@example.com DocumentRoot "/Applications/MAMP/htdocs/playmedeux/http/" ServerName dummy-host2.example.com ErrorLog "logs/dummy-host2.example.com-error_log" CustomLog "logs/dummy-host2.example.com-access_log" common
You'll notice that there are two uncommented dummy servers set up, if you want to reference them at a later date just simply comment them out using the "#" infront of text on every line, or you can simply delete them. If your commenting it should end up like this:
# # ServerAdmin firstname.lastname@example.org # DocumentRoot "/Applications/MAMP/htdocs/playme/http/" # ServerName dummy-host.example.com # ServerAlias www.dummy-host.example.com # ErrorLog "logs/dummy-host.example.com-error_log" # CustomLog "logs/dummy-host.example.com-access_log" common #
Right, now to adding our new one, called testrun, let's copy the format from above and insert this code into the bottom of our file. Note that at this point you now need to decide where your document route is going to be, in most cases I would just create a folder inside htdocs inside my mamp install something like below:
dave$ mkdir /Applications/MAMP/htdocs/testrun/
Our document root in our VirtualHost setup should now reflect that when you add the following.
ServerName testrun DocumentRoot /Applications/MAMP/htdocs/testrun/
Notice how i've decided to make this available on a specific port but have just opted for any port, normal port numbers for apache are :8888 or :80 so you could specify either one of those if you so wished like so:
As opposed to:
Onto the final leg, now we just need to reboot apache, so, in terminal you can do:
Or simply if you prefer the mamp interface a stop and start of server's should suffice.
One last thing, let's quickly add a index.html file to our Document Root for testing purposes.
And enter "Welcome to the testrun vhost - Served by MAMP" to the file. Save and quit.
Finally type http://testrun:8888 into your favourite browser (of course with MAMP started) and you should see your index.html file served up nicely.