passenger + apache + NameVirtualHosts
Everyone knows how to setup passenger and apache on a linux box. But if you have multiple apps, its a pain keeping track of all the localhost port numbers that point to each of them. In this article I discuss you can have addresses like myapp.mybox.lan to point ot specific apps. I’ve written this for a linux system but it should work just as well for a Mac. Also, I haven’t tried this out with ngnix. I’ve also gone through the setting up of Passenger for those of you who still use script/server.
Setting up Passenger
Installing the passenger apache modules is as easy as:
$ gem install passenger
$ passenger-install-apache2-module
That will download and compile passenger’s apache modules. Once compiled the installer will give you instructions to load these modules using apache configuration directives. You’ll probably have to edit your apache configuration file for that. If you’re on linux, these will probably be in /etc/apache2/[apache2.conf|httpd.conf]. Just append the three lines that the installer gives you to this file. They’ll look something like this:
LoadModule passenger_module /home/rjsvaljean/dev/ruby/lib/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/mod_passenger.so
PassengerRoot /home/rjsvaljean/dev/ruby/lib/ruby/gems/1.8/gems/passenger-2.2.11
PassengerRuby /home/rjsvaljean/dev/ruby/bin/ruby
Creating the domains
Once that’s done lets create our rails app in /path/to/app/blog. In order to demonstrate how to create different addresses for different apps create another app at /path/to/app/todolist. Say that we want those two apps accessible at blog.lan and todolist.lan. Notice that I’ve use the TLD .lan but that’s completely aribitrary. You can use anything that you want. Now comes the bit where we tell your computer to got to localhost when you point your browser to blog.lan. For this we need to edit our /etc/hosts file. You’ll need to be root to edit it. Add the following two lines:
127.0.0.1 blog.lan
127.0.0.1 todolist.lan
Creating ‘NameVirtualHost’s
Now we need to instruct apache where to look when it gets a request for blog.lan. Open up the config file once more. First you need to declare that Named VirtualHosts are running on post 80 - your default port for http requests. To do that:
NameVirtualHost *:80
Now define your VirtualHost sections:
<VirtualHost *:80>
ServerName blog.lan
DocumentRoot /path/to/app/blog/public
</VirtualHost>
<VirtualHost *:80>
ServerName todolist.lan
DocumentRoot /path/to/app/todolist/public
</VirtualHost>
Now reload apache with ‘/etc/init.d/apache2 reload’ and you’re done. Pointing your browser to blog.lan or todolist.lan whould show you the default rails page for a new app.