Using Virtual Hosts on the Apache Web Server

Having a local development environment set up on your computer is an efficient workflow for developing a site, but what do you do when you have multiple projects to work on simultaneously? This is the function of Virtual Hosts — which allows you to run multiple sites all on the same server — whether that be a local or production server.

This post will walk through the steps to setting up Virtual Hosts on MAMP, although the steps are roughly the same for any server running Apache.

Enable virtual hosts

The first step is to find the MAMP application in your Applications folder, select “Show Package Contents” by right-clicking on it, then open up the httpd.conf file that is located in conf/apache, with your text editor of choice.

There will be a section designated for virtual hosts:

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

Remove the # hash sign before Include in order to enable virtual hosts, and then save the file.

Enable SymLink Override

In the same httpd.conf file, there will be a section dealing with SymLinks:

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride None

Change AllowOverride None to AllowOverride All.

Designate the file path for the virtual host

After selecting “Show Package Contents” on the MAMP application, find the httpd-vhosts.conf file in htdocs/extra. Open this .conf file in a text editor, and add the following code to the very bottom of the file:

<VirtualHost *:80>
  DocumentRoot "/directory/filepath"

Change “/directory/filepath” to whatever is the actual file path for your  project, for example:

Document "/dev/my-new-site"

After updating the file path for your virtual hosts, you will want to restart MAMP in order for the changes to take effect.

Adding your local domain to the hosts file

Open up Terminal or another command line application of your choice. Type the following command:

sudo pico /etc/hosts

(Note: you can use nano, vim or another command line-based text editor — pico was just chosen out of convenience).

After entering the command you will be asked for the root password for your system.

A text file will appear, showing something along the lines of:

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##       localhost
::1             localhost
bc34::2%lo3     localhost

Go to the very bottom of the file (by using the down arrow key). Find the last line that begins with is the IP address for your computer, or localhost.

Tab a few spaces so the cursor is lined up with the “second column” of text above it, and add the name of your new virtual host (in our example,

Save the file with Control + O, hit enter, and then Control + X to exit out of pico.

Using your custom URL

By default, the localhost always has :8888 in its URL. So in our example, would take us to our new virtual host in the browser. We can remove this :8888 extension so we only have to use our chosen URL.

Open up htttpd.conf again. In the file, the following two lines of code will appear twice:

Listen 8888
ServerName localhost:8888

Update both of these two lines to:


Listen 80
ServerName localhost:80

Save and exit httpd.conf. Open Preferences in the MAMP application, and select the Ports tab. Change the Apache Port to 80, the Nginx Port to 8888, and the MySQL Port to 3306.

Hit ok, restart the servers in MAMP, and you should now be able to navigate to the new virtual host with

For more information on server management, or to learn more about web design and development services in the Los Angeles area, consult an expert at Sunlight Media.

Nick ensures all our projects are delivered on time, keeping in close communication with all our clients during the entire design and development process. While always deeply interested in technology since childhood, Nicholas has been involved in web development in a professional capacity since 2012, as both a front-end developer and project manager. He is most adept at HTML, CSS & JavaScript, but is interested in the entire spectrum of computer science. Some of his tech interests include full-stack JavaScript development, Unix-based operating systems, open-source web projects, and computer-assisted composition.  Outside of his work at Sunlight Media, he is a musician and composer.

The post Using Virtual Hosts on the Apache Web Server appeared first on Torque.

Read Full Article Here

Leave a Reply