33

I am new to Mac but used Ubuntu for development for a long time. I know how to create virtual hosts in Ubuntu but have no idea about Mac. I have created a hosts entry like below :

##
# 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 mysite.loc
255.255.255.255 broadcasthost
::1             localhost

But what to do next?

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Ritesh
  • 4,720
  • 6
  • 27
  • 41
  • Also make sure that port 80 is specified in the ```/Applications/MAMP/conf/apache/httpd.conf ``` Listen 80 Listen 8888 ``` – kast96 Aug 23 '22 at 06:07

8 Answers8

77

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 webmaster@dummy-host.example.com
    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 webmaster@dummy-host2.example.com
    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..!!

Tpojka
  • 6,996
  • 2
  • 29
  • 39
Ritesh
  • 4,720
  • 6
  • 27
  • 41
  • 1
    For me it just says "It works" and nothing else shows up. Why wouldn't it reference the correct folder now? :o – trainoasis May 31 '16 at 17:57
  • 8
    On the general configuration of MAMP , Apache port should be set to 80, to make this configuration works – byroncorrales Feb 01 '17 at 17:15
  • 3
    Worked for me. Only thing is that while accessing virtual host, please do not forget to mention port. E.g. if your site name is myproj then, access this by http://myproj:8888/ where, 8888 is the port number. – Pupil Apr 25 '17 at 09:11
  • 1
    @trainoasis append :8888 after your url will do. – Keith Yeoh Jun 28 '18 at 08:14
  • @Ritesh, could you please take a look my question? https://stackoverflow.com/questions/52510110/virtual-host-with-mamp I have a problem when I creating with Vhost. – May Phyu Sep 26 '18 at 04:49
  • For people who are searching for Drupal 8 related changes while creating virtual host, you can refer this: https://stackoverflow.com/a/63734684/3901591 – Akshay Sep 04 '20 at 04:10
18
  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

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Srinivasan Raman
  • 441
  • 1
  • 4
  • 7
  • 3
    Great tip about allowing SymLink. It helped me after wasting a lot of time figuring what's wrong. – Dror Bar Apr 15 '19 at 14:48
4

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 !

André DLC
  • 261
  • 4
  • 11
  • Worked for me. Only thing is that while accessing virtual host, please do not forget to mention port. E.g. if your site name is myproj then, access this by http://myproj:8888/ where, 8888 is the port number. – Pupil Apr 25 '17 at 09:11
  • @Andre DLC could you please take a look my question? https://stackoverflow.com/questions/52510110/virtual-host-with-mamp I have a problem when I creating with Vhost. – May Phyu Sep 26 '18 at 04:50
  • Yo! bro. You saved my hours of time. – Channaveer Hakari Jan 08 '20 at 10:03
2

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.
obwansan
  • 21
  • 1
1

Recently I changed from XAMP to MAMP on MAC. I tried to set up my last virtual hosts, but MAMP's 8888 port number was avoid the regular work.

Finally I found the solution. You could change the Listen port and the ServerName in httpd.conf as you could find in the following post: https://www.taniarascia.com/setting-up-virtual-hosts/

szatti1489
  • 352
  • 2
  • 5
  • 16
0

Adding to the answer of Ritesh

You probably also want to add a directory configuration in your httpd.conf similar to the one that is already there, but for your the document root of your new server.

For Example:

<Directory "/Users/username/Sites/mysite">
    Options All
    AllowOverride All
    Order allow,deny
    Allow from all
    XSendFilePath "/Users/username/Sites/mysite"
</Directory>
rene
  • 41,474
  • 78
  • 114
  • 152
Jidbo
  • 1
  • 1
0

Building off of Srinivasan's answer. This is what I did in order to have 2 virtual hosts set up

  • myapp-local.local:8888/
  • myapp-local2.local:8888/

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

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

<VirtualHost *:8888>
    DocumentRoot /Applications/MAMP/htdocs/my_app
    ServerName myapp-local.local
    <Directory "/Applications/MAMP/htdocs/my_app">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:8888>
    DocumentRoot /Applications/MAMP/htdocs/instance-2/my_app
    ServerName myapp-local2.local
    <Directory "/Applications/MAMP/htdocs/instance-2/my_app">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

sudo vi /etc/hosts

##
# 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 myapp-local.local
127.0.0.1   localhost myapp-local2.local
255.255.255.255 broadcasthost
::1             localhost

Drupal specific: sites/default/settings.php

$settings['trusted_host_patterns'] = [
  '^localhost$',
  '^myapp-local.local$'
  '^myapp-local2.local$'
];
*/

restart MAMP server

jakebugz617
  • 319
  • 1
  • 11
0

In my case Document Root set in Virtual Host was not working because of incorrectly set ports.

In your MAMP App go to Preferences => Ports. If it is set to 8888 make sure to set it to 8888 in your /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf file as well

hzak
  • 719
  • 9
  • 18