Easy way to deploy Rails 4 application in Unicorn with Nginx

In this post I will show you how to host our rails application with unicorn and nginx

The first step is obviously to install nginx for that go to your control terminal then type

sudo apt-get install “nginx”

After the installation go to /etc/nginx/sites_enabled/ – (or wherever you installed nginx)

in this sites_enabled folder you have default.conf file delete that file and create new file called nginx.conf

server
{
listen 80 default;
#server_name example.com;
root /home/ideology/test_server_nginx/public;
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_pass http://localhost:3000;
}

error_page 500 501 502 503 /404.html;
}

save the file with above code

Now In your rails application folder gemfile.rb add gem “unicorn” and run bundle install

Then start your rails application with unicorn with port 3000

    /application_folder/unicorn -p 3000

Then you can seen your application in browser with nginx server at port 80

Thats all! very simple yes?……

To change environment to production

RAILS_ENV=production rake db:migrate

and then in your boot.rb file add this lines

require ‘securerandom’
ENV[‘SECRET_KEY_BASE’] = SecureRandom.hex

then start your rails application unicorn server

RAILS_ENV=production unicorn -p 3000

And thats all your environment is changed to production now ,

But this is just a fast start up only this is not the correct solution to host your application you need additionally unicorn configuration files for further reference http://unicorn.bogomips.org/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s