Assigning a Virtual Domain Name to Your Localhost
This quick guide will walk you through the process of setting up a virtual domain name on Apache so that you can easily access your local "website" via a local network. This would be a good first step in setting up an intranet.
With that in mind, let's pretend we are setting up an intranet for a small business and want users to be able to access it via http://intranet/. And, for the sake of example, let's say that your computer's network IP address is 192.168.2.100. Here's the process:
Add Your Virtual Host to httpd.conf
…Actually, you'll probably be editing httpd-vhosts.conf, but start by looking in your httpd.conf file to make sure. If you see something like this, you'll need to find and edit the file that it references:
# Virtual hosts Include conf/extra/httpd-vhosts.conf
Within that httpd-vhosts.conf file, you should see something that looks like this:
##<VirtualHost *:80> ##ServerAdmin firstname.lastname@example.org ##DocumentRoot "C:/xampp/htdocs/dummy-host.example.com" ##ServerName dummy-host.example.com ##ServerAlias www.dummy-host.example.com ##ErrorLog "logs/dummy-host.example.com-error.log" ##CustomLog "logs/dummy-host.example.com-access.log" common ##</VirtualHost>
That's an example virtual host commented out with the ## before each line so it will be ignored. Leave it there for future reference, but copy and paste another version right below it and get rid of the pound signs to uncomment it. Next, you're going to want to edit the information:
<VirtualHost *:80> ServerAdmin email@example.com DocumentRoot "C:/xampp/htdocs/intranet" ServerName intranet ServerAlias localhost ErrorLog "logs/intranet-error.log" CustomLog "logs/intranet-access.log" common </VirtualHost>
Save that file, then restart Apache. At this point, Apache knows to direct any traffic coming in to that address to the appropriate folder. But you still need to map out the address on the computers in the network (including yours).
Edit Your Local Host File | Windows
This next part tells Windows that whenever you type http://intranet/ into your browser, you want to access Apache on 192.168.2.100. (Note: For your local computer, use 127.0.0.1 instead of your machine's network IP address.)
Because of security settings, you'll likely need to be running Notepad as an administrator. To do that, click the Start button, find Notepad, right mouse click on it and select "Run as Administrator". After Notepad opens up, open up C:\WINDOWS\system32\drivers\etc\hosts (it might be in a slightly different location depending on your version of Windows and your installation options.
You should see some comments up top and then this:
127.0.0.1 localhost ::1 localhost
Those are your computer's IPv4 and IPv6 local addresses. Below them, you'll want to add your new entry:
127.0.0.1 localhost ::1 localhost 192.168.2.100 intranet
Save the file and then browse to http://intranet/ and you should see your new intranet. Pretty easy, right?
Edit Your Local Host File | Mac OSX
If you're on a Mac, follow these steps instead to edit your local host file. Similar to what we did in Windows, you'll need to edit as a superuser. To do so, open up Terminal and then type this:
sudo nano /etc/hosts
That will bring up a text editor with a file that includes mappings between IP addresses and hostnames. You'll want to add your new entry to the end:
Exit out of the editor (save when prompted), close down terminal and then open your browser and navigate to http://intranet/. You should be all set.