How to Set Up Virtual Hosts in Apache on Mac OS

How to set up virtual hosts in apache on Mac OS.

What is Virtual Host

Virtual Host is used to serve multiple websites from single server. Virtual host used to enclose a group of directives that will apply only to a particular virtual host.

How to Set Up Virtual Hosts in Apache on Mac OS

Let’s set up our virtual host. To start with, Open httpd.conf file in your editor. I am using terminal with vim editor.

If you are not familiar with vim editor then check my previous tutorial on Vim editor guide.

Search for httpd-vhosts.conf and uncomment this line. Search and replace in Vim editor.

Now open httpd-vhosts.conf file in vim (or any editor you prefer).

 

Directives and their Meaning used in Virtual Host

DocumentRoot – It specify which file to serve by Apache for a given request. In my case  i have pointed my new project directory (/Library/WebServer/Documents/first_project/ ).

ServerName – ServerName is used to uniquely identify a virtual host when using name based virtual hosts.

ServerAlias – It sets the alternate name for a host. It may include wildcards as well (ServerName *.local.com) .

AllowOverride All – This directive allow .htaccess file directives to apply on your project. If you don’t want to apply .htaccess directives then set AllowOverride None.

Complete explanation of htaccess is explained in my previous htaccess tutorials.

Now we have set up our virtual host. Let’s map ip address to domain name in hosts file.

Add following entry in hosts file.

Now everything is done. Let’s restart apache server.

Reference Link

Apache virtual host documentation

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.