Phusion Passenger on Ubuntu

Overview
The following a guide to configuring Phusion Passenger on Ubuntu for serving up your Ruby on Rails applications.
Requirements
It is assumed that you are familiar Ruby, Ruby on Rails, Apache, Phusion Passenger, and Ubuntu. It is also assumed that you have a Ubuntu Server or VM setup and configured with a proper server stack that includes Apache, MySQL, Ruby, Ruby on Rails, etc. You can learn how to setup and configure a fresh Ubuntu Server by reading my Ubuntu page before preceding.
Setup
Phusion Passenger will warn you of missing software but lets go ahead and beat Phusion Passenger to the punch by executing the following commands from the command line:
- sudo apt-get install libopenssl-ruby
- sudo apt-get install apache2-prefork-dev
Now we can install Phusion Passenger:
- sudo gem install passenger
- sudo passenger-install-apache2-module
Apache Configuration
Edit the /etc/apache2/httpd.conf or /etc/apache2/apache2.conf file (depending on how your server is setup, use one or the other but not both) by adding the following to the end of the file:
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/mod_passenger.soPassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6PassengerRuby /usr/bin/ruby1.8PassengerDefaultUser www-data
NOTE: Be mindful of the Phusion Passenger version as you might need to adjust the version numbers for your particular release. Otherwise, just use the notes provided for you during the Phusion Passenger install.
Finally, execute the the following commands from the command line:
sudo usermod -a -G www-data <your user login>sudo a2enmod ssl
The first command ensures the default www-data user group is group to your primary user account owning the server files. Otherwise, Phusion Passenger will assume to run the Rails app as root which might cause problems. The last command tells Apache to enable SSL support (you might or might not want this).
Apache Non-Secure Virtual Hosts
Edit your virtual host file (example: /etc/apache2/sites-available/example) so that the following is used:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName one.example.com
DocumentRoot /web/one/public
<Directory /web/one/public/ />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Repeat the <VirtualHost/> block for each Rails site you want to setup.
Restart Apache to pick up the new changes by executing the following command line: sudo /etc/init.d/apache2 restart
Apache Secure Virtual Hosts
Edit your virtual host file (example: /etc/apache2/sites-available/example-ssl) so that the following is used:
NameVirtualHost *:443
<VirtualHost *:443>
ServerName www.yourhost.com
DocumentRoot /somewhere/public
SSLEngine on
SSLCertificateFile /etc/ssl/certs/<your server name>.crt
SSLCertificateKeyFile /etc/ssl/private/<your server name>.key
</VirtualHost>
Repeat the <VirtualHost/> block for each Rails site you want to setup.
NOTE: While this setup will allow you to host multiple secure Rails sites it will not allow you to use a different SSL certificate for each as that is impossible. Even if you change the SSL certificate for each virtual host only the first one defined will be used.
Restart Apache to pick up the new changes by executing the following command line: sudo /etc/init.d/apache2 restart
How to Generate Your Own SSL Certificates
Run the following commands to create a temporary SSL key and certificate for testing with your applications:
- openssl genrsa -des3 -out server.key 1024
- openssl req -new -x509 -nodes -sha1 -days 365 -key server.key -out server.crt
Make sure to copy these files to the appropriate location by executing the following commands:
mv server.crt /etc/ssl/certs/<your server name>.crtmv server.key /etc/ssl/private/<your server name>.keychmod 400 /etc/ssl/certs/<your server name>.crtchmod 400 /etc/ssl/private/<your server name>.key
The files names and paths need to match as to what you entered in your virtual host file for port 443 (as mentioned above).
NOTE: To get up and running quickly without creating your own SSL keys and certificates, simply use the snake oil files as listed in the /etc/apache2/sites-available/default-ssl Apache configuration file.
Resources
7 Comments to Phusion Passenger on Ubuntu
Leave a comment
You must be logged in to post a comment.
Search
Categories
- Adventures
(105)
- Announcements
(36)
- Business
(19)
- Electronics
(28)
- Employment
(1)
- Epicurean
(10)
- Games
(3)
- Literature
(1)
- Mechanical
(4)
- Meetups
(18)
- Movies
(2)
- Music
(26)
- Photography
(1)
- Services
(29)
- Software
(136)
I am a relatively new user of Ruby on Rails. I recently started using it on Ubuntu (again, I’m new to Ubuntu, as well). I would like to be able to host a Rails site using Apache, so I have installed Apache2 along with Phusion Passenger. I have been able to create and display static .html pages (located in /var/www, e.g., /var/www/index.html). However, my Rails site is located at /home/{username}/rails/{railsapp}.
I created a sym-link to the rails site. The sym-link is in /var/www and it points to /home/{username}/rails/{railsapp}/public. When I try to load that site, I get a 403 Forbidden “You don’t have permission …” error.
I tried following the writeup, but I found a few things somewhat confusing regarding the lines:
1. sudo usermod -a -G www-data
2. sudo a2enmod ssl
Specifically, does refer solely to the username? or, does it somehow include the password?
Thanks for any assistance.
Sandy
Sandy,
I would suggest that you start with the basics first which would be to confirm that you have Phusion Passenger configured properly. Instead of running your Rails app from your user home directory, I would create a simple Rails application with some basic scaffolding first and deploy it to the /var/www directory where you are currently able to serve up basic HTML pages. Just make sure your path looks like this:
/var/www/(rails app name)/public
…and that your virtual host reflects that fact as well. Then reboot Apache2 to pick up the new changes.
After that works, then you can focus on setting up your Rails app from your user home directory via a sym link as you have described.
BTW, the real reason you are probably hitting permission problems is because your user account is not part of the www-data group that Apache2 uses to serve up pages. You can work on configuring your user account to this group later but, again, keep it simple and just make sure you can serve up your Rails app with www-data permission. So do the following once you have the Rails app deployed:
sudo chown -R www-data.www-data /var/www/(rails app name)/public
sudo chmod -R 775 /var/www/(rails app name)/public
That should allow you to serve up your Rails app with proper permission. Once that works, then you can move on to configuring your user account to work with Apache2.
So far, I’m not having a good time. I was able to create a simple, scaffolded rails application, called “testapp” in /var/www/. That rails application works when I use “sudo script/server” from terminal while in the /var/www/testapp folder. It starts Mongrel on port 3000, and I can use Firefox to run the application, including the saving of data to the database. If I then stop the server (using Ctl-C), and try to use Firefox to view the application at http://localhost/testapp/public, I get the Rails “Welcome aboard” screen. However, I believe that I am only seeing the static page, as clicking on the “About your application’s environment” link, merely gives me:
Not Found
The requested URL /testapp/public/rails/info/properties was not found on this server.
Apache/2.2.9 (Ubuntu) DAV/2 SVN/1.5.1 mod_mono/1.9 PHP/5.2.6-2ubuntu4.1 with Suhosin-Patch Phusion_Passenger/2.1.3 Server at localhost Port 80
rather than the anticipated environment.
I hate to say it, but I’m clueless about what’s happening, especially since I’ve now seen multiple screencasts wherein “It just works!!”
HELP!!!!
Update:
It’s still not working, but when I tried to do the install on my notebook, I got a bit further, as follows. Now, when I try to run the scaffolded Rails application (called “testapp”), which is located in my /www/var/ directory, I get the “Welcome onboard” screen, but now if I click the “About your application’s environment”, I get a Passenger error screen. Looking at the apache2 error log file, it says that the rails-2.3.2 gem is missing. However, the gem is installed (as confirmed during the passenger-install-apache2-module step), and testapp runs fine when I run it from Mongrel in the same location (and its “environment” includes rails-2.3.2). echo $PATH shows that the directory with rails-2.3.2 is present.
What’s “Plan B”?
Sanford,
It sounds like you are almost there but I would probably need to take a peek at your local environment to understand more specifically what you have setup. Just double check you have followed my notes above.
BTW, you might find this article useful in addition to my notes: How-To Setup a Linux Server for Ruby on Rails - with Phusion Passenger and GitHub. Give it a shot as it might shed more light on your problem.
Brooke, I noticed a few issues with your non-SSL apache config while I was creating my client’s server:
* Option => Options
* Remove the empty tag closure on the Directory tag.
* No space between allow,deny
Kev
Awesome, thanks. I’ve updated the original post to reflect your comments. BTW, I left the Directory closure since it would be invalid syntax without it. The option indents maybe threw you off?