Setting Up Virtual Hosts

Setting Up Virtual Hosts

This tutorial will show you how to work on multiple servers locally using virtual hosts.

Did you read about how to set up a local server environment, or do you already know how to do that? The next step is setting up virtual hosts. I’ve found most – if not all – of the resources and tutorials about setting up virtual hosts to be lacking. I’m going to set it up for the first time on the laptop I’m on, so I’ll be able to guarantee every step.

At this point, MAMP is set up, and going to http://localhost:8888 will take you to the document root you set.

1. Allow virtual hosts


In Finder, go to Applications > MAMP > conf > apache > httpd.conf and open the file with your text editor.

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

In File Explorer, go to C:/ > MAMP > conf > apache > httpd.conf and open the file with your text editor.

Find this line:

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Uncomment the code.

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

2. Allow SymLink Override

Find this line in that same httpd.conf file.

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride None

You’re going to change None to All.

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride All

3. Add the virtual host path

First of all, open MAMP, and ensure the document root is Applications > MAMP > htdocs. This is the default, but if you’ve been using localhost, you may have moved it.

All the way at the end of the extra/httpd-vhosts.conf file, you’re going to place a code that signifies a virtual host and specifies the path. The last line in the document should be </IfModule>, so place this code after that.

<VirtualHost *:80>
  DocumentRoot "/path/to/directory"

Change the DocumentRoot to wherever your PHP project is located. This is my path, but yours will of course be different.

DocumentRoot "/Users/tania/sites/learnphp"
DocumentRoot "C:/Users/tania/sites/learnphp"

Make sure to restart the servers on MAMP after making any changes! If you don’t reset the server to apply the changes, nothing will work even though you know you’ve done everything right. Trust me, I’ve done that too many times.

4. Allow your computer to recognize your local domain


Open the Terminal application. It doesn’t matter if you don’t know how to use Terminal or the command line; you only need to follow strict commands right now. (However, please read this guide when you decide to learn it!)

Type this in Terminal.

sudo pico /etc/hosts

You will be prompted for your password. Then a scary screen will come up that looks like this.

## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## localhost broadcasthost ::1 localhost fe80::1%lo0 localhost

You are actually editing a text based file. At this point, I’m just going to keep pressing the down arrow until I reach the end of the list. At the next line, I will type my new server name after, which is the computer’s local address.

Now press ctrl + o – Not cmd + o – to save the file. Press enter to confirm. And press ctrl + x to exit.


If Notepad is open, close out of it. This time right click on Notepad and select “Open as Administrator”. Open this file in Notepad:


At the bottom of the file, type the name of your virtual host.

At this point, the Window’s installation of Virtual Hosts is complete! Go to in your browser. Make an index.php file with the contents <?php echo 'Hello, world!' ?> and place it in the directory to test.

5. Remove :8888 from custom server URL

Almost done! After restarting your MAMP server, you can go to and it will take you to your path.

That’s pretty good. But, I want to make this perfect. I want to remove the :8888 from the end of the URL.

Back in the httpd.conf file, find these two instances (they won’t be next to each other):

Listen 8888
ServerName localhost:8888

And change them to this:

Listen 80
ServerName localhost:80

Save the file. Finally, in MAMP, Preferences > Ports, change the ports to 80, 8888, and 3306.

Restart your servers and try in your browser.

And now it works! You can repeat the code as many times as you want, for as many virtual hosts as you want.

<VirtualHost *:80>
  DocumentRoot "path/to/website1"

<VirtualHost *:80>
  DocumentRoot "path/to/website2"

If the above example doesn’t work, try removing :80 from the VirtualHosts tag, as this seemed to fix the problem for anyone having an issue. However, I just tested this April, 2017 on a new MAMP and new computer, and I can confirm if you follow it exactly you won’t have an issue.

Share to Twitter

Write a response

Your email address will not be published.

58 responses

  • Ben says:

    Followed the tutorial to the letter, but now all I’m getting when I visit is a 404 Not Found error. Tried the removal of :80 in the VirtualHosts tag but this has no affect.

    Any ideas?

    • Tania says:

      I recently set up a MacBook Pro and iMac following my own instructions to see where there could be an issue, and I was unable to find one. I would really love to know how I can make this more clear so no one has an issue. Did you restart MAMP after making the changes?

  • Jovanni says:

    The final step, is displaying a 404 error. What should I do? I worked fine before I changed the localhost of 8888 to 80.

  • Steve says:

    Hi Tania, is there a difference between adding virtual hosts, to just adding localhost in the url? Advantages?

    • Tania says:


      You can work on multiple websites at the same time with Virtual Hosts. With local host, you’re stuck with one. So I always use Virtual Hosts.

  • Wesley says:

    I was able to set up the virtual host “” but when I tried to changed the instances of 8888 in “httpd.conf” to 80 (and likewise in MAMP/Preferences/Ports) – to turn “” to “” – the apache server fails to start.

    No error message: it’s just unable to start for some reason.

    Removing “*80” from the virtual host tag did not resolve the issue.


    (macOS Sierra, April 8, 2017)

  • Juca (Brazil) says:

    Oh really!!! Many thanks Tania. You have an admirer here in Brazil. Congratulations…

  • Juca (Brazil) says:

    Hi Tania, Congratulations. I’m in trouble. I did everything right but, message appears: index of in the browser. What do I do?

    • Tania says:

      You’re not in trouble. That means you succeeded. Congratulations. Now you can put some content in the directory.

      • Amy DeStefanis says:

        I have the same issue – when I put in the browser after making the changes, I get “Index of / ” though I DO have content in the target path (the original path where we set our Document Root to in MAMP, right?)

        When I go to extra/httpd-vhosts.conf, the last line is not , by the way. I put the VirtualHost for in at the end anyway… Could this be the issue?

      • Amy DeStefanis says:

        Follow-up. In my case, removing :80 from the Virtual Host tag DID fix this issue. Go figure.

  • Anthony Robin Schneider says:


    I’m having some trouble editing the httpd.conf file. I can’t find the SymLink override section in my file. Am I looking in the wrong place? The closest I get is this:

    Options FollowSymLinks ExecCGI
    AllowOverride All
    Order deny,allow
    Allow from all



    • Tania says:

      Hi Anthony,

      You may be running a different version, I’m not sure. I would assume the ExecCGI is the same thing, and there’s no harm in testing it out.

  • John says:

    Hi Tania! πŸ™‚

    Should I just use the same DocumentRoot path for two sites if I want to use the same
    wordpress installation for both?

  • Jacquie says:

    When I enter I get a page from MAMP PRO saying that my virtual host was successfully set up and that the place holder should be replaced.

    I have tried fixing this every way possible so to double check:

    In Step 3 you tell us to make sure that in Preferences our DocumentRoot is: Applications/MAMP/htdocs
    *does this htdocs file need to be replaced?

    Then you tell us in the httpd.conf file the VirtualHost DocumentRoot should link to our files, mine is: “/Users/Jacquie/Desktop/WordPress/learnphp”
    *should these not be the same?

    • Tania says:

      The first part is not code – when you go to the program MAMP, and click on “Preferences -> Web Server” it will say “Applications -> MAMP -> htdocs”. This is not Applications/MAMP/htdocs in your httpd.conf.

  • Matthew Booth says:

    Thanks for this Tania :). I have another way in testing PHP websites before sending them live. This has been a helpful article πŸ™‚

  • Elvira says:

    Very helpful. Thank you!!

  • Simon says:

    Thank you. Very helpful. Managed to unmuddle the muddle I’d made of my hosts, vhosts, confs, et. al.


  • Subha says:

    Hi Tania, I am using my organization’s laptop and do not have permissions to modify hosts file. Can I continue the php theme build without configuring virtual hosts and just referring by localhost ?

  • Celine says:

    Finishing setting up on Windows, all worked out. Awesome!! Thank you very much, your tutorial is very helpful!

  • Catherine Lee says:

    Hi Tania,

    I can’t get to point to the DocumentRoot listed in httpd.conf. It always goes to the directory listed in the MAMP settings. When I attempt to make a second virtual host, it also goes to the same directory listed in the MAMP settings. I’ve restarted MAMP by turning it on and off, and by closing it and rebooting it again. I’ve updated to the latest version of MAMP.

    • Catherine Lee says:

      I figured it out. It works if I remove 80 in the VirtualHost tag.

      DocumentRoot /Applications/MAMP/htdocs/portfolio201509

      • Tania says:

        Interesting, I wonder if something changed in a more recent version of MAMP. The way I mentioned still works for me on Windows and Mac, but if anyone has an issue, they can try this way as well.

      • Mike says:

        Hi Tania.

        Thanks so much for this tutorial! Super helpful!

        Just to confirm, I also had the same issue as Catherine.

        My final block looked like this when I finally got it working(!):


        <VirtualHost *>
        ServerName my-server-name
        DocumentRoot "/path/to/document/root"


        MAMP 4.1
        OS X Sierra 10.12.3

      • Mike says:

        Ohhhh, sorry.. Guess you’re not supposed to also escape the !

        <VirtualHost *>
        ServerName my-server-name
        DocumentRoot "/path/to/document/root"

  • Tengu says:

    Great info!

    Only thing I did different was putting the virtual host info in the extra/httpd-vhosts.conf file (and allowing that file by uncommenting the proper line in httpd.conf.

    Only thing that doesn’t work (and I hope you can help me with) is getting rid of the :8888. I changed the Listen port and the localhost. Changed the ports in Mamp but (as I expexcted really) when restarting Apache, Mamp complains that the built-in Apache server already uses that port (which is true).

    So … what now without messing something up? Change the built-in Apache to :8888? (I have a Mac by the way), or shutting it down all together?

    • Gary says:

      Hi, likewise (Windows 10, MAMP 3.2.2) I cannot remove the “:8081” suffix from the domain. Also, MAMP cannot use Port 80 as it is in use by another process (or is reserved).

      I’d love to get rid of the suffix but I just can’t figure it out. Excellent article by the way, thank you for sharing!

  • joshua says:

    Hi great article!

    Original page ( loads, but additional pages do not.
    Do i need to change the Terminal code and the MAMP directory each time as well?


    DocumentRoot “path/to/example”

    Not working:

    DocumentRoot “path/to/website1”

    • Tania says:

      Hi! You’ll have to make multiple entires in the host file (just press enter and write a new name below it) and add another entry in the httpd.conf file.

      • Brit says:

        I’m still unable to do this with multiple sites and I’m not sure what I’m doing wrong. Here is what I have in the host file:

        And here is what I have in the httpd.conf file:

        DocumentRoot “C:Users\britc\OneDrive\Documents\Sites\learnphp

        DocumentRoot “C:Users\britc\OneDrive\Documents\Sites\test

        For bbv1, the page I’m trying to display is saved as index.php, which says “Hello aliens!” For bbv2, the page I’m trying to display (which is saved at documents\sites\test) is index.html, which says “this is a test” But when I go to in the browser, index.html does not display, instead I’m still seing index.php saying “Hello aliens”

      • Tania says:

        Have you restarted the Mamp server?

      • Kevin says:

        Tania, I am having the same issue. I am able to see the and it is linking correctly, however any other VirtualHost I attempt to add shoes up linking to the path for

        In httpd.conf:

        DocumentRoot “H:/Kevin/Sites/Template”

        DocumentRoot “H:/Kevin/Sites/Test”

        In Host file:

        I have also restarted my MAMP server as well and still both link to template

      • Ian says:

        I was also having trouble with multiple vHosts all serving the same content, but was able to solve it by adding the following above the directives:

        NameVirtualHost *

      • Tania says:

        Interesting. I recently went through this tutorial on a brand new computer and was able to follow it without an issue, so I’m not sure why it would be like that for you. Well, as long as it’s working.

  • James Becker says:

    Worked flawlessly on the first try! Thank you!

  • Nate says:

    Hi Tania,

    Firstly, thank you so much for all these awesome tutorials. You’ve inspired me to do more development from scratch instead of relying on some unnecessary frameworks. I’m really new to this, but I love how so many people in the web design and development community (like you) consistently put out such useful information to help newbies like me get better faster. Your articles have easily been among the best tutorials I’ve found.

    That being said, I’m having trouble with this tutorial and I’d appreciate any help if it’s not too much trouble.

    I’ve been using MAMP for a little while now to play with WordPress and develop sites locally. I’m familiar, though by no means totally proficient, with the command line. Still, I found your instructions very straightforward and easy to follow.

    My Document Root is in MAMP/htdocs/exampledirectory. So the last line of my httpd.conf file looks like this:

    <VirtualHost *:80>
    DocumentRoot &quot;/Applications/MAMP/htdocs/exampledirectory"

    I have two other WordPress sides in the same htdocs folder. I followed your directions up through step 4, and then went to Instead of seeing the index.php of the single WordPress install I’m trying to reach, I arrive at the index of htdocs, with a list of all three wordpress installs.

    Am I missing something? Or is there an additional process to configure virtual hosts for multiple WordPress installs in the same directory?

    I really appreciate any help you can give, and I hope I formatted my code correctly. Ion’t use markdown or escaped HTML/PHP very much.

    Thanks in advance, and happy coding!


  • Speed says:

    Tania – Everything works flawless, The procedure was clear and precise. Best tut ever! Thank you for taking time to writing this.

  • Sindhu says:

    Super helpful and easy to follow steps! Thank you so much.

  • Sky says:

    Thanks for the help Tania. You saved the day.

  • Lukiih says:

    Tania, thanks for this great article. I got a WordPress instance up and running successfully about a month ago after using some of your tutorials.

    Recently I changed the directory of my local files. All I did was move my WordPress files two folders level up. I then went into MAMP preferences and httpd.conf file to repoint everything to the new directory. I restarted MAMP as you’ve suggested. Whenever I go to my, the index/home page renders correctly (CSS, HTML, etc), but all the other pages are throwing the “Not Found The requested URL /about was not found on this server.” error.

    Any ideas on why this might have happened? Thanks in advance!

  • Jai says:

    Hey, tutorial is excellent, and it all works. However, I’ve tried to add another virtual host pointing to the same test file in the same location – I’ve left MAMP port and root settings the same as well. The only thing I’ve changed is the ServerName.

    I kept and setup a second virtual host called and this one doesn’t load while still loads. I’ve even switched the order they’re listed in. Any reason why other ServerName’s are not working, but only is?

  • Jus says:

    I used to use MAMP all the time but then I discovered AMPPS which is a bit like Softaculous (a 1 click installer shared hosts use) but it runs locally. You can easily provision hundreds of apps like WordPress such with a couple of clicks. It’s worth checking out even if you decide it’s not for you.

  • jose says:

    Hi, this is the best tuto I’ve found so far! I still have a problem though. Seems that every time I uncomment this line :

    Include conf/extra/httpd-vhosts.conf my MAMP won’t start. Please help :/

    • jose says:

      Nevermind I fixed it, it was this line that was causing the problem :

      ServerAlias *.dev *.work *

      However, it seems that I cannot access my project directories in this fashion anymore:

      Do I need to create a new virtualhost for “localhost” so I can access those directories in that way?

  • Joe says:

    After trying and failing for hours(!) to get MAMP to “see” my virtual host, this tutorial got me further than anything else I’ve tried – thank you! – I got all the way through step 4 successfully, but now I am trying to remove the :8888 from the URL following your step 5 exactly and its still not working (pulling hair out!). Entering my virtual name mydevsite.loc redirects to http://mydevsite.loc:8888/ and I get a

    This site can’t be reached

    mydevsite.loc refused to connect.

    I’ve gone over your step 5 many times, checking everything and no luck. Please help!!

    • Tania says:

      Hi Joe – you’re positive you restarted the server? The directions I have should be exact, so without seeing what you’re working with, I don’t know how else to help you. Send me an email with some more details or screenshots? I also want to note that it’s important to clear your cache. Please try visiting the site from a different browser (Firefox, Safari, Edge, whatever).

  • Will says:

    Worked a treat, thank you πŸ™‚

  • Can Margarini says:

    Question. shouldn’t we add new entries to the httpd-vhosts.conf file instead of adding them to the main httpd.conf?

Write a response