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:
-
Prepare your shell environment as described above.
-
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:
-
Prepare your shell environment as described above.
-
Switch to your project directory:
cd $PROJECTDIR
-
Update the
Gemfile
in your Rails project directory to specify the gems for your project. -
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:
-
Upload your project directory to your Rails application directory, for example:
/home/myuser/apps/myapp/
-
SSH to your application's shell user account.
-
Set your
GEM_HOME
andPATH
environment to your application and then install your project dependencies withbundle
: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
-
Edit the
start
andstop
scripts in your Rails application directory to change thePROJECTDIR
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'
-
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:
-
Create a new PostgreSQL database and make a note of the following:
- The database name
- The database user name
- The database user password
-
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 andmyproject
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
-
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:
-
Create a new MariaDB database and make a note of the following:
- The database name
- The database user name
- The database user password
-
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 andmyproject
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
-
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:
-
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
-
Configure your project environment's
database.yml
section as follows, substitutingdb/production.sqlite3
with the path to your SQLite database file:production: <<: *default adapter: sqlite3 database: db/production.sqlite3