Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ZhouMeichen/6973719 to your computer and use it in GitHub Desktop.
Save ZhouMeichen/6973719 to your computer and use it in GitHub Desktop.

Deploy Rails Application (Nginx+Unicorn+MySQL) onto OpenShift

1) Create a DIY cartridge and add a MySQL cartridge

$ rhc app create -a [appname] -t diy-0.1
$ rhc cartridge add -a [appname] -c mysql-5.1

2) Download and install RVM for Openshift

$ rhc app ssh [appname]
$ cd $OPENSHIFT_DATA_DIR
$ curl -L https://raw.github.com/xiy/rvm-openshift/master/binscripts/install-rvm-openshift.sh | bash -s

1) Installation

$ declare -x RAILS_ROOT=~/app-root/repo/
$ cd $OPENSHIFT_DATA_DIR
$ git clone git://gist.github.com/2832578.git nginx-unicorn

$ chmod +x nginx-unicorn/install-nginx-unicorn.sh
$ nginx-unicorn/install-nginx-unicorn.sh

2) .openshift/action_hooks/start configuration

# make sure we can use rvm and bundle - you should have set up a gemset for your app already!    
source $OPENSHIFT_DATA_DIR/.rvm/scripts/rvm
rvm use 1.9.3-p125@gemsetname

# start the nginx http server
$OPENSHIFT_DATA_DIR/nginx/sbin/nginx

# start the unicorn backend server
unicorn_rails -E production -D -c ~/app-root/data/nginx-unicorn/unicorn.rb

3) .openshift/action_hooks/stop configuration

# kill nginx+unicorn through their pidfile
kill `cat $OPENSHIFT_REPO_DIR/tmp/pids/unicorn.pid`
kill `cat $OPENSHIFT_REPO_DIR/tmp/pids/nginx.pid`

4) .openshift/action_hooks/deploy configuration

pushd ${OPENSHIFT_REPO_DIR} > /dev/null
bundle exec rake db:migrate RAILS_ENV="production"
popd > /dev/null

5) nginx.conf configuration

# Modify data
gzip_types text/htm

# Add data
location ~* .(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js){
    root $OPENSHIFT_REPO_DIR/public
}

6) unicorn.rb configuration

7) nginx.conf test

$ sbin/nginx -t -c conf/nginx.conf

3. database.yml configuration

production:
encoding: utf8
reconnect: false
pool: 5
database: <%=ENV['OPENSHIFT_APP_NAME']%>
username: <%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>
password: <%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>
socket: <%=ENV['OPENSHIFT_MYSQL_DB_SOCKET']%>
host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>

4. Static assets precompile

1) Modify config/environments/production.rb

config.assets.compile = true

2) Rake

$ bundle exec rake assets:precompile RAILS_ENV=production
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment