Hi folks,
as you know it’s exciting to create a new rails application for several tasks. It’s fast, easy and everything is predefined. But what do you do if your application is (nearly) done? The next logical step is to set up a production web server – for me, this step always was a difficult issue. Mongrel/WEBrick was started via ‘ruby script/server’ and your application was reachable on your localhost, mostly on port 3000. You’re the only user who interacts with it – no problem (as long as your application is in development).
“The Web, however is an extremely concurrent environment. Production web servers, such as Apache, Lighttpd, and Zeus, can work on several requests – even tens or hundreds of requests – at the same time. A single-process, single-threaded Ruby-based web server can’t possibly keep up.” (quoted from ‘Agile Web Development with Rails’)
Therefore I want to show briefly how to set up a front-end server with an existing Rails application using an Apache server and the RubyGem ‘Passenger‘. Do the following as root.
1. Install Passenger (One-click Install) (assumed that Ruby itself and all needed Gems are installed)
2. Install Apache and it’s dependencies:
$> zypper in apache2
3. Add the Passenger module to your Apache server:
$> a2enmod passenger
4. Create a virtual host on your Apache server. Create ‘/etc/apache2/vhosts.d/myapp.conf’ and insert:
<VirtualHost *:80>
ServerName www.myapp.com
DocumentRoot /srv/rails/myapp/public
RailsEnv development
<Directory /srv/rails/myapp/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
Be sure that the path to your application is correct and do not forget the public directory! As you can see this virtual host receives all requests on port 80 (http). The line ‘RailsEnv development‘ specifies the ‘RAILS_ENV’ variable (in this case ‘development’, the default value is ‘production’). Normally you want ‘production’ for your production server!
5. Activate your virtual host in ‘/etc/apache2/listen.conf’. Just enable the line (remove the leading hash mark)::
#NameVirtualHost *:80
6. Now you can start your Apache server:
$> rcapache2 start
Important: when you want to check the log file be aware of the mode Passenger runs the Rails application (‘production.log’/’development.log’). By default, the log file is written by the user who owns the ‘environment.rb’ – check the log file’s write permissions (See also: User switching).
Have a look at the documentation! There you find a lot of configuration options which you should think of.
That’s it. Sure, there are many many other ways to get such a server running and this was just scratching the surface but should be a good point to start. Thanks to Thomas Schmidt for a good introduction into the topic.