MAMP Virtual Hosts

Discussion in 'Web Design and Development' started by Kelon, Jul 19, 2009.

  1. macrumors member

    Joined:
    Jul 14, 2009
    Location:
    UK
    #1
    Hi
    Is the free MAMP actually able to take on board virtual hosts?
    I've previously, umpteen times, added virtual hosts to the installed Apache (comes with 10.4). It's so easy it is like falling off the proverbial log.
    Which is why it strikes me as odd I should suddenly become incapable of adding the correct amends to conf/host files with MAMP. Everything I look at outlines the usual standard entries - yet, my MAMP seems to be ignoring the entries in the conf file giving me "connection refused" messages. I think the hosts file is being picked up, but I don't know of a way of actually testing whether it is hosts or conf that's being ignored - if anyone knows, that'll be really cool and maybe a step to resolving this.

    I'm really stumped as all the instructions on the net seem to spell out the 'usual' falling-off-the-log amends that, as I say I've done successfully umpteen times with the Mac out-of-the-box Apache.

    For info:
    The ports are the default for MAMP - 8888 and 8889 respectively.
    Root folder as default: /Applications/MAMP/htdocs (specified in the preference of the start-up dialogue)

    Entry in hosts
    Code:
    127.0.0.1 localhost
    127.0.0.1 mySite.dev
    etc.,
    Interestingly there is no 127.0.0.1 localhost:8888 in the hosts file yet localhost:8888 works. How is this?

    Entry in /Applications/MAMP/conf/apache/httpd.conf
    Code:
    NameVirtualHost *
    
    <VirtualHost *>
        ServerName mySite.dev
        DocumentRoot /Applications/MAMP/htdocs/mySite
    </VirtualHost>
    localhost:8888 works regardless... interestingly it ignores any directive I might add to VirtualHost such as below continuing on to the root specified in the default.

    Code:
    <VirtualHost *>
        ServerName localhost:8888
        DocumentRoot /somewhereElse/htdocs
    </VirtualHost>
    Hoping I'm missing something blindingly obvious - cos blindingly obvious will be easy to sort. Cheers.
     
  2. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Here's what I have for my virtual hosts. First edit your httpd.conf file located at /Applications/MAMP/conf/apache/httpd.conf. I added the following code block to the very end of the file. I should also note that I have my port set as 80 rather than the default 8888 in MAMP. The below configuration uses a folder named site.com that holds the web root for the site (like the htdocs folder that MAMP uses), and we'll be setting up a virtual host to access the site locally using the alias site.local. This name can be just about anything, but people often use their domain name and then just change the extension to something like local or dev.

    The DocumentRoot and ServerName lines inside each VirtualHost section are the only required lines. Other parts are optional.

    Code:
    #
    # Use name-based virtual hosting.
    #
    NameVirtualHost *:80
    
    <VirtualHost *:80>
     DocumentRoot /Applications/MAMP/htdocs
     ServerName localhost
    </VirtualHost>
    
    <VirtualHost *:80>
     DocumentRoot /Volumes/website/site.com
     ServerName site.local
     ErrorLog /Volumes/website/logs/apache_error_log
     CustomLog /Volumes/website/logs/access_log combined
     <Directory "/Volumes/website/site.com">
       Options All Indexes
     </Directory>
    </VirtualHost>
    Next, we need to update the /etc/hosts file on the system. Open Terminal and type in,
    Code:
    sudo pico /etc/hosts
    It'll ask for your password then take you into a editor. You'll need to use your arrow keys to get around. There will already be some stuff in this file. Add the following line to the file (replacing the name with yours),

    Code:
    127.0.0.1 site.local
    Hit ctrl+x to exit the editor. It will ask you to save, press 'y' then hit enter. You're all set here. You'll need to restart MAMP and may need to reboot your system for those settings to take place.
     
  3. thread starter macrumors member

    Joined:
    Jul 14, 2009
    Location:
    UK
    #3
    Yes, Angelwatt is correct. Looks complicated, but is all do-able.

    The key thing here - which is consistent with what I was finding on the net generally relating to this - is the switching from the default MAMP port 8888 to the standard port 80 setting in order to achieve virtual hosts with MAMP. The two Apache servers (Built-in one and MAMP one) will not run together because ports only support one-at-a-time.

    I decided that if I did assign the 80 port to MAMP, I'd want to put into place something else and.... while researching it, decided I'd want to put something else-else into place too... and possibly so-on. So together with my (probably nonesensical) discomfort with running a server from my applications folder I decided (yep, hands up, rather arbitrarily) on another route.

    Some resources I came across in my searches/deliberations:
    sitepen.com - Super Charge Your MAMP
    postpostmodern.com - A Smarter MAMP
    and...
    Symbolic link production from the desktop - contextual menu plug-in

    Hope that's helpful - I've only implemented the Symbolic link plug-in (quite useful in a not-very-often-but-when-it-is kinda way), not undertaken any of the work outlined on the first two sites above. Regards.
     
  4. macrumors newbie

    Joined:
    Mar 3, 2009
    #4
    How do I force my browser get the local data instead of remote?

    Hi, I'm a copy/paste developer, so far, and believe it or not, I've managed to create nice wordpress websites.
    So now I'm one year later, installing MAMP to have them all (like 8 of them) on my local, or at least the new ones, so I'm experimenting with this and it's very interesting.

    I've managed to setup two different websites locally, so when I use them without www, it takes the virtual domain saved in my httpd.conf and my hosts. I expected, when I use www to be taken to the remote site (the remote site is set up without www everywhere)

    httpd.conf has

    Code:
    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 <VirtualHost> block.
    #
    <VirtualHost *:80>
        ServerAdmin sergio@sergiozambrano.com
        DocumentRoot "/Users/sergio/Sites"
        ServerName Sites
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAdmin sergio@mysite1.info
        DocumentRoot "/Volumes/Trabajos/mysite1/site"
        ServerName mysite1.info
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAdmin sergio@mysite2.com
        DocumentRoot "/Volumes/Trabajos/mysite2/website"
        ServerName mysite2.com
    </VirtualHost>
    
    
    and hosts is
    Code:
    127.0.0.1	mysite1.info
    127.0.0.1	mysite2.com
    
    The problem is, when I let the ethernet cable plugged in, the website is read from the web.

    Is there any way to force the content to be loaded from the local ip?

    Thanks.
     
  5. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    @sergiozambrano, I recommend using a different extension for the domain. I generally use .local for local testing, e.g., mysite1.local.
     
  6. macrumors 6502

    Joined:
    Aug 31, 2003
    Location:
    Campbell, CA
    #6
    I usually use a subdomain to identify sites that I'm working on. For example: dev.website.com.

    That way if I screw up and accidentally leave in a hard coded link to http://dev.website.com, it won't be a broken link on the live site since the live site will interpret the "dev" subdomain as an alias of "www".

    Of course, I have my name servers customized in such a way that it'll honor "dev" as a CNAME (alias) of "www".

    ----------

    BTW, MAMP Pro is totally worth the money. Upgrade from the free version if you haven't done so already. If you work on a lot of different sites or have a habit of testing a lot of different software, the ease of setting up new virtual hosts on the pro version just can't be beat.
     

Share This Page