Development Tips

Here are some development tips and tricks which you might find useful when doing web development. It involves some dns tricks and how to work with multiple sites on the same development machine.

Edit Your DNS

When developing web sites it is convenient to use proper domain names. This is especially true if you develop multiple web sites at the same time. Normally, your web server (e.g. Apache) will listen on 127.0.0.1 so you can just type http://127.0.0.1/ in your browser. However, there is a way to create domain name mappings on your computer for free.

This is done by editing your hosts file. On Mac and Linux, it can be found at /etc/hosts, on Windows at C:\Program Files\System32\drivers\etc\hosts. This file contains hard-coded dns mappings that the computer should use before asking a DNS server. Add the following to the line containing 127.0.0.1:

127.0.0.1 localhost www.example.com

You can then point your browser to http://www.example.com and it will automatically go to 127.0.0.1

Use .localhost domain names

While convenient, it is often good not to use the .com (or real) domain name for your development site. A good practice is to use the .localhost domain, e.g. www.example.localhost. This makes it simple and easy to know which domain you are working with. It will also not lead to confusion as to whether you've overriden the real dns mapping or not, since .localhost is not a public top level domain.

Multiple sites

If you develop multiple sites, you can just keep adding new entries to your hosts-file. They can all map to the same ip as long as you use HTTP. Example:

127.0.0.1 localhost www.example.com www.example2.localhost

In Apache, you can just create one new VirtualHost section per site. Remember to use the NameVirtualHost directive so that Apache will know you are serving multiple sites on the same ip. E.g.

NameVirtualHost 127.0.0.1:80

<VirtualHost www.example.localhost:80>
	ServerName www.example.localhost
	DocumentRoot /some/path/to/example
</VirtualHost>

<VirtualHost www.example2.localhost:80>
	ServerName www.example2.localhost
	DocumentRoot /some/path/to/example
</VirtualHost>

What about HTTPS?

One thing to remember when using multiple sites and HTTPS is:

Use one IP per domain name!

Due to the encrypted nature of the https protocol, Apache will not be able to determine which domain name a HTTP request is for. Apache will get an encrypted http request and can only determine which SSL key and VirtualHost to use by looking at the port number and ip address. Since virtually all HTTPS sites use the same port (443), you will need to use a separate ip per domain. This way, Apache will know which VirtualHost the request is for and use the correct SSL key to decrypt the traffic properly.

For the example above with two domain names, we can use two different ips in our hosts file:

127.0.0.1 localhost www.example.localhost
127.0.0.2 localhost www.example2.localhost

The Apache will be slightly different. No NameVirtualHost directive is required, but you need to make sure that Apache listens on all ips you need.

Listen *:443

<VirtualHost www.example.localhost:443>
	ServerName www.example.localhost
	DocumentRoot /some/path/to/example
		
	SSLEngine on
	SSLCertificateKeyFile /etc/apache2/ssl/www.example.localhost.key
	SSLCertificateFile /etc/apache2/ssl/www.example.localhost.cert
	SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>

<VirtualHost www.example2.localhost:443>
	ServerName www.example2.localhost
	DocumentRoot /some/path/to/example
		
	SSLEngine on
	SSLCertificateKeyFile /etc/apache2/ssl/www.example.localhost.key
	SSLCertificateFile /etc/apache2/ssl/www.example.localhost.cert
	SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>