Home arrow Ruby-on-Rails arrow Page 2 - Deploying an Application to the Server

Deploying an Application to the Server

In this fourth part to a five-part series on deploying a Rails application, you'll learn how to deploy an application manually, create users and groups, and more. This article is excerpted from chapter 12 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
August 24, 2010
  1. · Deploying an Application to the Server
  2. · Starting LightTPD

print this article

Deploying an Application to the Server - Starting LightTPD
(Page 2 of 2 )

Now that we have the project on the production machine, we can start LightTPD by using the lighttpdcommand and specifying the location of the configuration file:

$ sudo lighttpd -f /u/apps/emporium/current/config/lighttpd-production.conf

We start LightTPD using thesudocommand because the server needs to bind to the restricted port 80. You can verify that LightTPD is running by executing the following command:

$ ps -ef|grep lighttpd

--------------------------------------------lighttpd  2325   1  0 13:53 ?     00:00:00 lighttpd -f /u/apps/emporium/...

Note that the process is running under thelighttpduser we just created, even though we started it using thesudo command and another user. This is because we set theserver.usernameandserver.groupnameparameters tolighttpdin thelighttpdconfiguration file.

Starting FastCGI Processes

Before starting the FastCGI processes, we need to change the group ownership of the application directory to railswith the following command:

$ sudo chgrp -R rails /u/apps/emporium/current/
$ sudo chown -R rails /u/apps/emporium/current/

Next, start the FastCGI processes (and the application) by executing the following commands:

$ su - rails


$ /u/apps/emporium/current/script/process/spawner -p 7000 -i 4

--------------------------------------------Checking if something is already running on port 7000...NO
 Starting FCGI on port: 7000
  spawn-fcgi.c.190: child spawned successfully: PID: 4289
Checking if something is already running on port 7001...NO
 Starting FCGI on port: 7001
  spawn-fcgi.c.190: child spawned successfully: PID: 4291
Checking if something is already running on port 7002...NO
 Starting FCGI on port: 7002
  spawn-fcgi.c.190: child spawned successfully: PID: 4293
Checking if something is already running on port 7003...NO
 Starting FCGI on port: 7003
  spawn-fcgi.c.190: child spawned successfully: PID: 4295

Thespawner script automatically starts the number of FastCGI processes specified with theiparameter. As you can see from the example, thespawnerscript starts four processes listening on ports 7000, 7001, 7002, and 7004.

Capistrano expects to find a “spinner” script that it can execute to start (spawn) your application, so save the following inscripts/spin:

/u/apps/emporium/current/script/process/spawner -p 7000 -i 4 --repeat=60

Notice that we added therepeat option, which keeps thespawnerprocess running. Thespawnerchecks (every 60 seconds) that all FastCGI processes are running, and starts them again if they crash.

Note  Remember to make the scripts executable by runningchmod ug+x script/process/*, before checking in the files to Subversion. Subversion will preserve the access rights after adding the files to source control. This means that the scripts can be executed when the code is checked out by Capistrano on the production server.

Next, run thespinscript:

$ scripts/spin

Verify that there are onelighttpdprocess, aspawnerprocess, and four FastCGIrubyprocesses up and running with the following command:

$ ps -ef | grep 'ruby\|lighttpd'

--------------------------------------------lighttpd  2325   1  0 13:53 ?      00:00:00 lighttpd -f➥
rails      4557  1  0 15:03 ?       00:00:00 ruby➥
-p 7000 -i 4 --repeat=10
rails      4559  1  0 15:03 ?       00:00:01
/u/apps/emporium/current/public/dispatch.fcgi rails      4561  1  0 15:03 ?       00:00:01
/u/apps/emporium/current/public/dispatch.fcgi rails      4563  1  0 15:03 ?       00:00:01
/u/apps/emporium/current/public/dispatch.fcgi rails      4565  1  0 15:03 ?       00:00:01

The first column in the output tells you the owner of the process. You can now try to access your production environment by opening one of Emporium’s pages, such ashttp://production_server/catalog, in your browser. You should see the Emporium catalog load. If not, examine the Rails and LightTPD error logs to see what went wrong.

Phew! Remembering all of the commands required to deploy a Rails application is tough. This is where Capistrano comes in handy. In the next section, we’ll automate the deployment with Capistrano.

Automating Deployment

Capistrano is a tool aimed at automating deployment tasks, but it can also do a lot more. Capistrano allows you to execute almost any command simultaneously on multiple remote servers. For example, it can restart a web server, create a database, deploy your application, and start the FastCGI dispatchers at the same time on one or more remote servers. Instead of remembering a long series of commands, you can deploy a new version of your application with just one command: rake deploy.This makes it easy to deploy new versions or simple bug fixes. Capistrano does everything in transactions and supports rollback, so that if something fails on one server, the changes on all servers are rolled back. Capistrano also lets you roll back to the previous version of an application after deployment (by runningcap rollback).

Capistrano requires that you have an SSH server running on your remote server. (Setting up an SSH server was discussed earlier in this chapter.) Capistrano first logs in to the remote server using SSH, and then issues shell commands, which are part of a user-defined deployment script, over the secure connection.

Capistrano uses Portable Operating System Interface (POSIX) shell commands, so the operating system on the remote server must be POSIX-based. Linux, Mac OS X, and Unix systems fulfill this requirement, but Windows doesn’t.

Installing Capistrano

You can install Capistrano with the RubyGems package system. Simply execute gem install capistranoon the command line:

$ sudo gem install capistrano --include-dependencies

Note that Capistrano will ask you for the password when it establishes the connection to the remote server. By default, Capistrano shows the password in clear text as you type, which is not a good idea if Kevin Mitnick is standing behind you. To hide the password, install thetermiosRubyGem package with the following command:

$ sudo gem install termios

With Capistrano installed, you’re ready to prepare for deployment. The first step is to create the script, or deployment recipe.

We'll cover that tomorrow, in the final part of this series.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials