How to Use Ruby on a Gandi Web Hosting

A Ruby web hosting allows you to publish web applications written in Ruby easily and affordably.

Prerequisites

Users of Ruby web hostings 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 configuration file

  • 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 web hosting. It must therefore be included in your git tree and pushed along your application.

Domains

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

If you would like different domains to route to different Ruby applications on a single Web Hosting web hosting, 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 Web Hosting families, Ruby web hostings support two types of databases: MySQL and PostgreSQL.

PostgreSQL

PostgreSQL version 9.6 is available for Web 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 Web 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 web hostings on Web 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 web hosting 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 Web 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

Pushing Your Application to Your Web Hosting

You must use git to push your application to your web hosting 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 web hosting 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 web hosting.

$ 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 web hosting’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 web hosting families, it’s possible to run scheduled tasks (cron jobs) on Ruby web hostings. 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 Web Hosting, see the Anacron documentation.

Internal Design

Ruby web hostings use Uwsgi to execute your application’s code. Apache is used to serve static files. Apache 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