Deploying Rails Application on Amazon Ec2

Hello guys

Posting after a long time. But any ways below are the steps if you want to deploy a rails app on ec2 directly without using any 3rd party service providers

All the details mentioned below after for ubuntu so path might vary for other Linux flavours.

step 1 = Set java home. Ofcourse you need to have java installed and I assume that you already have it installed.

JAVA_HOME=/usr/lib/jvm/java-6-openjdk/
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH

Step 2 = login to amazon aws go to the account tab and download security credentials.  You will see 2 things

1) private key 2) certificate

Download them and store it on some path like /home/dhaval/cert or whatevery

Than export the keys and set it up with the following commands

export EC2_PRIVATE_KEY=/home/dhaval/cert/pk-2spdXHCQ4HDMJNCULJB5NA4JNLE7SWOR.pem
export EC2_CERT=/home/dhaval/cert/cert-2spdJXHCQ4HDMJNCULJB5NA4JNLE7SWOR.pem

Step 3 = Download api tools available on aws site

http://aws.amazon.com/developertools/351?_encoding=UTF8&jiveRedirect=1

export it and set the path

export EC2_HOME=/home/dhaval/cert/ec2-api-tools-1.3-62308
export PATH=$EC2_HOME/bin:$PATH

Step 4 = set up ssh keys using the following commands

ec2-add-keypair gsg-keypair

Now save this generated kaypair in ~/.shh folder with name id_rsa-gsg-keypair

Step 5 = Start an instance using the command below

ec2-run-instances (ami instance name) -k gsg-keypair

Step 6 = Check status of the instance

check status with

ec2-describe-instances (instant name)

eg.

ec2-describe-instances i-10a64379

Step 7 = Authorization of network ports using commands as below

ec2-authorize default -p 22
ec2-authorize default -p 80

Step 8 = Connect to your instance using the command below

ssh -i id_rsa-gsg-keypair root@machine_name

Thats all once these steps are done you can see the instance running on the aws console.

Than install ruby gems + rails + other required gems and packages and you are set to go.

In order to upload your files you can use Capistrano or also you can ssh through ftp using the key specified.

Hope this helps. If you are stuck at any step do feel free to ask question and I shall get back to you.

Njoi

Be Sociable, Share!

3 Step Solution to Missing rs-ruby error

Today we were trying to move one project of rails from one pc to the other. We took the source code along with the db (since the db had data in it and it was required) and configured it. But when we started the server using ruby script/server we got a strange error missing rsruby

searched for it and tried few things. Finally got it solved in 3 steps

>export R_HOME=/usr/lib/R
> sudo ln -s /usr/lib/R/lib/libR.so /usr/lib/libR.so
> gem install rsruby — –with-R-dir=$R_HOME –with-R-include=/usr/share/R/include/

Thats all… rsruby will be installed on your system and the project will be running without any issue.

Hope this works for you

Thanks

Be Sociable, Share!

Handling cookie overflow problem in rails

Are you getting cookie overflow error when you are loading a lot of flash[:notices] ??

Something like CGI::Session::CookieStore::CookieOverflow

if so here is a solution for it

Just write this method in ur controller

before_filter :session_cleanup

def session_cleanup
backup = session.data.clone
reset_session
backup.each { |k, v| session[k] = v unless k.is_a?(String) && k
=~ /^as:/ }
end

What this will do is it will clear old data thats already there in the session and restore this one.

The above method wont work if your data size is >= 4k in that case the only solution is to use another session store.

Hope this helps 🙂 … Njoi

Be Sociable, Share!

Read XML file using Hpricot in Rails

Here is a small post on how to read an xml file using Hpricot in your Rails application.

First of all you need to install hpricot with the following command

gem install hpricot

Here is a sample xml file which I want to read. You can just paste the following code and create .xml  file

<profiles>

<profile>
<name> test </name>

<user_ids>
<user_id>39</user_id>
</user_ids>
</profile>

<profile>
<name> test 123</name>

<user_ids>
<user_id>39</user_id>
<user_id>46</user_id>
</user_ids>
</profile>
</profiles>

Now here comes the code to read this file. Don’t forget to add require ‘hpricot’ in the controller where you are putting this code.

test = Hpricot::XML(File.open(“test.xml”, “r”))

(test/:profile).each do |pro| render :text=>(((pro.at(“name”).innerHTML.strip).to_s)+”=>”+(pro.at(“   _id”).innerHTML.strip).to_s).inspect and return false
end

Remove .inspect and return false once u get the required output

Hope this helps. If you have any doubts just post a message and I will try to reply.

Be Sociable, Share!

Setting up FTP or SFTP server using vsftpd on ubuntu

Recently I was given a task to configure FTP server so that the user can upload files through FTP. I found VSFTPD really a good solution for it. Its very simple to configure Here are the steps

1) Install vsftpd using the following command

sudo apt-get install vsftpd
2) On doing so it will get installed in /etc path by default. To confirm the same go to /etc/vsftpd.conf using

sudo nano /etc/vsftpd.conf

Now if you want to allow local users to login find local_enable=YES  and uncomment it

To disable anonymous ftp change anonymous_enable=YES to anonymous_enable=NO

Uncommenting the line (write_enable=YES) will let ftp users upload content to the server. (Thats what I wanted to do)

Thats all its done. Restart the server using

sudo /etc/init.d/vsftpd restart

This is how you will be able to allow ftp login.

But if u have many sites hosted on a server and you want to allow to access only one particular site to a certain user you need to do the following

1) create groups for specific sites using

groupadd site1
chgrp -R site1 /var/www/site1

2) Add user to that group

useradd -G site1 username

3) Give permission to a particular group

chmod -R  g+w /var/www/site1/*

I think thats all. You should be able to access your server using ftp and even upload content on it.

Oh yes one more thing was I wanted the user to go directly to the folder where i wanted him to upload the file. So for that you need to change the root path/home path. For this u need to open passwd file in /etc using

nano /etc/passwd

Looks for the ftp access and the username You will find the current path. In most cases it will be /home change that to your preferred one.

Hmm Thats it..

Be Sociable, Share!

Deploy or Configure Rails on a Ubuntu Server

I have configured many servers till now on different OS. Like CentOs, Fedora and Ubuntu. From all the Operating systems Ubuntu is my favorite. The reason is its rails friendly according to me. Also Fedora is also good but I like Ubuntu more might be just bcoz I am more used to it 🙂

Talking about configuring rails app on an Ubuntu server takes following steps on a plain slice

1) Step 1 will be to install basic ruby gems and other packages

If its a blank slice you might also need to update ur repos using

apt-get update and aptitude install build-essential before u start installing ruby and other required libraries

sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby sqlite3 libsqlite3-ruby1.8.

2) Create simlinks

sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby
sudo ln -s /usr/bin/ri1.8 /usr/bin/ri
sudo ln -s /usr/bin/rdoc1.8 /usr/bin/rdoc
sudo ln -s /usr/bin/irb1.8 /usr/bin/irb

3) Install Ruby gems (Make sure you have the latest version from http://rubyforge.org/frs/?group_id=126

We will use the 1.3.2 version. Get the tar version from http://rubyforge.org/frs/download.php/55066/rubygems-1.3.2.tgz using wget http://rubyforge.org/frs/download.php/55066/rubygems-1.3.2.tgz.

Then do  tar xzvf rubygems-1.3.2.tgz

cd  rubygems-1.3.2

sudo ruby setup.rb (to install the setup)

then do simlink sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

4) Then install rails (most important :D)

gem install rails -v 2.2.2 (make sure u specify the version u want to install else it will install the latest one)

5) Next step will be database. I use MySql and so here is the procedure  for it

sudo aptitude install mysql-server mysql-client libmysqlclient15-dev

You will be prompted to enter password 2-3 times while the installation is on. Don’t forget to setup a password.

6) Then comes one important part which is mysql-ruby library. Most people forget this and then MySql don’t work for them.

sudo aptitude install libmysql-ruby1.8

I think that’s all for Ruby + Rails + Ruby gems + Mysql

7) Since you are using Ubuntu make sure u have installed all the essentials things using

aptitude install build-essential.

This will install cc, gcc and other core required libraries. which will help u in installing gems and configuring them. (if you haven’t done that before)

8 ) Like I said before you will now also require gem. And the most common one is ImageMagick and Rmagick you can install them using

sudo apt-get install imagemagick

sudo apt-get install libmagick9-dev

sudo gem install rmagick

These 3 steps works in most cases. If it doesn’t work for you let me know via comment and I shall help u in solving it. You may also visit my previously written blog post on http://blog.dhavalparikh.co.in/2008/02/rmagick-installation-on-ubuntu/

9) Some other important ones are capistrano and git which u might require to install. Not covering them as of now.

10) You might also require nginx for webserver (I use Nginx so I am writing about it).

sudo aptitude install nginx

This will install nginx. For more information about nginx and its configuration you may visit my previous posts on

http://blog.dhavalparikh.co.in/2008/11/nginx-configuration-expiry-headers-and-gzip-component-with-rails-nginxconf/

That’s more than what you need to run a basic rails apps. But I think these are essentials for a good rails app deployment.

You can even read more about Mongrel Clustering with Nginx on my posts which could be again a part of server configuration

http://blog.dhavalparikh.co.in/2008/11/mongrel-clustering-with-rails/

I think thats all. If you have any doubts or problems write me a comment and I shall reply u ASAP.

Be Sociable, Share!
1 3 4 5 6 7 23