If you are developing a Rails site locally with subdomains and you boot your application from WEBrick then you’ll get a URL like http://0.0.0.0:3000. The DNS for this resolves to your local machine so you’ll be able to see the site straight away with zero configuration.
The problem arises if you are developing an application that relies on subdomains. So a user can create an account and access the application at something like http://myaccount.example.com/. To get this working we need to a bit of work.
To test subdomains you can just add a record to your hosts file. On Unix style systems you can generally find this at /etc/hosts
This will resolve the domain to the application running on your localhost, but you’ll still need to apply the port number. So accessing http://theateam.example.com:3000 will serve the site. You’ll need to do the logic as to what to show based on the subdomain in your application of course.
This solution is quicker to setup but has the disadvantage of having to add a new record to your hosts file each time you create a subdomain.
A much better way is to serve your site locally from Passenger. If you deploy using Passenger you have the added advantage of developing on the same platform that you’ll be deploying to.
First you’ll need to install Passenger. This is reasonably straightforward:
gem install passenger
There will be some instructions after the second command that you’ll need to add passenger.conf file. Once that’s done you need to set up the virtual host.
I’ve covered serving sites locally on OSX before so if you need a brush up consult that article.
Tim Pope recognised the problem of subdomains and registered the domain smackaho.st and pointed it to 127.0.0.1. This means it will resolve to your local machine without any configuration. This means that we can set up a virtual host based on this domain and use a wildcard to also serve subdomains on that domain. Here’s how my virtual host setup looks:
<VirtualHost *:80> <Directory /Users/george/Sites/mysite> Options +FollowSymlinks +SymLinksIfOwnerMatch AllowOverride All </Directory> DocumentRoot /Users/george/Sites/mysite/public ServerName smackaho.st ServerAlias *.smackahost.st RackEnv development # RailsEnv development # for Rails 2.x.x apps </VirtualHost>
Restart Apache and you should be good to go
sudo apachectl restart
Now any subdomain you visit on smackahost.st will be served from your Rails application. No need to keep adding subdomains to your hosts file.
There is also another domain that serves the same purpose if you prefer it - lvh.me.
Linux and Unix du command tutorial with examples
Tutorial on using du, a UNIX and Linux command for estimating file space usage. Examples of showing a disk usage summary, outputting a human readable format, showing the size of a directory and showing the ten largest files or folders on a system.
Linux and Unix sha1sum command tutorial with examples
Tutorial on using sha1sum, a UNIX and Linux command to compute and check a SHA-1 message digest. Examples of reading a SHA-1 message digest, writing a SHA-1 message digest to a file, and checking a SHA-1 message digest.
Linux and Unix pwd command tutorial with examples
Tutorial on using pwd, a UNIX and Linux command for printing the name of the current working directory. Examples of printing the current working directory, avoiding symlinks and how to get the current working directory in shell scripts.