Deployment Using Mina

Got a nice web app and want to deploy and you have no clue about deployment of a rails app.. well that’s exactly me 2 days ago… here I’m sharing what I have learnt

Assumptions

  1. You have a working app called your_rails_app.
  2. You have access to a linux server with a working nginx server on 27.128.129.130.
  3. Your app is also on github.

Introducing Mina

We are going to use mina gem for deployment so add mina gem to Gemfile. I used latest code in github, the reason was the current version 0.2.1 has some issues (after entering the user name for connecting to server, there was no response) so it should be like this in your gem file

gem “mina”, :git => ‘git://github.com/nadarei/mina.git’
and run
bundle install

Configuration for mina
mina uses a configuration file for deployment, to create this file run

mina init

this would make a sample file under the config
config/deploy.rb

before changing the settings in the file connect to server and create a directory ready for your app

mkdir /home/user_name/sitedir

Note that the user which you are using to connect to the server should have full access to this folder so grant the permissions

chown -R user_name /home/user_name/sitedir

now we have to config mina to deploy our app to this folder. open config/deploy.rb
change the following lines according to your needs

set :domain, '127.128.129.130' 
set :deploy_to, '/home/user_name/sitedir'
set :repository, 'https://github.com/..."
set :branch, 'master'
set :user, 'user_name'
set :rail_env, 'production'

Mina manages your app in different versions and it uses a common log and config which is shared among these versions, the deployment script creates this file. we have to change it to use mongoid

queue! %[touch "#{deploy_to}/shared/config/mongoid.yml"

so ready to create our app’s structure? run

mina setup --verbose
# I always prefer to use --verbose switch to see whats happening

you may have noticed the message that tells you to change database.yml file. it’s because we have not changed the echo line to show mongoid.yml instead. change it if you like to!!
remember: we are using mongo so there would be no migrations comment this line
now if you look in the server side you would have 2 new dirs in your sitedir.
releases #holds your apps diffrent versions
shared #holds common features among your defferent versions. log and config are here

we are ready for first deploy. so run

mina deploy

Here you need to supply password for server.
if you are using a ssh handle for git you should configure the server to have access to your repository. I have used https address which in each deployment it asks for git user-name and password

Mina would invoke “bundle install “, if you are using rvm in the server this line would return an error
bundle: command not found
to fix this just uncomment this line in deploy.rb

require 'mina/rvm'

and invoke the ruby version in this line

# invoke :'rvm:use[ruby-1.9.3-p125@default]'

Now the rails app is up there int the server but first we should tell our nginx server how to look for our app and send the requests there , so open nginx configuration file: /opt/nginx/conf/nginx.conf
and add this lines to bottom of the file

server{
listen 80;
server_name myapp.com;
root /home/siva/mohammad/sitedir/current/public/; 
passenger_enabled on;
rails_env production;
}

there can be several sites listening on the same port
if you want your site to be the default loading site add default_server property to the listen attribute

listen 80 default_server;

save the file. note that this file is write protected and needs to be open as super user

restart nginx

./opt/nginx/sbin/nginx -s stop
./opt/nginx/sbin/nginx

so it’s possible to have multiple apps on listtening on one port so  you should configure your system to send the request properly. so open host file

/etc/hosts on  your local ubuntu system
%systemroot%/system32/drivers/etc on your windows system

and add the following line to the end of file

127.128.129.130 myapp.com

open your browser and point to myapp.com

there you go 🙂

5 thoughts on “Deployment Using Mina

  1. Awesome blog! Do you have any hints for aspiring writers?
    I’m planning to start my own website soon but I’m a little lost on everything.
    Would you recommend starting with a free platform like WordPress or
    go for a paid option? There are so many choices out there
    that I’m totally confused .. Any tips? Thank you!

    1. It really depend’s on your requirements and your level of expertise in web development. what do you need from a weblog. if it’s just a weblog then go for wordpress. but sometimes as a web developer you want to add the development of your own blog to your CV.

  2. Do you have a spam problem on this site; I also am a blogger, and I was wondering your situation;
    many of us have created some nice procedures and we are looking to
    trade methods with others, be sure to shoot me an e-mail
    if interested.

  3. A person necessarily help to make significantly articles I
    would state. That is the first time I frequented your web page and to
    this point? I amazed with the research you made to make this
    actual submit amazing. Fantastic task!

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.