Deploy Rails 3.x Application

This document helps you quickly deploy Rails3.x-based applications to the cloud help. For Sinatra or other types of Ruby applications, please refer to Build Language-Ruby

Environment Settings

Basic Environment

This article uses Ubuntu 14.04.2 LTS system, root user operation, Rails uses version 3.2.3

Install git command line or git GUI software locally
$ apt-get update
$ apt-get install git
Install Ruby 2.0.0
$ cd /tmp/  && mkdir soft && cd soft
$ wget http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p645.tar.gz
$ tar xvzf ruby-2.0.0-p645.tar.gz
$ cd ruby-2.0.0-p645
$ ./configure --prefix=/usr/local/ruby
$ make && make install
$ export PATH=$PATH:/usr/local/ruby/bin/
$ echo 'export PATH=$PATH:/usr/local/ruby/bin/' >> ~/.bashrc
$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-linux]

# Set Taobao gem source
$ gem source list
*** CURRENT SOURCES ***

https://rubygems.org/

$ gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources

$ gem sources -a https://ruby.taobao.org/
https://ruby.taobao.org/ added to sources

$ gem source -l
*** CURRENT SOURCES ***

https://ruby.taobao.org/
Install Rails 3.2.3
$ gem install rails -v 3.2.3
$ rails -v
Rails 3.2.3

# Install bundle
gem install bundle

Create an Application

Create a New Application

$ mkdir -pv /app/ && cd /app/
$ rails new myapp --database=mysql

# Modify Gemfile, replace source with Taobao address
$ sed -i s'#rubygems.org#ruby.taobao.org/#g' Gemfile

# Regenerate the Gemfile.lock file
$ bundle install

Use Existing Apps

If you use an existing application, please make sure to use the mysql database.

$ grep mysql2 Gemfile

If there is no mysql2 mark, please add:

$ echo "gem 'mysql2'"  >> Gemfile

# Regenerate the Gemfile.lock file
$ bundle install

Database Configuration

When the application is deployed to Gridworkz, it is recommended to use environment variables for the configuration of the database, which can avoid the leakage of sensitive information and ensure the flexibility of the configuration. The following is an example, edit config/database.yml:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: myapp_development
  pool: 5
  username: root
  password:
  host: localhost

test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: myapp_test
  pool: 5
  username: root
  password:
  host: localhost

production:
  adapter: mysql2
  encoding: utf8
  reconnect: true
  pool: 5
  host: <%= ENV['MYSQL_HOST'] %>
  port: <%= ENV['MYSQL_PORT'] %>
  database: myapp
  username: <%= ENV['MYSQL_USER'] %>
  password: <%= ENV['MYSQL_PASSWORD'] %>

Please fill in the database configuration of development and test environment according to local actual situation

The environment variables starting with MYSQL_ of production are automatically generated after the cloud help is associated with the MySQL service.

The production database needs to be created in advance

Rails Plugin

In order for Rails applications to work better on the cloud, it is recommended to add the rails_12factor gem to modify the Gemfile file and add the following content:

$ echo "gem 'rails_12factor', group: :production" >> Gemfile
# Update dependent files
$ bundle install

Set Asset Pipeline

Since Rails 3.1 version, the concept of Asset Pipeline has been introduced, which provides a framework for connecting and compressing JavaScript and CSS files. It also allows other languages ​​and preprocessors to be used to write JavaScript and CSS, such as CoffeeScript, Sass, and ERB.

We recommend that users generate static resources of the online environment locally, which can speed up the time of online deployment. The local operation method is introduced below.

RAILS_ENV=production bundle exec rake assets:precompile
/usr/local/ruby/bin/ruby /usr/local/ruby/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
Compiled application.js  (0ms)  (pid 1601)
Compiled application.css  (0ms)  (pid 1601)
Compiled application.js  (0ms)  (pid 1601)
Compiled application.css  (0ms)  (pid 1601)

If you execute bundle exec rake assets:precompile, the following error is reported:

  • ExecJS::RuntimeUnavailable: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes
  • Just remove the # gem’therubyracer', :platform => :ruby comment in the Gemfile file, and then execute bundle install to install gem and update dependent files
  • RAILS_ENV=production bundle exec rake assets:precompile This command sets the RAILS_ENV environment variable to production, because the application deployed on the cloud will automatically set RAILS_ENV= production

In order to tell Gridworkz not to generate assets during the build process, you need to add the following configuration in the config/application.rb configuration file:

config.assets.initialize_on_precompile = false

Specify Ruby Version

In order to ensure that the offline and online ruby ​​versions are always available, the ruby ​​version requirements can be written in the Gemfile file, so that when the program is deployed in the cloud help, the specified version of ruby ​​will be installed according to our requirements

$ echo "ruby '2.0.0'" >> Gemfile

# Regenerate dependent files
$ bundle install

Click on the Ruby language overview to view the ruby ​​version supported by Gridworkz

Configure Operation Mode

By default, rails uses webrick as the web server on the platform, but webrick is only used for the development environment. For questions, see: rails default web server. We recommend using Puma as the web server. You can refer to the documentation of using Puma to deploy Rails applications. Here is how to use Puma:

  1. Install puma
echo "gem 'puma'" >> Gemfile

#Install puma
bundle install
  1. Place puma
touch config/puma.rb

vi config/puma.rb

Paste the following part into the config/puma.rb file

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)

# If the program is not thread-safe, you need to set threads_count to 1
# threads_count = 1

threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port ENV ['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Valid on Rails up to 4.1 the initializer method of setting `pool` size
  ActiveSupport.on_load(:active_record) do
    config = ActiveRecord::Base.configurations[Rails.env] ||
                Rails.application.config.database_configuration[Rails.env]
    config['pool'] = ENV['MAX_THREADS'] || 5
    ActiveRecord::Base.establish_connection(config)
  end
end

Writing Procfile

echo "web: bundle exec puma -C config/puma.rb" > ./Procfile

Deploy the Application to the Cloud

Create an Application

Please refer to the operation method: New Application-Source Code Construction

Submit Code

The last step of creating the application will give the application’s git code repository address, the submission user name is the cloud help login email, and the password is the platform login password.

The command to submit code under the command line:

$ cd / app / myapp
$ git init
$ git add .
# Set git submission information, fill in here according to the actual situation of the user
$ git config --global user.email "demo@gridworkz.com"
$ git config --global user.name "demo"
$ git commit -m "init"
$ git remote add origin http://code.gridworkz.com/app/team name_application name.git
# git push origin master
Username for'http://code.gridworkz.com': Platform login email address
Password for'http://zyq916@gmail.com@code.gridworkz.com': platform login password
Counting objects: 68, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (52/52), done.
Writing objects: 100% (68/68), 82.08 KiB | 0 bytes/s, done.
Total 68 (delta 2), reused 0 (delta 0)
To http://code.gridworkz.com/app/ team name_application name.git
 * [new branch]      master -> master

One-click Deployment

After creating a new application, follow the prompts to complete and click Create to automatically deploy.

View Log

During the deployment process, after the deployment is complete, you can enter Application Console-Logs to view log information during application operations.

Access the App

After successful deployment and startup, you can directly open the application through the “Access” button on the application overview page