Now From Home

Working from home

Ubuntu VPS With PostgreSQL 9.1 and Rails

Setting up the VPS

Once you get access to your server as root, the first step is to secure the access to it. The following steps are taken from the fantastic guides at Linode.com. This one is from Linux Security Basics.

I always create a new user called deploy or similar which I use for everything so…

1
2
~$ adduser deploy
~$ echo 'deploy ALL=(ALL) ALL' >> '/etc/sudoers'

The second command allows the user deploy to use the sudo command.

Lockdown ssh

First of all, be sure to have SSH keys generated in your local machine and in the server. Use //ssh-keygen// if not.

Copy your ssh keys to the authorized key on the remote machine:

1
2
scp ~/.ssh/id_rsa.pub deploy@hostname.com:/home/deploy/.ssh/uploaded_key.pub
ssh deploy@hostname.com "cat ~/.ssh/uploaded_key.pub >> ~/.ssh/authorized_keys"

On the remote machine:

Edit /etc/ssh/sshd_config file and set the following options:

1
2
PermitRootLogin no
PasswordAuthentication no

Now you can logout and login again using:

1
~$ ssh deploy@hostname.com

To fully update your server type (from this guide):

1
2
~$ sudo apt-get update
~$ sudo apt-get upgrade --show-upgraded

Set the hostname:

1
2
~$ sudo echo "serverhostname" >> /etc/hostname
~$ sudo hostname -F /etc/hostname

Install basic libraries in the server which are needed to install other packages from source:

1
2
3
4
~$ sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev
libcurl4-openssl-dev git-core zlib1g zlib1g-dev  libssl-dev
libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev
libxslt-dev autoconf libc6-dev curl

Installing PostgreSQL 9.1

I want to install the latest version of PostgreSQL, however, depending on your Ubuntu version, the following command will prompt you to install an older version of PostgreSQL:

1
~$ sudo apt-get install postgreSQL

In Ubuntu 10.04 LTS I get PostgreSQL 8.4 which I don’t want. In order to get the latest version we need to add a new PPA

To do so, you need to follow this guide Clik on the link Read about installing at the first paragraph.

The first method didn’t work me:

1
~$ sudo add-apt-repository ppa:pitti/postgresql

Unfortunately it complains that add-apt-repositorty is not found. I’ve read that this command is provided by the package phyton-software-packages but because I don’t want to install anything but the essential in the server I’ll update /etc/apt/sources.list manually.

Now you should be able to install PosgreSQL 9.1 successfully.

Once installed, you can connect using the user postgres, which is a Unix user and the superuser for PostgreSQL.

1
~$ sudo -u postgres psql

Installing RVM

Instructions taken directly from http://beginrescueend.com/rvm/install/. I’m using the isolated install for my user ‘deploy’ instead of a system wide instal. I don’t plan to have more users on the server and if something goes wrong I think it will be easier to fix it if the changes are limited to one user.

1
~$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

rvm should be now installed on ~/.rvm

Now modify .bash_profile to be able to run RVM

1
2
~$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
~$ source .bash_profile

Installing Ruby 1.9.3

1
rvm install 1.9.3

(this will take a while)

Set ruby 1.9.3 as the default Ruby:

1
rvm --default use 1.9.3

I don’t want to download and install documentation for my gems in the server so I like to create the file ~/.gemrc to prevent install gem documentation:

1
echo 'gem: --no-ri --no-rdoc' >> ~/.gemrc

Installing Passenger and Nginx

It seems that the installer breaks if you don’t install first PCRE. Hopefully this will be fixed, meanwhile:

1
sudo apt-get install libpcre3 libpcre3-dev

And then:

1
2
gem install passenger
rvmsudo passenger-install-nginx-module

Choose the default options

1
rvm wrapper 1.9.3@global passenger

Init script for Nginx so it is started with the server starts:

1
2
3
4
5
cd /opt
wget -O init-deb.sh http://library.linode.com/assets/601-init-deb.sh
mv /opt/init-deb.sh /etc/init.d/nginx
chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults

And that’s it, now we have the basics to serve a Rails apps.

Next steps which I don’t have a customized step-by-step guide:

Configure Nginx

Install a SSL certificate

Comments