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.
sudo vim /etc/apache2/httpd.conf
Search for httpd-vhosts.conf and uncomment this line. Search and replace in Vim editor.
// Uncomment this line.
Now open httpd-vhosts.conf file in vim (or any editor you prefer).
sudo vim /private/etc/apache2/extra/httpd-vhosts.conf
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.
CustomLog "/private/var/log/apache2/local.com-access_log" common
Options Indexes FollowSymLinks
Allow from all
Now we have set up our virtual host. Let’s map ip address to domain name in hosts file.
sudo vim /etc/hosts
Add following entry in hosts file.
127.0.0.1 local.com www.local.com
Now everything is done. Let’s restart apache server.
sudo apachectl restart