Use Domain Name instead of Localhost for Development with Apache

Hello friends, let’s do some tweak in our development environment. In this article we are going to use domain name instead of localhost for development with Apache and Windows.

This is great, instead of using common approach with typing localhost/myweb/myfile.php in your web browser, you’ll be using http://example.com/myfile.php and that in your local development environment. This is a recommended way to develop a website as it resembles the live production scenario.

Synopsis

It is a two-step process:

  • First we need to configure OS host file to map example.com to localhost’s IP address (127.0.0.1 here).
  • Then we’ll configure Apache server’s vhost configuration file to redirect all request coming from example.com to specific web directory.

How Domain Mapping Process Work in Local Development Environment?

Let’s understand how this works. I hope you know that all devices on internet have a unique IP address and that IP address can be mapped to a particular domain name.

For Ex: www.google.com or any other specified domain name is linked to a unique IP address that may be assigned to a server computer. Now whenever you make an http request by typing the domain name in your browser, the browser then searches the IP address assigned to that domain name and if it finds the IP address successfully, it redirects the request to given IP address.

Now the trick is to fool the “browser IP address search” by giving the desired IP address entry in host file.

Step-1: Configure OS host file

First of all we need to put an entry in host file. A host file is a kind of local DNS system, it is useful in domain name resolution. You can find the hosts file in following locations.

Windows: C:\Windows\System32\drivers\etc\hosts
Linux: /etc/hosts

Now open the file and add the given entry

127.0.0.1 example.com

This simply tells the OS to redirect all the requests made for example.com to 127.0.0.1, the IP address of all local computers.

Note: You might need administrative privileges to write the file.
Note: However, you can use any domain tld you want, still, you should stick to some general convention. We generally use .test tld. So, suppose when developing a website for Mohan Computers we can use mohancomputers.test as the desired domain name.

Step-2: Configure the Web Server (Apache)

Next, we need to configure the web server to serve the requests made to our configured domain name. Here, we use Apache as the Web Server.

Open the installation directory of Apache Web Server and then open the file /conf/extra/httpd-vhosts.conf. This is the Virtual Host File of the Apache Web Server. Again you might need administrator privileges to to edit the file.

<VirtualHost *:80>
ServerName example.com
DocumentRoot "D:\htdocs\example.test"
</VirtualHost>

That’s it, save the file and restart Apache. Now you can start using example.test to test run your website stored in D:\htdocs\example.test

Hope you find it helpful, comment below and share the knowledge.

Leave a Reply

Close Menu