Apache local website for development/testing (Windows/Mac/Linux)

Sometimes you have many website projects that need to use whole (sub)domain on a single test server, then VirtualHost is the soultion.

Step 1: Create VirtualHost

Find your httpd.conf, typical path:

XAMPP: xampp/apache/conf/
Mac/Linux:  /etc/apache2/conf
EC2: /etc/httpd/conf

Edit httpd.conf, You need to be root under Linux/Mac, so
sudo vi /etc/apache2/conf/httpd.conf

Add the following lines at the bottom
(You can use any domain name, and add as many VirtualHost as you want):

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/company.com"
ServerName local.company.com

Finally restart Apache.

Mac:sudo apachectl graceful
Linux:sudo /etc/init.d httpd reload
Windows: Run services.msc and restart Apache service

Step 2: Edit your hosts file spoof your hostname

Windows: %SystemRoot%\system32\drivers\etc\hosts
Unix/Linux/Mac: /etc/hosts

Add the following line at the bottom: local.company.com

Note: You must be administrator/root to edit hosts file

Unix/Linux/Mac: sudo vi /etc/hosts
Windows XP: Open Notepad and open “%SystemRoot%\system32\drivers\etc\hosts” directly
Windows Vista and later: Search for Notepad, right click and “Run as Administrator“, then and open “%SystemRoot%\system32\drivers\etc\hosts”

You should able to access http://local.company.com on your computer now.

Reference: http://httpd.apache.org/docs/2.0/vhosts/name-based.html

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.