hostname

Setting up subdomains on your local web server

One of the primary tasks for a web developer is to set up a development environment that resembles the production environment as much as possible.

When I was a beginner in web development, I used to create a subfolder for each new project in my web server documents root and put all my project files in there; the local address of my projects would look something like this: localhost/myproject/index.php. This approach was very awkward and used to cause many problems during deployment into production environment.

Now, when I start developing a new web project, first thing I do is setting up a local hostname for my project; something like myproject.localhost which maps to a folder separated from other ones.

In this article, I want to show you how you can create hostnames on your local computer. I assume that you use Apache web server. Please note that these hostnames are not necessarily subdomains of localhost. I like to create them like myproject.localhost; you can create something like mywebsite.com.dev or even google.com.

For creating a local hostname, you should do these steps:

  1. Map you hostname to an IP on your local computer or local network. It usually is 127.0.0.1. You can set different IPs as well.
  2. Create a virtual host in you Apache webs server an tell it where does this hostname point to.
Syndicate content