Create Virtual Hosts on MAMP
MAMP is a great GUI tool for creating server on localhost. But the free virsion of it only has very basic configurations. For example, creating a virtual host is not included. So I have to write the configurations on my own.
This article is based on Windows OS. Setup on Mac OS should be similar.
Why Virtual Hosts
Question: Why do we need to virtual hosts anyway? Answer: it simplifies your testing and developing. When developing a website, I want to visit it via something like http://test.local
, rather than http://localhost/test/
. The benefit of the former option is not just that it looks more like a real URL, or it is shorter. By creating test.local
as the local domain, the root directory for your site is configured to any location your would like it to be, so there is no more hazard of nested path.
Modify Hosts
First, add the desired domain to the system’s host file. It is located at C:\Windows\System32\drivers\etc\hosts
.
Add this line:
127.0.0.1 test.local
You can replace test.local
with almost anything of your choice.
Modify Apache Configuration
Modify C:\MAMP\conf\apache\httpd.conf
.
Find this line:
# Virtual Hosts
# Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Change it to
# Virtual Hosts
Include C:\MAMP\conf\apache\httpd-vhosts.conf
Create Virtual Host Configuration
Now create the configuration file httpd-vhosts.conf
.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot 'C:\MAMP\htdocs'
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot 'C:\MAMP\htdocs\test'
ServerName test.local
</VirtualHost>
Start MAMP, your site should be available at http://test.local
.