June 15, 2012

Setting up a production server with ruby on rails (Deployment included) Part 3

Welcome to part 3 of setting up a production server with rails 3. In part 1 of this series we installed rvm, ruby, rails and setup a firewall. In part 2 of this series we installed mysql/postgresql, apache, and passenger. In part 3 of this tutorial we will install capistrano so we can deploy our application to our server.

What you will need

Make sure you have a git repository setup for your rails application so that you can deploy your app

Installing Capistrano

Now that our server is setup we can install capistrano. Logout of your server in go inside your local application folder. Once inside run

gem install capistrano

After you have installed the capistrano gem lets capify our rails project. This will create a couple of files for us but the one that we’re most interested in is the deploy.rb file.

capify .

Open the deploy.rb file and add in your server and git information.

set :application, "appname"
set :repository, "git@github.com:username/appname.git"

set :scm, 'git'

set :user, 'usernameyousetupforserver'
set :branch, "master"

set :git_shallow_clone, 1
set :use_sudo, false
set :deploy_to, "/path/public/#{application}"
set :deloy_via, :remote_cache
set :keep_releases, 1
set :rails_env, "production"
set :migrate_target, :latest

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

role :web, "xx.xxx.xx.xxx" # Your HTTP server, Apache/etc
role :app, "xx.xxx.xx.xxx" # This may be the same as your `Web` server
role :db, "xx.xxx.xx.xxx", :primary => true # This is where Rails migrations will run

 namespace :deploy do
   task :start do ; end
   task :stop do ; end
   task :restart, :roles => :app, :except => { :no_release => true } do
     run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
   end
 end

 namespace :deploy do
   desc "Recreate symlink"
   task :resymlink, :roles => :app do
     run "rm -f #{current_path} && ln -s #{release_path} #{current_path}"
   end
 end

After you have setup your information above run the following

  cap deploy:setup
  cap deploy
  cap deploy:check

The above will setup the necessary files then deploy your application and finally check to see if you’ve encountered any errors while deploying.

June 13, 2012

Setting up a production server with ruby on rails (Deployment included) Part 2

Welcome to part 2 of setting up a production server for your ruby on rails application. In part 1 we setup rvm, ruby, and rails. In Part 2 of this tutorial we will setup a mysql, postgresql, and finally install apache & passenger.

Installing Mysql

Since we have rails and ruby setup lets install mysql. If you would like to use postgresql skip this part in go down below.

sudo apt-get install mysql-server mysql-client libmysqlclient-dev`

Now install the mysql2 gem for rails

gem install mysql2

Installing Postgresql

Now that we have ruby and rails installed we can install postgresql. First lets install the python software properties.

sudo apt-get install python-software-properties

Next well add the backports and repo and update apt

    sudo add-apt-repository ppa:pitti/postgresql
    sudo apt-get update

Next we’ll install postgresql

sudo apt-get install postgresql-9.1 libpq-dev`

Apache & Passenger

Now that you have your database setup we can finally install Apache and passenger which is what we’ll be using to power our server.

    sudo apt-get install apache2 libcurl4-openssl-dev apache2-prefork-dev \
                     libapr1-dev libaprutil1-dev

Next we will install the passenger gem

gem install passenger

Now we’re ready to install the Passenger Apache2 module which will setup our server files etc.

passenger-install-apache2-module`

After the installation is complete you will need to add in the three lines of code from the installation into your passenger.load file. After you have pasted the code into the file press cmd + x and Y to save the file.

sudo vim /etc/apache2/mods-available/passenger.load

Here is the 3 lines of code. Do not copy below, just for example

  LoadModule passenger_module /home/yourusername/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.5/ext/apache2/mod_passenger.so
  PassengerRoot /home/yourusername/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.5
  PassengerRuby /home/yourusername/.rvm/wrappers/ruby-1.9.2-p180/ruby

Now lets enable the passenger module

sudo a2enmod passenger

Next we will setup a virtual host for our website.

sudo vim /etc/apache2/sites-available/appname

Then well add our server and site information into the file.

      ServerName xx.xx.xx.xx
      DocumentRoot /home/yourusername/appname/public

        AllowOverride all
        Options -MultiViews

Now lets disable the default Apache page and enable our website

    sudo a2dissite default
    sudo a2ensite appname

Now reload your web server and go to your ip and you should see a blank white screen.

sudo /etc/init.d/apache2 reload

In part 3 of this tutorial we will deploy our rails application with capistrano.

June 11, 2012

Setting up a production server with ruby on rails (Deployment included) Part 1

When first creating a ruby on rails application I thought about going with a service like heroku. Yes heroku is great since they manage your server but the prices and addons can get very expensive. If you’re a developer who is not scared to dive deep into the command line then look no further. In this tutorial I am going to share with you all how to setup your very own production server with rails 3.

What well be using

  • Ubuntu 10.04
  • RVM
  • git
  • Ruby 1.9.3
  • Rails 3.2.4
  • Mysql / Postgresql

What you will need

Setup server

Now since you are aware of the tools that we’ll be using, lets begin setting up our server from scratch. When you are all signed into your hosting account setup a new ubuntu 10.04 lts(Lucid Lynx) server. If you are using rackspace then I would start with the 256mb of ram(you can scale if needed). After you have picked your server and have setup a root password we will begin logging into the server via ssh and the terminal.

Open your terminal and type root@ your ip address for your server. A prompt should ask if you would like to save key. Yes is the answer.

ssh root@xx.xx.xxx.xxx

Now that your logged into the server lets create a new user so that you are not logged in as root for everything. It will ask for you to enter a password for that user. Enter the password then press enter to skip full name etc.

adduser yourusername

Now that our user account is setup we need to allow our user to install programs but not as root. Open the sudoers file.

sudo vim /etc/sudoers
    # User privilege specification
    yourusername ALL=(ALL) ALL

Now that we have our user created(though we’re still logged in as root) we can stop people from trying to brute-force our root password and take over out account.

vim /etc/ssh/sshd_config

Look for the line that has "PermitRootLogin" and change it from yes to no. Now lets restart the server.

/etc/init.d/ssh restart

Then logout of your server as root

logout

Now that we are all logged out, lets login using our new user credentials. Connect to the server using ssh

yourusername@xx.xx.xxx.xxx

Installing rvm

After successfully logging in as the new user let;s update the software on the server.

    sudo apt-get update
    sudo apt-get upgrade

Now we’ll install git so that we can install rvm, ruby, and other dependencies that we may need.

sudo apt-get install git-core

Next we’ll install rvm

    gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
    \curl -s

Then run source so we will not have to exit terminal.

source ~/.rvm/scripts/rvm

Type code below, if working correctly it will return "RVM is a function"

type rvm | head -n1

Now that rvm is setup let’s install ruby dependencies

#!/bin/bash

###### CONFIG
ACCEPTED_HOSTS="/root/.hag_accepted.conf"
BE_VERBOSE=false

if [ "$UID" -ne 0 ]
then
 echo "Superuser rights required"
 exit 2
fi

genApacheConf(){
 echo -e "# 
}
    sudo apt-get install build-essential bison openssl libreadline6 \
                     libreadline6-dev curl git-core zlib1g zlib1g-dev \
                     libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev \
                     sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev \
                     ncurses-dev

Installing ruby & rails

Now we’re ready to install ruby. This will take a while

rvm install 2.2

Now set ruby 2.2 as default

rvm --default use 2.2
gem install rails -v4.2.4

Before moving on make sure ruby and rails are installed in that you’re using the correct version and that they are both installed.

    ruby --version
    rails --version

Adding a firewall

Now lets install a firewall so we’re not easily venerable to outsiders. For this tutorial I will use Uncomplicated Firewall – UFW, which is a firewall for our ubuntu server.

sudo apt-get install ufw

Now lets setup the rules for our firewall

    sudo ufw default deny
    sudo ufw allow ssh
    sudo ufw allow http
    sudo ufw enable

Now lets check our rules to see if port 22, and 80 are open to the internet

sudo ufw status

In Part 2 of the next tutorial we will setup mysql, apache, and get our web server up and running.