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:
In each operating system, there is a file that lists all pairs of hostname/IP addresses. In each OS it is located in a different path, but in all of them -as far as I know- it's name is hosts.
Path of the file in some operating systems are:
| OS | Path |
|---|---|
| Windows: NT, 2000, XP, Vista, 7 | %SystemRoot%\system32\drivers\etc\hosts |
| Most Unix and Unix-likes | /etc/hosts |
| Mac OS X | /private/etc/hosts or /etc/hosts (/etc on Mac OS X is a symbolic link to /private/etc) |
By default, hosts file contains few rows, each one looks like this:
127.0.0.1 hostname
By default, localhost is mapped to IP 127.0.0.1. You should add you own rows as well. Here is an example:
Add your own rows and save the file. Changes should take effect immediately. Otherwise, run one of these command in command line:
/etc/rc.d/init.d/nscd restart
dscacheutil -flushcache
lookupd -flushcache
ipconfig /flushdns
Go to this folder: <Apache folder>/conf and open file https.cong in an editor. Find the following lines:
Save file and now go to this folder: <Apache folder>/conf/extra and open file httpd-vhosts.conf it in an editor.
For each hostname you added to the hosts file, you should define a virtual host here. Add following lines to the file:
Here is full list of directives:
Save the changes and restart the Apache.
If for any reason you decided to add your virtual hosts directly into httpd.conf file, you should add following directive to this file as well:
This directive exists in httpd-vhosts.conf by default.
Comments
Post new comment