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).
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:
--------------------------------------------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:
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:
Verify that there are onelighttpdprocess, aspawnerprocess, and four FastCGIrubyprocesses up and running with the following command:
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.
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.
You can install Capistrano with the RubyGems package system. Simply execute gem install capistranoon the command line:
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.