How to create virtual hosts in MAMP?

Solution 1:

While googling, I found these steps to easily create virtual hosts on MAMP:

  1. Open your console in mac and edit your hosts file like this

    sudo vim /etc/hosts

This opens a system file that contains the following line:

127.0.0.1    localhost

add your desired host name after local host:

127.0.0.1    localhost mysite.loc

press ESC, then :wq! to overwrite and close the file.

  1. Now go to your MAMP directory and open apache config file located at /Applications/MAMP/conf/apache/httpd.conf in any text editor and locate the following lines:
# Virtual Hosts
# Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf

Remove the hash (pound) sign from the beginning of the line that begins with Include

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

Save the file, and then open Applications/MAMP/conf/apache/extra/httpd-vhosts.conf. This is where you define the virtual hosts.

  1. At the bottom of the page are two examples of how to define virtual hosts in Apache. They look like this:
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/Applications/MAMP/Library/docs/dummy-host.example.com"
    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
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/Applications/MAMP/Library/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

Edit both examples. Virtual hosts override the existing localhost, so the first one needs to re-establish localhost. Edit the second one for the virtual host you want to add. Only the DocumentRoot and ServerName directives are required. To add a virtual host for mysite, the edited definitions should look like this:

<VirtualHost *:80>
    DocumentRoot /Applications/MAMP/htdocs
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/username/Sites/mysite"
    ServerName mysite.loc
</VirtualHost>

This assumes that you want to locate the files for mysite in your Sites folder. Replace "username" in the second definition with your own Mac username. If you want to store the files in a different location, adjust the value of DocumentRoot accordingly.

If you want to create more than one virtual host, copy one of the definitions, and edit it accordingly.

Save all the files you have edited, and restart the servers in the MAMP control panel. You should now be able to access the virtual host with the following URL: http://mysite.loc/.

Enjoy..!!

Solution 2:

  1. Allow virtual hosts

    Go to Applications > MAMP > conf > apache > httpd.conf

    Find this line:

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

    Uncomment the code by removing the hash symbol.

    # Virtual hosts
    Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
    
  2. Allow SymLink Override

    Find this line in that same httpd.conf file.

    <Directory />
        Options Indexes FollowSymLinks
        AllowOverride None
    </Directory>
    

    change None to All.

    <Directory />
        Options Indexes FollowSymLinks
        AllowOverride All
    </Directory>
    
  3. Add the virtual host path

    Go to Applications > MAMP > conf > apache > extra > httpd-vhosts.conf

    add the virtual host with servname and document root like the below code

    <VirtualHost *:80>
      ServerName example.dev
      DocumentRoot "/path/to/directory"
    </VirtualHost>
    
  4. Allow your computer to recognize your local domain

    Open terminal and type

    sudo pico /etc/hosts
    

    then add your domain

    127.0.0.1 example.dev
    
  5. Restart your server.

    If the url is showing error in chrome try safari

Solution 3:

In my config in MAMP, only the first virtual host was responding.
After hours of search I founded the instruction for solving the problem (before listing virtual hosts definitions) :

NameVirtualHost *:80

Now, my 3 virtual hosts are working !

Solution 4:

I followed this post, as recommended by szatti1489, and it worked for me: https://www.taniarascia.com/setting-up-virtual-hosts/

A couple of points are worth mentioning though:

  • This line didn't already exist in my httpd.conf file, I had to add it: Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
  • I had to use the .test domain ending, not .dev for my VirtualHost ServerName. The post mentions this, but then continues using.dev. Apparently, Chrome didn't support the .dev domain ending after 2017, although it didn't work in Firefox or Safari for me either.