Archive for the ‘Amazon EC2’ Category

A Step by Step Guide to Setup Rails application on Ec2 instance (Ubuntu Server)

Sometimes the Bitnami or other Rails AMIs doesn’t fit your needs directly and you will feel the need of building the Server yourself.Here I go step by step in building such a stack on top of Amaon EC2 Ubuntu Server.
  • Rails applications are a little bit different to install on servers but the process is very easy.Rails application needs a web server and an application server to run with. For development, it comes with default Webrick server that serve as application server on local machine. For setting it up on production server, we have the following choices on Web and application servers :-

    • Web servers

      1. Apache

      2. Nginx

    • Application Servers

      1. Passenger

      2. Thin

      3. Puma

      4. Unicorn

  • The simplest and best combination consists of Nginx + Passenger. It allows greater flexibility for configuration and also allows good speed over other combinations. So we are going to setup an Rails application using Nginx + passenger configuration on a bare Ubuntu server. Here are the steps :-

  1. Launch an Ec2 instance with ubuntu AMI. Make sure you have HTTP and SSH access to the server.

  2. SSH into the server by using private key (.pem) used while launching the instance and install the available updates by running :-

    sudo apt-get install updates
  3. Now you need to setup ruby on your server, so install the single user rvm ruby by following this blog.
  4. Load the rvm and make the installed ruby as default by running the following commands :-

    source ~/.rvm/scripts/rvm
    rvm use 2.1.0 –default
  5. Install the version control to clone your rails application to server. We generally use Git with rails application which can be installed by running the following command :-

    sudo apt-get install git
  6. Now clone your application on the server :-

    git clone yourepo.git

    Note:- In case of private git repository, you need to add public key of server to deploy keys of your repository, otherwise you will be promped with an permission denied error.

    OR

    Deploy using application to this server using Capistrano script. Please read this blog for more details on deploying your application using Capistrano.

  7. Now go to your application and install the gems by running bundle install command. If you want to setup your database on the same server, you can do the same by using the following commands 😐

    • In case of MYSQL

      sudo apt-get install mysql-server mysql-client
      sudo apt-get install libmysql++-dev
    • In case of POSTGRESQL, follow this blog for installation and then install the development headers

      sudo apt-get install libpq-dev

      After setting this up, migrate your databases in whichever environment you want to launch the server.

  8. Now install the Passenger gem by running :-

    gem install passenger

  9. Next step is to install the Nginx server, but we have some pre-requisits for this.

      1. It needs curl development headers which can be installed by :-

        sudo apt-get install libcurl4-openssl-dev

      2. It will be installed under /opt directory and your user should have permissions to that folder, so make your user as user owner for /opt directory by :-

        sudo chown -R ubuntu /opt

  10. Now install the Nginx server with passenger extension by running the following command :-

    passenger-install-nginx-module

  11. Set your Nginx server as service in init script by using the following commands :-

    wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
    sudo mv init-deb.sh /etc/init.d/nginx
    sudo chmod  +x /etc/init.d/nginx
    sudo /usr/sbin/update-rc.d -f nginx defaults
  12. Setup your application path in the nginx configuration file i.e. /opt/nginx/conf/nginx.conf.

    server {

    listen 80;

    server_name localhost;

    root /home/ubuntu/my_application/public #<-- be sure to point to 'public'

    passenger_enabled on;

    rails_env production;

    }

  13. Lastly start your server by running the following command :-

    sudo service nginx start

     


Attaching an EBS volume to Amazon EC2 instance

The EC2 backed Amazon’s instances come with a limited storage space by default(8GB to be specific mounted on /). However, some applications may require much more instance space. This can be accomplished by attaching an EBS storage to the your EC2 instance. Attaching extra storage on EC2 instance is pretty easy and you got to follow a few very simple steps:
  • Allocate a new EBS in your amazon AWS EC2 dashboard. The option comes under the heading volumes in the major head Elastic Block Storages. You will be asked for the size you want to allocate. The availability zone must be the same as the Amazon EC2 instance’s zone to which you want to connect this volume to.
  • In the list of volumes, attach the newly created EBS volume to the desired instance(As you select the volume using the checkbox, an option will appear on the top to attach it to an instance).
  • Now connect to your instance using ssh and run sudo fdisk -l. In the meantime do checkout our latest SIP Calculator fdisk will show the list of volumes and your newly created volume should appear unpartioned in this list. Format the file system /dev/xvdf(ubuntu’s external name to your drive) using the following command:
    sudo mkfs.ext4 /dev/xvdf
  • Mount the volume onto the system and add it to /etc/fstab for mounting at the system start(Here we assume that you will mount to /vol, this can be any directory that you like):
    sudo mkdir -m 000 /vol
    echo "/dev/xvdf /vol auto noatime 0 0" | sudo tee -a /etc/fstab
    	sudo mount /vol 
  • Thats it, enjoy your extended partition !!

Using Amazon SES for email delivery in Rails 3

Recently, Amazon added a new product to their cloud services kitty called as Amazon SES(Amazon Simple Email Service). The service can be used for bulk and transactional emails by developers and businesses alike. So as you might have guessed by now, the service competes head on with Postmark, Sendgrid, socketlabs etc. The simple reason for scrapping the said services and going ahead with Amazon SES is the price, thats it !! Now, lets dive deep into how to get Amazon SES running with a simple web application(We will be using rails3 as an example client application in this writeup): Setting up Amazon SES for delivering emails 1. Apply for the activation of SES in your amazon AWS account. If you are an existing AWS user, you will already be getting Amazon marketing promos to activate SES. Or you can sign up for ses here. 2. By default Amazon SES will be activated in a sandbox mode. This means that all the email addresses that you will be sending mails to, will need to be verified otherwise the email wont be delivered. Also, with verified email addresses, you can send up to 200 messages per day, at a maximum rate of 1 message per second 3. To get your email addresses verified, download Amazon SES scripts 4. To run the SES verification script, Perl must be installed on your system with the dependent modules. 5. Run the following command: ses-verify-email-address.pl -k creds.txt -v Amazon will send an email at the specified address. Please click the link in that email and you are done with the verification process. You are now done with verifying this email address. Please note that creds.txt here contains the AWS credentials. A sample creds.txt file looks like: AWSAccessKeyId= your_access_key AWSSecretKey= your_secret_key 6. After testing your emails in the sandbox, you can click here to Request for Production Access to Simple Email Service. They say it may take up to 24 hours, but my access request was granted in about 30 minutes – now I can send to any address! This was all about setting up Amazon SES. Now lets move to integrating the setup with a Rails 3 application. Configuring a Rails 3 application to send mails through Amazon SES: There are already a number of open source gems/plugins available to configure action mailer to send email thro Amazon SES but we decided to go ahead with drewblas/aws-ses (the aws-ses gem) for a simple reason that it works painlessly. Now: 1. Add ses to the gem file
gem "aws-ses"
2. Extend ActionMailer in config/initializers/your_config_file.rb, where config_file.rb is the name of the file which contains initialization routines for ses gem:
ActionMailer::Base.add_delivery_method :ses, AWS::SES::Base,
  :access_key_id     => ENV['AMAZON_ACCESS_KEY'],
  :secret_access_key => ENV['AMAZON_SECRET_KEY']
3. Configure action mailer to send thro ses. In config/environments/*.rb
config.action_mailer.delivery_method = :ses
Thats it !! You are now ready to send mails from rails app thro Amazon SES. Happy Mailing !!

Setting up Rails 3 stack on an Amazon EC2 instance

Recently, we had an encounter with Rails 3 on EC2 for one of our esteemed clients. The client wanted to host his application on Amazon EC2(and rightly so given that its such an amazing infrastructure at such an affordable cost). After searching the public AMIs, we could not come out with an AMI that had all our requirements satisfied(Rails 3, Postgres, phusion passenger). This lead us to trigger our sys admins to prepare the bare minimum EC2 AMI with the required development stack. We documented the steps if they can be helpful to someone. 1. Choose a bare minimum AMI, we chose centos 5.5 based 64 bit AMI. 2. download the keypair and log into your server with ssh. 3. You need to be root to perform the following steps, so if you log in with ec2-user type in the following command to become the root user:
sudo su -
Install Ruby and Rails 3:
  1. The amazon machine instance(AMI) comes with ruby 1.8.7 installed as of today.
  2. Rails 3 needs ruby 1.9+ to work.
  3. Ruby 1.9.1 is a buggy version so we will go ahead and install ruby 1.9.2
  4. We could not find rpms of ruby 1.9.2 on centos repos or even on rpmforge, so we had to go ahead and install ruby from source.
  5. Download ruby sources from http://www.ruby-lang.org/en/downloads
  6. wget ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.2-p136.tar.gz
  7. unpack it !!
    tar xzvf ruby-1.9.2-p136.tar.gz
  8. Install development tools.
    • yum groupinstall ‘Development Tools’
    • yum install readline-devel
  9. Move to ruby sources and compile
  10. cd ruby-1.9.2-p136
    ./configure
    make && make install
    
  11. Sit back and relax. Ruby will take some time to compile. It took about 7-9 minutes for us on EC2 micro instance
  12. After you are done with ruby install. Install rails.
    gem install rails
Install Apache and Passenger
  • Install Apache 2.
    yum install httpd
  • yum install httpd-devel
  • Install passenger.
    passenger-install-apache2-module
  • In case you get the following error,
    To install OpenSSL support for Ruby:
  • Please (re)install Ruby with OpenSSL support by downloading it fromhttp://www.ruby-lang.org/. Go to ruby source directory and install openssl extension.
Configure Passenger
Configure passenger. Depending upon your passenger version you would need to add following lines to your httpd.conf file. (Please note that these directives would come for you after the passenger install, you just need to copy and paste from there). For me the passenger version was 3.0.2:
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2
PassengerRuby /usr/local/bin/ruby
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2
PassengerRuby /usr/local/bin/ruby
Create virtual host configuration:
<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!
<Directory /somewhere/public>
AllowOverride all              # <-- relax Apache security settings
Options -MultiViews            # <-- MultiViews must be turned off
</Directory>
</VirtualHost>
Install postgres
yum install postgres
Thats it !! We are all done with our awesome rails 3 stack on Amazon EC2 !!
Please feel free to post any queries in the comments.