2011/07/06

Setting up Ruby on Rails 3 on a CentOS 5.4 VPS (or a tale of how a prototype turned into a specification and needed to be hosted)

For my work I had developed a prototype in Ruby on Rails to be able to do usability tests on the more dynamic parts of a new website.

However as the project progressed and the design was refined based on usability testing, it became much easier just to update the prototype rather than to change the original specifications.

As the project grew it became more and more cumbersome to keep everyone in the 40 plus people team up to date with the latest changes in the prototype and making sure that the team had a common vision of what the end product was supposed to look like. My only way to demo the site was to either take screenshots or run a server locally from my virtual machine running Ubuntu where I ran my development environment.

Running a Rails server centrally in a Microsoft centric organisation is easier said than done and I had to look outside the company for hosting providers. Eventually I settled for Hostingrails.com's virtual private server option (VPS for short, a virtual machine).

Purchasing the VPS subscription was quite easy and within a couple of hours I got an email with my credentials. But getting the server up and running turned out to be much more cumbersome than I originally imagined. I am by no means I Linux guru but I have spent enough time tinkering with various Linux and Unix distributions to not shy away from the task of configuring a server.

The server ran CentOS 5.4 and which is a Red Hat distribution sans the commercial parts. This proved to be the first hurdle: I was used to using fink on Mac OS X when installing the odd x11 application or apt-get on Ubuntu but yum (CentOs's frontend for the rpm package manager) or worse yet rpm were both new to me.

I started out using using a guide I found on assertivemagazine.com "Setup a Ubuntu VPS for hosting ruby on rails applications" but CentOS is no Ubuntu and that became quickly became abundantly clear to me. First off all the server didn't come with yum installed. Which meant that I had no "user friendly" front-end for package manager and that getting one (in this case yum) up and running was the first order of business. I had no choice but to get acquainted with rpm.

Let's walk through the process step by step (this post assumes basic knowledge of the vi editor for editing text files and I am not saying that this is the right way, it's just the way I got it working).

Step 1. Adding users and disabling root access with ssh

To add a user first log in to the machine using ssh (type: ssh root@) and once inside create a new administrator:
  1. Create a new user using: adduser
  2. Add an admin group using: groupadd admin
  3. Run visudo and give the admin group the right to run the sudo command by appending %admin ALL=(ALL) ALL
    to the end of the visudo file.
  4. Add the user you created to the admin group using usermod -a -G admin
  5. Log in as the user you created to make sure it works.
  6. Disable ssh access by root by making sure that the line beginning with PermitRootLogin is uncommented in /etc/ssh/sshd_config and set to: PermitRootLogin no
  7. Restart the ssd service using service sshd restart

Step 2. Getting yum up and running (getting acquinted with rpm)

Since yum wasn't installed from the get go I had to figure out how to install it using rpm. Luckily I found an older obsolete post that helped me to figure out what I needed to do "Install yum with rpm on CentOS". Based on the approach described there, an page about rpm and manually copying the links in the CentOS package repository (which had moved since the article was written) I figured out that the command I needed to run was the following:

sudo rpm -Uvh
http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/python-libs-2.4.3-43.el5.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/python-2.4.3-43.el5.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/python-elementtree-1.2.6-5.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/python-iniparse-0.2.3-4.el5.noarch.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/python-devel-2.4.3-43.el5.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/python-sqlite-1.1.7-1.2.1.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/rpm-python-4.4.2.3-22.el5.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/python-urlgrabber-3.1.0-6.el5.noarch.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/yum-metadata-parser-1.1.2-3.el5.centos.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/yum-fastestmirror-1.1.16-14.el5.centos.1.noarch.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/yum-3.2.22-33.el5.centos.noarch.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/rpm-libs-4.4.2.3-22.el5.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/rpm-4.4.2.3-22.el5.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/popt-1.10.2.3-22.el5.x86_64.rpm http://mirror.centos.org/centos-5/5/os/x86_64/CentOS/m2crypto-0.16-6.el5.8.x86_64.rpm


Step 3. Making sure that the system was up to date

Once yum is installed updating the packages is quite simple using: yum update.
To make sure you have the necessary tools installed run sudo yum groupinstall "Development Tools" which among other things installs gcc which is needed for the Rails installation.

Step 4. Installing what yum can't provide (git and sqlite)

Strangely enough yum didn't have a version of git in its repositories nor a recent version of SQLite, which both are needed for the Rails installation.

4.1. Installing Git
  1. Install the necessary libraries if they aren't already installed: yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel
  2. Download the git source code: wget http://kernel.org/pub/software/scm/git/git-1.7.6.tar.gz
  3. Untar the source code once the download is done: tar xvfz git-1.7.6.tar.gz
  4. Install the sourcecode by first moving to the directory where the files were expanded: cd git-1.7.6.tar.gz
  5. Then issue this command: make prefix=/usr/local all
  6. Last but not least run: make prefix=/usr/local install
  7. Check if git is working using: git --version
4.2 Installing SQLite
  1. Download the source using: wget http://sqlite.org/sqlite-amalgamation-3.6.23.1.tar.gz
  2. Unpack the it using: tar xvzf sqlite-amalgamation-3.6.23.1.tar.gz
  3. Move into the folder with the unpacked source: cd sqlite-amalgamation-3.6.23.1
  4. Run ./configure
  5. Make and install the package using: make && sudo make install
Step 5. Installing rvm and Ruby on Rails

Finally the last stretch of the journey. All that is left is to install rvm and rails:
  1. Download and install rvm: bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
  2. Add rvm to your path so that you can invoke it as a function by appending: [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
    to your .bash_profile
  3. Reload .bashrc for the changes to take effect by running: source .bashrc
  4. Test that rvm works by: rvm -v
  5. Install Rails using: rvm install 1.9.2
  6. Set it as default Ruby version using: rvm --default use 1.9.2
  7. Update the gems to make sure that you are up-to-date by running gem update --system and gem update
  8. Because of the non-standard sqlite3 installation you need to run the following when installing the gem: gem install sqlite3-ruby — –with-sqlite3-include=/usr/local/include \ –with-sqlite3-lib=/usr/local/lib
  9. Finally install Ruby on Rails by running: gem install rails
  10. Test that it works by running: rails -v
That's it.

Sources:
http://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-users-add.html
http://www.cyberciti.biz/faq/linux-sudo-allows-people-in-group-admin/

http://wiki.centos.org/HowTos/Network/SecuringSSH
http://kernel.org/pub/software/scm/git/RPMS/x86_64/
http://www.softpanorama.org/Utilities/rpm.shtml#Installation_Removal

http://www.how-to-linux.com/centos-52/install-git-161-on-centos-52/
http://tech.wangyaodi.com/2010/08/26/install-sqlite3-ruby-on-centos/