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.
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Linux and Unix watch command tutorial with examples
Tutorial on using watch, a UNIX and Linux command for executing a program periodically and showing a fullscreen output. Examples of watching a file download, a network interface come up, and showing the five most CPU intensive processes.
Build your own Vim statusline
Statuslines in Vim are not hard to create. Making your own means one less dependency in your life.
Custom Vim Bindings in tmux 2.4
tmux 2.4 made a significant change to key bindings. Here is how to support custom keybindings for versions before and after tmux 2.4