Ruby on Rails (old version)

Ruby on Rails aka "Rails" is a popular web application framework built with Ruby.

This documentation is applicable to Rails apps installed on Opalstack prior to May 2022. If you installed a Rails app on Opalstack after that date please see our current Rails documentation.

Working with gems in your application environment

If you need to install additional gems or modify gems in the application environment:

  1. Prepare your shell environment as described above.

  2. Use the gem command to install the gems that you need.

    gem install name_of_gem
    

Working with gems in your Rails project

If you need to install additional gems or modify gems in your Rails project:

  1. Prepare your shell environment as described above.

  2. Switch to your project directory:

    cd $PROJECTDIR
    
  3. Update the Gemfile in your Rails project directory to specify the gems for your project.

  4. Install the gems with the bundle command:

    bundle install
    

Serving your own Rails project

If you want to serve your own Rails project from a Rails app installed via your Opalstack dashboard:

  1. Upload your project directory to your Rails application directory, for example: /home/myuser/apps/myapp/

  2. SSH to your application's shell user account.

  3. Set your GEM_HOME and PATH environment to your application and then install your project dependencies with bundle:

    export GEM_HOME=$HOME/apps/myapp/env
    export PATH=$HOME/apps/myapp/env/bin:$HOME/apps/myapp/newproject/bin:$PATH
    cd $HOME/apps/myapp/newproject
    bundle install
    
  4. Edit the start and stop scripts in your Rails application directory to change the PROJECTDIR variable on line 4 to point to your project directory.

    For example if your project directory is named newproject you would change this...

    # change the next line to your Rails project directory
    PROJECTDIR='/home/myuser/apps/myapp/myproject'
    

    ... to this ...

    # change the next line to your Rails project directory
    PROJECTDIR='/home/myuser/apps/myapp/newproject'
    
  5. Finally, run the following commands to switch your Rails instance over to your project:

    export APPDIR=$HOME/apps/myapp
    mkdir -p $APPDIR/newproject/tmp/pids
    cp $APPDIR/oldproject/tmp/pids/* $APPDIR/newproject/tmp/pids/
    $APPDIR/stop
    $APPDIR/start
    

Using Databases with Ruby on Rails

PostgreSQL

To use a PostgreSQL database with your Rails project:

  1. Create a new PostgreSQL database and make a note of the following:

    • The database name
    • The database user name
    • The database user password
  2. SSH into your Rails application's shell user account and run the following commands to install the PostgreSQL dependencies for Ruby (substituting myapp with your Rails application name and myproject with your Rails project name):

    export APPDIR=$HOME/apps/myapp
    export PROJECTDIR=$APPDIR/myproject
    export GEM_HOME=$APPDIR/env
    export PATH=/usr/pgsql-11/bin/:$PROJECTDIR/bin:$GEM_HOME/bin:$PATH
    gem install pg
    
  3. Configure your project environment's database.yml section as follows:

    production:
      <<: *default
      adapter: postgresql
      database: your_database_name
      username: your_database_user_name
      password: your_database_user_password
    

MariaDB (MySQL)

Opalstack uses MariaDB for MySQL-compatible database services. To use a MariaDB database with your Rails project:

  1. Create a new MariaDB database and make a note of the following:

    • The database name
    • The database user name
    • The database user password
  2. SSH into your Rails application's shell user account and run the following commands to install the PostgreSQL dependencies for Ruby (substituting myapp with your Rails application name and myproject with your Rails project name):

    export APPDIR=$HOME/apps/myapp
    export PROJECTDIR=$APPDIR/myproject
    export GEM_HOME=$APPDIR/env
    export PATH=$PROJECTDIR/bin:$GEM_HOME/bin:$PATH
    gem install mysql2
    
  3. Configure your project environment's database.yml section as follows:

    production:
      <<: *default
      adapter: mysql2
      database: your_database_name
      username: your_database_user_name
      password: your_database_user_password
    

SQLite

The CentOS operating system used by Opalstack includes a version of SQLite that is too old to be compatible with most current web applications. To work around this, Opalstack maintains a recent version of SQLite in the /usr/sqlite330 directory on web servers.

Note: Rails applications installed via our installer use an environment variable in the start script which allows them to use the newer version of SQLite when they are running. However, when you are working with your Rails application at the command line you must configure the environment manually by setting the LD_LIBRARY_PATH variable as shown below.

To use a SQLite database with your Rails project:

  1. SSH into your Rails application's shell user account and configure your shell environment to recognize the newer SQLite:

    export PATH=/usr/sqlite330/bin:$PATH
    export LD_LIBRARY_PATH=/usr/sqlite330/lib:$LD_LIBRARY_PATH
    
  2. Configure your project environment's database.yml section as follows, substituting db/production.sqlite3 with the path to your SQLite database file:

    production:
      <<: *default
      adapter: sqlite3
      database: db/production.sqlite3