Skip to content
pekman edited this page Aug 23, 2011 · 21 revisions

The deployment system is implemented with Vlad. The script is in config/deploy.rb. This file contains server settings, deployment path and the git repository URL at the start. The script currently uses Ruby through rvm, which must be set up on the server for all users (in /usr/local/lib).

When everything is set up, the deployment is done with the following command:

rake vlad:deploy

Current settings (at Soberit)

You need a private ssh key to access the Amazon EC2 server (e.g. in ~/.ssh/aapps).

Ssh access to Github must be properly configured and the user must have reading rights to the repository. This includes setting your public ssh key as an allowed key in Github's admin panel. This key is separate from the key used with EC2 server. See Github's help pages for further instructions.

Ssh in the local computer needs to be set up to use the correct ssh key to when connecting to EC2 server. This can be done by adding the following lines to ~/.ssh/config:

Host 79.125.124.244
    IdentityFile ~/.ssh/aapps
    User aaltoapps
    ForwardAgent yes

Server setup

This guide is for Ubuntu 11.04 running in an Amazon EC2 server.

  1. Create a new user account. The application will run under this account and the account will be used for deployment. The following steps are done as this user. The user needs sudo privileges for this to work.

  2. Install the following packages (sudo apt-get install package1 package2 ...):

    • gcc
    • g++
    • make
    • automake1.9
    • git
    • postgresql
    • zlib1g-dev
    • libcurl4-openssl-dev or libcurl4-gnutls-dev
    • libssl-dev

    If some of the -dev-packages are not installed, ruby-build may silently skip building some of its core modules, which some gems may require.

  3. Install ruby-build:

    git clone git://github.com/sstephenson/ruby-build.git
    cd ruby-build
    sudo ./install.sh
  4. Install rbenv:

    cd
    git clone git://github.com/sstephenson/rbenv.git .rbenv
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> .bash_profile
    echo 'eval "$(rbenv init -)"' >> .bash_profile
  5. Install the latest stable Ruby under ~/.rbenv/versions/ and set it as the default ruby:

    ruby-build 1.9.2-p290 $HOME/.rbenv/versions/1.9.2-p290
    rbenv rehash
    rbenv global 1.9.2-p290
  6. Install Nginx and Phusion Passenger:

    gem install passenger --no-rdoc
    rbenv rehash
    sudo sh -c "PATH=\"$PATH\" passenger-install-nginx-module"
    • Create a directory for the application and give yourself write permissions to it:

      sudo mkdir -p /deployment_path
      sudo chown your_username:your_groupname /deployment_path

      In Ubuntu your_groupname is typically the same as your_username.

    • Edit Nginx configuration in file /opt/nginx/conf/nginx.conf. For basic installation replace server section with the following:

      server {
          listen 80;
          server_name domain.name.of.the.server alternate.domain.name ...;
          root /deployment_path/current/public;
          passenger_enabled on;
      }
    • passenger-install-nginx-module doesn't seen to create init script for Nginx, so we have to do it ourselves:

      1. Create file /etc/init.d/nginx as root with the following contents:

        #! /bin/sh
        
        ### BEGIN INIT INFO
        # Provides:          nginx
        # Required-Start:    $all
        # Required-Stop:     $all
        # Default-Start:     2 3 4 5
        # Default-Stop:      0 1 6
        # Short-Description: starts the nginx web server
        # Description:       starts nginx using start-stop-daemon
        ### END INIT INFO
        
        PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
        DAEMON=/opt/nginx/sbin/nginx
        NAME=nginx
        DESC=nginx
        
        test -x $DAEMON || exit 0
        
        # Include nginx defaults if available
        if [ -f /etc/default/nginx ] ; then
                . /etc/default/nginx
        fi
        
        set -e
        
        case "$1" in
          start)
                echo -n "Starting $DESC: "
                start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \
                        --exec $DAEMON -- $DAEMON_OPTS
                echo "$NAME."
                ;;
          stop)
                echo -n "Stopping $DESC: "
                start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
                        --exec $DAEMON
                echo "$NAME."
                ;;
          restart|force-reload)
                echo -n "Restarting $DESC: "
                start-stop-daemon --stop --quiet --pidfile \
                        /opt/nginx/logs/$NAME.pid --exec $DAEMON
                sleep 1
                start-stop-daemon --start --quiet --pidfile \
                        /opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
                echo "$NAME."
                ;;
          reload)
                  echo -n "Reloading $DESC configuration: "
                  start-stop-daemon --stop --signal HUP --quiet --pidfile     /opt/nginx/logs/$NAME.pid \
                      --exec $DAEMON
                  echo "$NAME."
                  ;;
              *)
                    N=/etc/init.d/$NAME
                    echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
                    exit 1
                    ;;
        esac
        
        exit 0

        (copied from http://library.linode.com/frameworks/ruby-on-rails-nginx/ubuntu-10.10-maverick)

      2. Give the file execute permission

      3. Run the command: /usr/sbin/update-rc.d -f nginx defaults

      4. Start Nginx: sudo /etc/init.d/nginx start

  7. Set up PostgreSQL database

    • Edit /etc/postgresql/8.4/main/pg_hba.conf as root and add the line:

      local   all         all                               md5
      
    • Restart PostgreSQL (sudo /etc/init.d/postgresql restart)

    • sudo -u postgres psql postgres:

      CREATE ROLE aaltoapps WITH LOGIN PASSWORD 'random_password';
      CREATE DATABASE aaltoapps OWNER aaltoapps;
      
      
  8. Set up Ruby on Rails

    • do the following on your computer (not on the server):
      1. Set up a development environment
      2. Make sure that settings in the file config/deploy.rd are correct and that you have ssh access to the server
      3. Run the following command in the development directory: bundle exec rake vlad:setup