This is part III of III. If you need to catch up, Part I is here and Part II is here
Passenger
Now for Passenger. Passenger is going to serve our Rails apps (and do a darn good job of it too).
Passenger has a very nice user-friendly installer that will guide you through the setup. But first lets check to make sure that Passenger's dependancies are installed.
On your server run apt-get install build essential and sudo apt-get install apache2-prefork-dev
Okies, now lets install the passenger gem (which you can probably guess what that command will look like) sudo gem install passenger then (and here's where you need to start paying attention a little because Passenger is going to give you some code to copy and paste) sudo passenger-install-apache2-module
It's going to tell you to "add blah to httpd.conf". On my system the httpd.conf was empty and all the settings were being laid out in /etc/apache2/apache2.conf instead, so that's where I put the Include /etc/apache2/passenger.conf and Include /etc/apache2/sites-enabled/ instead of Include /etc/apache2/passenger_vhosts.conf because I already had the sites-enabled doodad setup and I just decided to stick with it. Which one you use makes no difference but I found that using sites-enabled doodad (which I'll cover shortly) was a better fit for me, especially because it was an Apache virtual hosts doodad and not a Passenger virtual hosts doodad I could specify all my virtual hosts the same way even if they weren't a Rails apps using Passenger.
So anyway, add the Include statements with sudo pico /etc/apache2/apache2.conf and just paste (or type) them somewhere near the bottom. Once you've done that (including your save and exit) lets restart Apache sudo apache2ctl graceful to load those changes.
Actually using it
We now have all the pieces in place to serve multiple Rails apps (or of course, just the one). Lets say we have an app deployed to our server. On my server, to keep things nice and straightforward I have my domains in /home/deploy/public_html/nameOfDomain this is mainly because I have other stuff installed in my home directory that I want to keep separate from the folders housing the domains but this is purely personal preference. Just make sure to put your domains somewhere that you feel happy giving people access to. So back to our app. It's called testapp and it was deployed with Capistrano so it's located at /home/deploy/public_html/testapp inside a "current" folder that's actually a symlink to a folder containing the most up to date code inside of a /home/deploy/public_html/testapp/releases/some_date_stamp.
So lets create a virtual host for our testapp.
On your server run sudo nano /etc/apache2/sites-available/testapp which will create an empty file in that location and open up the Pico editor for us. This is where we tell Apache about our app. Add some code like this...
<VirtualHost *:80>
ServerName www.testapp.com
ServerAlias *.testapp.com
DocumentRoot /home/deploy/public_html/testapp/current/public
<Directory "/home/deploy/public_html/testapp/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
So lets pick this apart a bit so we both understand how this works. A virtual host (for our purposes) is basically how one server, with one IP address can serve multiple websites. Each of your websites is going to have it's own declaration. We're just putting it inside of sites-available/testapp to make it easier on us. The *:80 tells Apache to listen out on Port 80 for requests for this virtual host. Port 80 is the standard door that http requests come to (Port 443 is the standard port that secure https connections use). Next we have ServerName www.testapp.com which tells Apache which server it's going to pretend to be - this should match the domain name for the app. The ServerAlias can be used to set an alias for your domain that you want to refer to the same app. The asterisk is a wildcard so any request with anything before the domain (eg, somesubdomain.testapp.com) gets treated just like it was www.testapp.com. It's important to realise that any alias you set inside of the VirtualHost declaration will be treated by Apache just like it was a request to the ServerName itself and that regardless of what you do here, you need to have the appropriate DNS records set up or this won't work anywho.
Next is the DocumentRoot. "Once I've received a request for www.testapp.com, what do I give 'em?" Well Apache, send them to /home/deploy/public_html/testapp/current/public where the files and code for testapp actually lives. Now, because this is a Rails app we need a couple of other settings to make this work. Most of this is black box code I picked up from here and there but without it requests to your Rails app's URL goes all funny. And not good funny.
FollowSymLinks is needed because when we go to /home/deploy/public_html/testapp/current the current directory is just a symlink (an alias) to the most recent release of your code which is in the neighbouring directory releases
AllowOverride None is about allowing .htaccess overrides. I didn't need that so it's set to none.
Allow from all allows requests from any IP address (I think).
Order allow,deny sets, if I had to guess, the order of whether it checks the Allow directive before the Deny directive (which I haven't set here, but you may wish to). For example you might have
Order deny,allow
Deny from all
Allow from 123.45.67.890
which will tell Apache to first, check the request against the Deny directive, then the Allow directive. But that's just a mildly educated guess. If you need this kind of routing, take a look in the Apache docs (link).
So now that we have our virtual host configured, lets tell Apache we want to enable it by running sudo a2ensite testapp (or sudo a2dissite testapp in future to disable it) and then another apache restart sudo apache2ctl graceful and you should see your Rails app waving back at you if you visit http://www.testapp.com
Yay!
When it comes to your next site you just need to repeat the virtual host steps outlined above and Bob's your Mother's brother, you've got another Rails app and domain happily coexisting. And it doesn't need to be a Rails app either. I'm using this exact same method to serve purely static sites too.
So that should about cover the basics for the bare minimum you need to know to get your server up and running multiple sites.
And just one more thing. In your /etc/apache2/passenger.conf it's recommended to set some basic performance related settings (more info here) to keep Passenger running fast and lean.




