Ruby

The Simple Hosting Ruby instance allows you to publish web applications written in Ruby easily and affordably.

Prerequisites

Users of Ruby Simple Hosting instances should have:

  • some knowledge of the Ruby language
  • some knowledge of git
  • an application compatible with Ruby (MRI) 2.4
  • an application compatible with Rack
  • an application that uses Bundler to manage dependencies

The following tools should also be installed locally on your computer:

  • ruby
  • bundler
  • git
  • SSH client

Directory structure

To be compatible with Rack, your application’s files must adhere to a specific layout, containing the following directories and files:

  • config.ru: A Rackup file like the one specified in the Rack documentation
  • public/: If you have any static files, they should be placed in this directory

Ruby on Rails applications (rails >2) should work out of the box.

Example file structure:

.
├── config.ru
└── public
    └── robots.txt

Installation of dependencies

In order for your Ruby application’s dependencies to be installed, you must declare them in a Gemfile file placed at the root of your project. This file must match the format indicated in the Bundler documentation.

Example Gemfile:

source 'https://rubygems.org'
gem 'rails', '~> 4.0.0'
gem 'debugger', group: :development

Note

During the installation of dependencies, the gems specified in the development and test groups will not be installed.

bundle install

Once your dependencies have been declared, you must generate a Gemfile.lock file by running the bundle install command on your computer.

This is the file that will be used to install the dependencies on your instance. It must therefore be included in your git tree and pushed along your application.

Domains

A domain name is automatically linked to your instance at creation time for testing purposes. You can link a number of additional domains and subdomains to your instance. All will use the same application code base.

If you would like different domains to route to different Ruby applications on a single Simple Hosting instance, then you will need to configure the routing yourself in the config.ru file.

A simple example:

map 'http://www.example.com/' do
     run Proc.new { |env|
        [200, {'Content-Type' => 'text/html; charset="utf-8"'},
             ['<!DOCTYPE html><html><meta charset="utf-8"><title>It works',
             "</title><b>It works!</b><br /><br />You've reached ",
             'www.example.com'
            ]
        ]
     }
end

map 'http://www.another-example.com/' do
     run Proc.new { |env|
        [200, {'Content-Type' => 'text/html; charset="utf-8"'},
             ['<!DOCTYPE html><html><meta charset="utf-8"><title>It works',
             "</title><b>It works!</b><br /><br />You've reached ",
             'www.another-example.com'
            ]
        ]
     }
end

Databases

Like the other Simple Hosting instance families, Ruby instances support three types of databases: MySQL, PostgreSQL and MongoDB.

PostgreSQL

PostgreSQL version 9.6 is available for Simple Hosting with Ruby.

Connection settings:

Hostname: localhost
Port: 5432

By default, you can use the hosting-db user without a password to connect to the default database, called postgres. We recommend that you create secure credentials for real-world usage.

You can create as many databases and database users as you need; you’re only limited by your disk space, which you can increase at any moment up to 1 TB.

You can manage your PostgreSQL database with the standard shell commands via the SSH console (psql, pg_dump, etc.), or from the Web admin panel with phpPgAdmin. Check out the reference article about PostgreSQL management on Simple Hosting to learn more.

Here’s an example config/database.yml configuration for Ruby on Rails that uses the default database.

production:
  adapter: postgresql
  database: ruby-example
  host: localhost
  port: 5432
  username: hosting-db
  password:
  encoding: unicode
  pool: 5

MySQL

MySQL 5.7 is available to use with Ruby instances on Simple Hosting.

Connection settings:

Socket: /srv/run/mysqld/mysqld.sock

You can connect to the MySQL database service through its Unix socket, available at /srv/run/mysqld/mysqld.sock. The default user is root and no password is required by default. However, you should create safe credentials for real-world usage. You can also use the default_db database for fast setups and testing purposes, as it is automatically setup when the instance is created.

You can create as many databases and database users as you need; you’re only limited by your disk space, which you can increase at any moment up to 1 TB.

You can manage your MySQL database with the standard shell tools (mysql, mysqldump, etc.) via the SSH console, or from the web admin console with PHPMyAdmin. Check out the reference article about MySQL management on Simple Hosting to learn more.

Here’s an example config/database.yml for Ruby on Rails with a MySQL database:

production:
  adapter: mysql2
  socket: /srv/run/mysqld/mysqld.sock
  database: default_db
  username: root
  password:
  encoding: utf-8
  pool: 5

MongoDB

MongoDB 3.2 is the version currently available for Ruby instances on Simple Hosting.

Connection settings:

Hostname: localhost
Port: 27017

The MongoDB database service is available on localhost at port 27017. No user and password are required by default, but you should create safe credentials for real-world usage.

You can create as many users and databases as you need; you’re only limited by your disk space, which you can increase at any moment up to 1 TB.

You can manage your MongoDB database from the standard mongo shell via the SSH console, or from the Web admin panel with Adminer. Check out the reference article about MongoDB management on Simple Hosting to learn more.

Here’s an example mongoid.yml file configuration for the Mongoid gem:

production:
  sessions:
    default:
      database: ruby-example
      hosts:
        - localhost:27017

Pushing your application to your instance

You must use git to push your application to your instance and to deploy it. Follow the instructions below to learn more.

Create a local git repository

First, in the root directory of your project on your local machine, run the following commands to initialize a Git repo to track your code’s changes:

$ git init
$ git add .
$ git commit -m "Initial commit"

Note

Remember that your git tree should include at least a config.ru file to boot your application. It should also track the Gemfile and Gemfile.lock files if you wish to install dependencies.

Deployment of your application

Note

The following instructions are available in the Deploy page of your instance management page. There, you will also find the value of the $GIT_URL variable used in the examples below.

Once your code is tracked by git locally, you can push and deploy to your instance.

$ git remote add gandi $GIT_URL
$ git push gandi master
$ ssh $GIT_URL deploy default.git

To deploy a particular branch or tag (a production branch, for example), just specify it as a second argument:

$ git push gandi production
$ ssh $GIT_URL deploy default.git production

Logs and troubleshooting

Standard output (stdout) as well as errors related to the application’s execution are stored in the following logfiles on your instance’s data disk:

  • via SSH: /srv/data/var/log/www/uwsgi.log
  • via SFTP: /lamp0/var/log/www/uwsgi.log

This is useful, for example, for verifying that your application has started correctly.

Cron jobs

Like other Simple Hosting instance families, it’s possible to run scheduled tasks (cron jobs) on Ruby instances. Environment variables defined by your application are also available in your cron jobs (see `internal_design`_ for more details on environment variables).

Example cron job:

1@hourly 0 test (cd /srv/data/web/vhosts/default; rake my_namespace:some_useful_task)

For more information on cron jobs and Simple Hosting, see the Anacron documentation.

Internal design

Ruby instances use Uwsgi to execute your application’s code. Apache is used to serve static files. ‘Apache <http://httpd.apache.org/>’_ will serve the files that exist in a directory named public/ at the root of your project and will relay all other requests to Uwsgi.

The following environment variables are assigned and available in your application and in cron jobs:

  • RAILS_ENV=production
  • RACK_ENV=production