PDA

View Full Version : Deploy rails app using SSH/PuTTY/Pageant, SVN, MySQL, Capistrano, Mongrel, nginx - 1


Larry
03-22-2008, 06:16 PM
Getting a Rails app up and running can be a daunting task - especially for Windows users who left the command line a long time ago. Fortunately, RimuHosting provides top-notch support in this area. Following are things they told me and I found on my own. Hopefully it will make your journey shorter and more fun.

Notes
It is assumed that RimuHosting has installed everything you need: Ruby, Rails, Subversion, MySQL, Mongrel and nginx. This is the simplest part of the process because all you have to do is ask them!

Windows-specific instructions or advice can simply be ignored by non-Windows users.

These notes were culled from things I wrote down during a week-long process => there are probably minor errors and/or omissions. Please report any problems or suggestions for improvements; a robust and accurate document benefits us and future Rails developers.


GUI Tools For Windows Users

IDE: Most Mac users use Textmate. I've never tried it, but have tried a slew of Windows IDEs. In my opinion, Netbeans 6.0 is the hands down winner - a truly amazing piece of software. Do yourself a favor and check it out. (http://www.netbeans.org/)

MySQL: SQLyog has a free community edition. What's nice is that you can use it to interact with your online database. (http://www.webyog.com/en/)

Subversion: TortoiseSVN lets you issue Subversion commands via the Windows Explorer window. (http://tortoisesvn.tigris.org/)

WinSCP: Provides easier interaction with your server through file transfer, manager, and editing capabilities. (Description: http://en.wikipedia.org/wiki/WinSCP, Download: http://winscp.net/eng/index.php)


SSH, PuTTY, Pageant
You communicate with your server using SSH. Normally this requires firing up a PuTTY session (which looks a lot like a DOS box), logging in (by supplying a username and password), and issuing commands at the command line level.

PuTTY provides the ability to connect over SSH, while Pageant automatically supplies your username and password to applications that ask for it: PuTTY, WinSCP, Subversion, Capistrano. This is a nice feature, especially for what should be hard-to-remember passwords.

Michael Slater has an excellent article describing how to set up SSH, PuTTY and Pageant: http://www.buildingwebapps.com/articles/4-using-ssh-keys-to-speed-login

Regarding that article:

1) When creating a key in PuTTYgen you should fill in the "comments" filed with a meaningful name, e.g. "yourusername@yoursite.com"

2) The article says you should construct a .bat file for Pageant. When I did that I got a "Could not load this key (not a private key)" error message during boot up. I found that typing the command directly into the "Properties > Shortcut" tab of the "Startup Folder" entry solved the problem:
Target: "c:\Program Files\PuTTY\pageant.exe" my_key_filename.ppk
Start In: "c:\Prgram Files\PuTTY\my_key_directory"

3) I wasn't sure what the "$SVN_SSH" was for in the Subversion config file; regardless, entering "ssh = plink.exe" worked fine for me.


WinSCP
This GUI program beats using PuTTY. But oftentimes you need to fire up PuTTY to issue commands at the command line. No problem - there's a PuTTY icon in the toolbar (between the "Session" and "Options" menus).

You need to "Session > Create Session" and fill in the appropriate values for the "root" account set up for you by RimuHosting (Host name, User name, Private key file). Later you will add another session for a "normal" user that you will create (below).


Security Concerns
Many books and website forums will tell you to switch the SSH port from the default of 22. That sounds good, but the problem is that you are going to be using a fair number of tools, each of which assume that SSH is on port 22. If you switch the default you are going to have to figure out how to specify a new SSH port for each of the players in you deployment scenario - not a fun task.
I left the default alone and followed RimuHosting's advice: They recommended disabling password logins, allowing login only by SSH public/private key. This makes sense because most attacks are brute force affairs that just fire passwords at the default port, so if that port is not accepting password logins you should be fine.
So on your server:
Edit: /etc/ssh/sshd_config and set "PasswordAuthentication no" (Use WinSCP > Edit ; Comment/uncomment appropriate "yes" and "no" settings
Enter: /etc/init.d/sshd reload (Use WinSCP to fire up a PuTTY session and then type this command; this is so the change will take effect)


Shooting-Yourself-In-The-Foot Concerns
You should only log in as "root" when absolutely necessary because you can do a LOT of damage as "root" if you don't know what you're doing - which pretty much describes all of us Windows users.

In Linux, it seems that everything is permission-based, so a lot of command issuing is trial-and-error: if your request is not accepted as a "normal user" then log out, log back in as "root" and give it a shot. (I guess you can use the "sudo" command, but I found that once I set up my "normal" and "root" accounts in WinSCP that logging out and back in was simply a matter of clicking a few selections under the "Session" menu.)

To add a "normal" user, login as "root" and:
Enter: useradd yourname (Creates /home/yourname and some other stuff)

Now you need to create another SSH public/private key pair for this user. Or not...

When you boot up Pageant asks you for your password. If you add another pair it's going to prompt you for both. If you're a sole developer using one computer then you can use the same public/private key you generated for "root":

Enter: mkdir /home/yourname/.ssh
Copy: /root/.ssh/authorized_keys to /home/yourname/.ssh

RimuHosting probably added some public keys to your root's "authorized_keys" file so they can access your server if need be; you can remove these for your "normal" user, leaving only your public key. Now the same public/private key combination that was used to verify "root" will be used to verify "username", thus making Pageant and the programs that it feeds your private key to happy.

When you're done, create a WinSCP session for this normal user like you did for "root". Now whenever you fire up WinSCP you can choose to log in as either "root" or "yourname". And when you fire up a PuTTY session from WinSCP (via toolbar icon) you will automatically and correctly be logged in as either "root" or "yourname".


Take A Breather
You've accomplished quite a bit: when you boot your Windows machine Pageant asks for your password for SSH public/private key authorization. Once entered, you can fire up WinSCP and connect to your server. From there you can start a PuTTY session for entering commands at the command line.

And you've got two users (with their corresponding permissions) who can do this: "root" and "yourname". Good stuff!

Now it's time to get your source code into Subversion. A piece of advice: perform all of the following steps not with your real app but with a starter app. Rails makes it painfully easy to generate a simple starter app, so fool around with this and a simple starter database so you don't screw up your existing code and/or data. Then, when you've got the sample app up and running, it will be a piece of cake to repeat the steps for your real application. (Okay, so maybe it won't be *that* easy, but it will definitely be eaiser the second time around!)

For the instructions that follow my Rails web app will be referred to as "sample".