Bare Ubuntu configuration for Ruby on Rails

General

Install Ubuntu 9.1 server using the Bare Server option

The first thing I like to do is add my account to the /etc/sudoers file. This just guarantees that I can always use sudo, even if I mistakenly mess up my group assignment.

sudo visudo
#  /etc/sudoers
root   ALL=(ALL) ALL
user ALL=(ALL) ALL

user should be replaced with the account you use to administer the box.  I like to do this because one time I messed up my group assignment on the only account that was able to admin the box.

alias sudo='sudo -E'

The reason we have that alias nonsense in there is because Ubuntu compiles the sudo command with the –with-secure-path option.  We want to pass our current path into the command.  The alias command is explained here.  I’m sure there are some security vulnerabilities with muti-user systems doing it this way, but I’m the only guy so no need to figure it out right now.

Next, update the apt-get configuration and then upgrade to make sure the latest packages are installed.

sudo apt-get update
sudo apt-get upgrade -y

Configure the server for static IP address.

SSH

Configure SSH.

Ruby

As I write this, the latest version of Ruby for apt-get is 1.8.7 patchlevel 174.

sudo apt-get install build-essential libssl-dev libreadline-dev rake -y
sudo apt-get install ruby rdoc ri rubygems libopenssl-ruby ruby-dev -y
I like to create a current symbolic link that links to the current gem so I can easily upgrade and not have to redo all my paths.
ln -s /var/lib/gems/1.8 /var/lib/gems/current

Now, source the file you just created.  Sourcing a file uses the contents of the file as if you typed them at the command prompt.  You just type a period followed by a space and then the filename.

. /etc/profile.d/gems.sh

You will notice that we did not install Rails.  This is intentional.  We will use

rake rails:freeze:gems

to freeze a known version of rails into our applications.

MySQL

sudo apt-get install mysql-server libmysqlclient-dev
sudo gem install mysql

Apache

We will install Apache2 as well as Passenger (mod_rails or mod_rack).

sudo apt-get install apache2 libapr1-dev apache2-prefork-dev -y
sudo gem install passenger
sudo /var/lib/gems/current/bin/passenger-install-apache2-module
After you run this, you will see instructions to add some lines to your Apache configuration file.  Mine looked like this:
LoadModule passenger_module /var/lib/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
PassengerRoot /var/lib/gems/1.8/gems/passenger-2.2.8
PassengerRuby /usr/bin/ruby1.8
LoadModule passenger_module /var/lib/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so   PassengerRoot /var/lib/gems/1.8/gems/passenger-2.2.8   PassengerRuby /usr/bin/ruby1.8

Just copy this to it’s own file in the conf.d directory of apache2.

sudo vi /etc/apache2/conf.d/passenger.conf

GIT

Install and configure GIT on the server and the client.

Configuring Ubuntu for a static IP address

The next thing we need to do is to set the network with your static IP addresses. First, edit the /etc/network/interfaces to include the following.

#   /etc/network/interfaces
 auto eth0
 iface eth0 inet static
 address xxx.xxx.xxx.xxx
 gateway xxx.xxx.xxx.xxx
 netmask xxx.xxx.xxx.xxx

This assumes you want the ethernet card on eth0 configured.  After this, we need to configure the primary and secondary DNS servers

#   /etc/resolve.conf
 nameserver xxx.xxx.xxx.xxx
 nameserver xxx.xxx.xxx.xxx

After these are set, you need to restart the networking

sudo /etc/init.d/networking restart

Using SSH on Mac to access Ubuntu server 9.10

First, make sure you can SSH to your server. If you need to install ssh on the Ubuntu server, use:

sudo apt-get install ssh
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

If you are wondering what the touch does, it just creates an empty file if it doesn’t exist.  That way i don’t have to write two sets of instructions based on whether you’ve ever had SSH configured before.  Next we are going to generate our keys and upload them to the server

ssh-keygen -t rsa
scp user@server.com:~/.ssh/authorized_keys ~/.ssh
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
scp ~/.ssh/authorized_keys user@server.com:.ssh/

What we are doing is copying the existing authorized_keys file to our local mac to add our key and then uploading it back.  We should now be able to login to our server using our new keys.

ssh user@server.com

The Mac will now prompt you for the password you used to create your ssh key.  Add it to your keychain.  From this point forward, you will no longer be prompted with your password.  Yay!

Creating a remote GIT repository

Clean, readable code is one of my highest goals when writing code.  I’m always thinking about the next guy, even when the next guy is just me, several months from now.   Nothing is worse than going into spagetti code and not knowing what the heck is going on.  Because of this, I like to refactor my code A LOT.  When you are using source control, you never get yourself into situations that you can’t get out of.  It’s also nice to go back and see how the code has evolved over time, to see why certain files were changed.  Because of all these benefits, I use source control even on my own tiny projects where I’m the only developer.

I have used visual source safe when I first got started and like all vss users, quickly moved away from it.  I’ve spent most of my career using perforce for work.  I tried out Subversion a long time ago, but I got tired of my database getting corrupted and having to drop to the command line to repair it.  I used a very early version of subversion and I hear that it is MUCH better now.  Anyway, I used perforce for my personal stuff because the first two clients were free.  Since I’m learning everything else, why not throw in GIT too.  It’s gotten a lot of press so I want to see what the fuss is all about.  Here is how I installed my first GIT repository on my ubuntu server.

ssh to the remote box and type the following commands:

sudo apt-get install git-core
sudo mkdir /var/git
sudo mkdir /var/git/snowcaptech.git
sudo chown jason /var/git/snowcaptech.git
sudo chmod 775 /var/git/snowcaptech.git
cd /var/git/snowcaptech.git
git init --bare --shared=group

What we are doing is creating an empty directory in the /var/git folder where I like to keep all my git repositories.  We are then changing the owner to me and setting permissions for me to write and everyone else to be able to read.  The git init –bare creates a repository without a working copy.

then to add a project from your local client to the remote repository, you would first tell GIT about your project by going to the directory of your project and from a command prompt typing:

git init
git add .
git commit -m "initial checkin"

This initializes your repository and commits it to your local GIT repository.  This is all you need to do to have a working source control system for just yourself.

I like to make my source available on a server so I can access it from anywhere on the planet.  I never need to do this, but it is cool to be able to do this.  In order to “push” your local GIT repository to the remote repository, you have to tell your local repository about the remote.  Sorry if that was confusing.  Read it a few times. :)

git remote add origin jason@dev.snowtech.com:/var/git/snowcaptech.git
git push origin master

As long as your settings are all correct, your repository should now be at the remote repository.