Step 1: Open Apache httpd.conf
Open the httpd.conf
sudo nano /etc/apache2/httpd.conf
Search for ‘vhosts‘ and include line
# Virtual hosts #Include /private/etc/apache2/extra/httpd-vhosts.conf Include /etc/apache2/vhosts/*.conf
Also allow another module to run by uncommenting:
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
Step 2: Edit the vhosts.conf file
Create a file called local.conf in /etc/apache2/vhosts/ and open this file to add in the vhost.
sudo nano /etc/apache2/vhosts/local.conf
In Apache this .conf file is a virtual host file which holds your server configuration i.e. ServerName, ServerAlias, DirectoryIndex, DocumentRoot. Any unwanted is commented out with a # sign.
If you would like to have multiple subdomains pointing to different directories, you need to copy the <VirtualHost *:80></VirtualHost> multiple times.
<VirtualHost *:80> # Admin email, Server Name (domain name), and any aliases ServerAdmin webmaster@example.com ServerName local.example.com #ServerAlias example.com # Index file and Document Root (where the public files are located) DirectoryIndex index.html index.php DocumentRoot /Library/WebServer/Documents/html # Log file locations LogLevel warn ErrorLog /Library/WebServer/log/error.log CustomLog /Library/WebServer/log/access.log combined <Directory "/Library/WebServer/Documents/html"> Options Indexes FollowSymLinks Order allow,deny AllowOverride All Require all granted </Directory> </VirtualHost>
So in the example above a vhost is created and log file directory has been assigned. The log file directory needs to be created manually, otherwise apache will give you error. What you need to change is the ServerName, ServerAdmin DocumentRoot to suit your needs.
Finish and save the file.
Now also you need to map the IP address to be the localhost.
Step 3: Map Your IP address to localhost
sudo nano /etc/hosts
Add the Domain and ‘www‘ alias to resolve to the localhost address
127.0.0.1 example.com local.example.com
Step 4: Restart Apache
sudo apachectl restart
Check out your local vhost domain in the browser by typing in example.com according to this tutorial’s host file. You need to change the domain accordingly to your host file.
Cheers!