Setting Up Virtual Hosts

Setting Up 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 octothorpe.

# 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 httpd.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 The Command Line Crash Course 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 CNTRL + oNot CMD + o! – to save the file. Press enter to confirm. And press CNTRL + 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"

Leave a Reply

Your email address will not be published.

Markdown is enabled in comments. If you would like to post code in your comments, please wrap it in a <pre><code>. HTML/PHP code must be escaped. Failure to do so will make me sad.


<pre><code>def print_hi(name)
  puts "Hi, #{name}"

27 comments on “Setting Up Virtual Hosts”

  • 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”

  • 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:

    &lt;VirtualHost *:80&gt;
    DocumentRoot &quot;/Applications/MAMP/htdocs/exampledirectory&quot;

    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?