How to deploy a Ruby on Rails application
Knowledgebase Article
}
Knowledgebase Article
In order to deploy a Ruby on Rails application we need to enable Ruby using cPanel, please review the following article: Enabling Ruby in cPanel
In this example, we will cover the basics and teach you how to deploy Redmine as an example.
For those who are experienced deploying Ruby on Rails application, it worth mentioning that the web server is configured with Phusion Passenger. The Ruby Selector basically setups all the basics for you:
Using Ruby Selector we have added our application base folder:
Our application will use Ruby 2.4 and the root path will be /home/rubytest/myapps/redmine.
If go to that directory you will see that the selector creates by default three folders: tmp, public, logs.
All the application files that requires public access will need to be placed inside the public folder.
Redmine is a flexible project management web application. Written using the Ruby on Rails framework, it is cross-platform and cross-database.
After we press “Setup” the selector will create the application and provide us the command to activate Ruby:
Now, we will continue the installation via SSH. This is what we need to do:
SSH commands:
cd myapps
wget http://www.redmine.org/releases/redmine-3.4.5.tar.gz
tar xvfz redmine-3.4.5.tar.gz
mv redmine-3.4.5/* redmine
rm -f redmine-3.4.5.tar.gz
Redmine by default already has a "public" folder, therefore, moving everything inside our root directory is fine.
a) Create a MySQL database and assing a user
b) Go to myapps/redmine/ and copy config/database.yml.example to config/database.yml
c) Edit the file with your favourite editor and remove everything except production with adapter “mysql2”. Adjust the configuration with the database and user created on step “a”:
production:
adapter: mysql2
database: user_redmine
host: localhost
username: user_redmine
password: my_password
d) Now we need the GEM bundler. Bunlder help us to install all dependencies (GEMS) required by Redmine, specified on the file “Gemfile” at the root directory of our application. To install bundler, you need to activate ruby using the command provided by cPanel Ruby Selector and then install the gem. In our example, the commands would be:
rubytest@ ~/myapps/redmine/config $ source /home/rubytest/rubyvenv/myapps_redmine/2.4/bin/activate
(myapps/redmine:2.4)rubytest@ ~/myapps/redmine/config $ gem install bundler
Fetching: bundler-1.16.1.gem (100%)
Successfully installed bundler-1.16.1
Parsing documentation for bundler-1.16.1
Installing ri documentation for bundler-1.16.1
Done installing documentation for bundler after 7 seconds
1 gem installed
(myapps/redmine:2.4)rubytest@ ~/myapps/redmine/config $
e) Now we should be able to install all the dependencies. Run the following command from root directory of the application, where the file “Gemfile” exists. If it is not able to locate Gemfile, it will display an error:
bundle install --without development test
IMPORTANT NOTE: As other applications, like Magento for example, you have the possibility to setup modes, in this case Redmine offers “development” and “test”. Each mode, has it’s own dependencies, with the –without flag, we tell bundler not to install them.
f) This step generates a random key used by Rails to encode cookies storing session data thus preventing their tampering. Generating a new secret token invalidates all existing sessions after restart.
bundle exec rake generate_secret_token
h) You should have received the following error: “LoadError: cannot load such file — bigdecimal”
That kind of error message usually means that a file/module/gem is missing, in this case, it is reporting a problem with a missing gem call “bigdecimal”.
The first thing you would do is to run “gem install bigdecimal”. That’s one approach but doesn’t usually work, because when using bundler it seems to only consider the gems installed and activated by the Gemfile. The best and most efficient way to resolve it is by editing the Gemfile. Open the file and include:
gem "bigdecimal"
Now execute “bundle install” again as demonstrated previously to make sure bundler install the missing dependency.
Generate the secret tocken again. It should work this time.
i) Create the database structure, by running the following command under the application root directory:
RAILS_ENV=production bundle exec rake db:migrate
j) Insert default configuration data in database, by running the following command:
RAILS_ENV=production bundle exec rake redmine:load_default_data
That's all! Type your domain name in your web browser and should be able to see your Ruby on Rails application working.
Powered by WHMCompleteSolution