In this article we'll see how Moss can create, deploy and monitor a Rails application. We'll use Fat Free CRM as an example, but other Rails applications can be set up similarly. We also assume that you use git in your development workflow. 

Connect your server to Moss

If you haven't done it yet, you must have at least one server in Moss. You must be the admin of your Moss's organization in order to perform this step.

While it doesn't make much difference, we note that the server we've used for this article runs Ubuntu 18.04.

Don't forget to upload your public SSH key onto your server, since you'll need to log into it via SSH in the following.

Install Ruby

Now you must log into your server as user moss and install Ruby by any means you feel comfortable with. In this example, we'll just use the software package that comes with Ubuntu 18.04 (note that Fat Free CRM supports Ruby 2.5).

local$ ssh moss@<server-ip>
$ sudo apt -y install ruby ruby-dev
$ ruby --version
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]

You must be the admin of your Moss's organization in order to perform this step.

Install your project's dependencies

Also as user moss, install everything your project's gems will require. In the case of Fat Free CRM, the following command applies:

$ sudo apt -y install libmagick++-dev libxml2 libxml2-dev libxslt1.1 libxslt1-dev libyaml-dev mysql-client libmysqlclient-dev libsqlite3-dev nodejs
$ sudo gem install bundle
$ exit

You must be the admin of your Moss's organization in order to perform this step.

Create your database and database user

If your application relies on a database (as it likely does), you'll have to create your database and user by any means.

In the case of a MySQL database, you should log into Moss and head to your server's databases tab. Then:

  1. Create a database user for your application: fat in this example.
  2. Create a database for your application: fat_free_crm_production in this example.
  3. Grant fat permissions on fat_free_crm_production.

Create your website

Now we're ready to create our website with Moss. Since Rails is not natively supported in Moss as of this writing, we'll choose "Static" as the language of our website - in this way, Moss won't install any software that we won't use in our server.

The git repo in this example is a fork of Fat Free CRM. The only difference with the upstream is that we've updated the file Gemfile.lock with the appropriate dependencies.

The important things to note here are in the "Advanced" section:

  • The root dir of the Rails application is public 
  • Everything not in your git repo must be specified as shared directories or files (files with secrets, user-generated content, application logs, etc). In the case of Fat Free CRM, we specify config/database.yml as a "Shared file".

Finally, we click the 'Add' button and wait for Moss to finish the process.

Set up shared directories and files

Since shared directories and files are not part of our git repo, we must provide them within the folder /home/<site-user>/sites/<your-site-name>/shared/.

In this example, we have to create the file config/database.yml with the appropriate content. In particular, it must contain the database name, database username, and database user password we created before. We must do this by logging into our server as user <site-user> (you'll find it at the 'Settings' tab of your site):

local$ ssh <site-user>@<server-ip>
$ cd sites/fatfreecrm/
$ mkdir shared/config
$ cat > shared/config/database.yml <<EOF
---
development: &development
  adapter: mysql2
  encoding: utf8
  database: fat_free_crm_development
  pool: 5
  username: fat
  password: arandompass

test:
  <<: *development
  database: fat_free_crm_test

production:
  <<: *development
  database: fat_free_crm_production

staging:
  <<: *development
  database: fat_free_crm_staging
EOF

Deploy your application

Now we'll tell Moss what commands it must run on our server in order to successfully build our application.

In this example we just need to provide a pre-activation script that installs all dependencies, runs database migrations, sets up an admin account, and compiles static assets.

export CI=true DB=mysql RAILS_ENV=production
bundle install --path vendor/bundle
rake db:migrate
rake ffcrm:setup:admin USERNAME=admin PASSWORD=something EMAIL=you@email.com
rake assets:precompile

Then click 'Update' and, after it successfully ends, click 'Deploy' and wait for Moss to finish the process.

If there's any problem during the execution of your deployment scripts, you can take a look to their log as user <site-user> in folder /home/<site-user>/sites/<site-name>/logs/deploy/.

Start Rails application server

The command bin/rails server starts a Puma application server to run your Ruby application. By default it listens on http://127.0.0.1:3000.

Instead of blindly running such command in a deployment script, we're going to create a supervisor process that will restart Puma in case it hangs.

For this, please log into your server as user moss and run the following commands (first adapt the content of the configuration file to your app instead of Fat Free CRM).:

local$ ssh moss@<server-ip>
$ sudo su
$ cat > /etc/supervisor/conf.d/fatfreecrm-puma.conf <<EOF
[program:worker-fatfreecrm-puma]
process_name=%(program_name)s_%(process_num)02d
directory=/home/<site-user>/sites/fatfreecrm/current
environment=RAILS_ENV="production",DB="mysql",CI="true"
command=/home/<site-user>/sites/fatfreecrm/current/bin/rails server
autostart=true
autorestart=true
user=<site-user>
numprocs=1
redirect_stderr=true
stdout_logfile=/home/<site-user>/sites/fatfreecrm/logs/worker-fatfreecrm-puma.log
EOF
$ supervisorctl reload
$ exit
$ exit

Set up Nginx

At this point your application is deployed on your server and it's reachable locally on port 3000. To make our app globally available, we just need to set up Nginx as a reverse proxy in front of Puma.

Moss allows you to fine-tune your config by means of the file server_params.<site-name>. Please log into your server as user moss and run the following commands:

local$ sudo moss@<server-ip>
$ sudo su
$ cat >> /etc/openresty/server_params.fatfreecrm <<EOF

# Puma application server
#
location ~ /(.*) {
      proxy_pass  http://127.0.0.1:3000;
}
EOF

Finally, head to your site in Moss and click 'Provision'. If everything goes well, your website is ready to go live ;)

Now you can update the appropriate DNS records, wait for them to propagate, and check your application is working as expected. In case you cannot update your DNS records now, you may wan to add a domain alias based on your server's IP address.

In our case, we finally have a working deployment of Fat Free CRM with zero-downtime automatic deployments, automatic free HTTPS (via Let's Encrypt certs), and zero-config monitoring & alerts :D

What's next?

Just keep developing your application and deploy it manually or automatically (after git push ) via Moss.

Did this answer your question?