Dynamic subdomains in development
The site I am currently working on requires dynamic subdomains.
Details on doing this can be found on the rails wiki here and the pages linking from there. As well as on this awesome post by Greg Willits, whch is the post that got me off the ground. The crux of the matter is adding a wildcard declaration to your apache virtual hosts setup like so:
ServerAdmin yourdomain.com ServerAlias *.yourdomain.com
Unfortunately you cant do that if you are running a mongrel server on your local machine. So, instead, as Greg shows us, you need to define the subdomains explicitly in your /etc/hosts file (or in the case of mac - /private/etc/hosts).
127.0.0.1 www.yourdomain.dev
127.0.0.1 subdomain.yourdomain.dev
This is a solution, but I wanted to have dynamic subdomains in my development environment.
At this point I would like to mention that I don’t know if this is the best way to do this, and I dont fully comprehend the degree to which this can turn your life upside down - it seems to work without problems on my setup: Kubuntu 7.10 and Rails 2.0 and Ruby 1.8.6
So, finally, the dynamic hosts in development.
I created a module called HostsEdit in my lib folder:
def update_hosts
puts "updating hosts"
if ENV['RAILS_ENV'] == "development"
the_hosts = []
file_location = File.exist?("/private/etc/hosts") ? "/private/etc/hosts" : "/etc/hosts"
File.open(file_location, 'r') do |f1|
File.open("#{file_location}.backup","w+"){|f|f.write(f1.read)} unless File.exist?("#{file_location}.backup")
the_hosts = f1.read.split("n")
end
User.find(:all).each do |u|
the_hosts << "127.0.0.1t#{u.login}.yourdomain.dev"
end
the_hosts << "127.0.0.1twww.yourdomain.dev"
File.open(file_location,"w+"){|f|f.write(the_hosts.uniq.join("n"))}
end
puts "done updating hosts"
end
What the update_hosts action does, is to create a backup of your hosts file (unless there already is one) then reads in the current hosts, adds a new host for each user (using the login as subdomain) before writing the hosts back to the hosts file.
I included the module at the bottom of my environment.rb file
include HostsEdit
HostsEdit.update_hosts
so each time my mongrel server starts, it writes all the required subdomains to my hosts file.
Two little things that I forgot about initially: You have to run your webserver as sudo (mac and ubuntu) and lastly, if you add a new user during a session, you need to add the new subdomain to the hosts file.
class User < ActiveRecord::Base
...
def after_save
if ENV['RAILS_ENV'] == "development"
HostsEdit.update_hosts
end
end
...
end
This isn’t a major breakthrough or anything to clever, but it achieves what I want - to not have to edit my hosts file manually everytime I want to use a new subdomain while testing the development of my site. Hope it helps. Let me know if it works, or if it causes problems that future users can be warned about ![]()
Welcome to 2008
2008 has rolled along and we’re wrapping up a few projects and getting started on some exciting new ones.
One of our big focuses for the year is to simplify. We’re narrowing the focus of what we do to make sure that we can do it well. The biggest casualty of this is our hosting business which has grown slowly but surely over the last few years, to the point where it is a major distraction at times. Coding is what we’re good at, so coding is where we want to spend our time.
In the next few months we also plan to have launched the first of our own products, which is something we’re really looking forward too.