How to Set Up Virtual Host with MAMP on a Mac OSX

Standard

1. Update the hosts file:
We are going to add an example domain named “thisdayinbangladesh.dev” as a virtual host through this tutorial. Lets first add this in the “hosts” file by typing the following command on the Terminal,

sudo nano /etc/hosts

Then find the following line,

127.0.0.1 localhost

update this to following,

127.0.0.1 localhost thisdayinbangladesh.dev

Also to add our domain to the IPv6 version find the following line somewhere bottom of the file,

fe80::1%lo0 localhost

update this line to the following one,

fe80::1%lo0 localhost thisdayinbangladesh.dev

Finally press “ctrl+x” and then “y” to save and exit this file.

2. Update httpd.conf file
Open up this file by typing the following command,

sudo nano /Applications/MAMP/conf/apache/httpd.conf

find out the following line,

#Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

It might have a “#” symbol at the very beginning of the line. Delete the symbol to activate this statement. If the line is not anywhere of the “httpd.conf” file then add the above line anywhere of this file. Now, save and exit this file.

3. Update httpd-vhosts.conf file
Now go into the “/Applications/MAMP/conf/apache/” folder by executing,

cd /Applications/MAMP/conf/apache/

If you can see a folder named “extra” then enter inside it otherwise create this “extra” folder and then enter inside it.
Now update (If there was already a “extra” folder then there should be a “httpd-vhosts.conf” file other wise just create the “httpd-vhosts.conf” file inside “extra” folder) the “httpd-vhosts.conf” file by typing the following,

sudo nano httpd-vhosts.conf

Then the final “httpd-vhosts.conf” file should have the following code block. Whether you add these lines or updated the existing file.

NameVirtualHost *:80

<VirtualHost 0.0.0.0:80>
DocumentRoot "/Applications/MAMP/htdocs"
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Applications/MAMP/htdocs/thisdayinbangladesh/public"
    ServerName thisdayinbangladesh.dev
    <Directory "/Applications/MAMP/htdocs/thisdayinbangladesh/public">
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Here “thisdayinbangladesh” is a project directory; actually a laravel4 project directory inside my “htdocs” folder and we added the “public” folder thats resides in “thisdayinbangladesh” folder as the document root cause laravel4 needs to point to the “public” folder as the application root URL.
In this stage of this example. You can forget about laravel4 and simply create the directory “/thisdayinbangladesh/public” inside your “htdocs” folder and place a simple “index.html” file there inside the “public” folder.

4. Restart:
Simply stop and re-start the Apache service from your MAMP control panel. Then try out the URL on your browser. http://thisdayinbangladesh.dev You should see your “index.html” file!

Important:
This tutorial will take you through out a nice series of posts on which a full laravel4 web application has been developed. That service is already LIVE HERE. So, stay tuned!

Update:
The next tutorial is ready Installing Laravel4 in Mac OSX with MAMP. So, you are another step close toward the full laravel4 application development series.

Advertisements

4 thoughts on “How to Set Up Virtual Host with MAMP on a Mac OSX

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s