Skip to content

Explained quick install for production

João Felipe N. Pimentel edited this page Sep 1, 2023 · 21 revisions

Production Environment Quick Setup

This is a guide with the same pattern as the Explained Quick install for development, except that it covers the setup of the Production Environment, which includes additional steps that should be done.

Remember that this guide will help you to set a production environment. The development environment setup guide can be found here.

You shall move on to other guides and/or tutorials if you want to know the features of our platform as soon as you finish this guide.

With no further delay, let us proceed!

Sumary

Requirements

To make this tutorial quick, we'll assume that you already have the following installed in your computer:

  • Ruby 3.2.x - Make sure you're using Ruby's correct version. Check the expected Ruby version for each SAPOS version:
SAPOS RUBY
>= 5.1.x 3.2.x
>= 4.5.x, <= 5.0.x 2.7.x
= 4.4.x 2.4.x
= 4.3.x 2.2.0
<= 4.2.x 1.9.3
  • Apache
  • Passenger
  • wget

With that said, we can proceed smoothly. :)

Configuration

First of all, clone the project at the usr/local folder and update the current branch with the wanted version, replacing the <version> with the wanted version:

$ cd /usr/local
$ git clone https://github.com/gems-uff/sapos.git
$ cd sapos
$ git checkout <version>

Now, here's something that we have not done in the development guide: generate a key (a secret token) that allows sessions for the application to be verified against a known secure key to prevent tampering. To do that, you should execute:

$ bundle exec rake secret

Then, you shall create an environment variable with this value in the operating system user account that runs Rails. This environment variable should be named SECRET_KEY_BASE. This is usually done in Unix environments by adding the following line to the .profile file in the home of the Rails user:

export SECRET_KEY_BASE=<the key generated with _bundle exec rake secret_>

This is a good moment to also add to .profile the environment variable that informs that we intend to run Sapos in production:

export RAILS_ENV="production"

We need, now, to make Rails generate the database stuff.

We suggest that you use MySQL engine for the production environment. So, we need to setup some of the database stuff. These stuff are located in config/database.yml. This file should look like this:

# SQLite version 3.x
#   gem install sqlite3
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

The database.yml, as you can suggestively tell, is the database setup file that will tell Rails the username, the password, and other information concerning the database which will be used in your application.

Since this is a quick tutorial, we'll not explain any further details about the options available in this configuration file. So we suggest that you just change the production environment setup, like thit:

production:
  adapter: mysql2
  encoding: utf8 
  reconnect: false
  database: sapos_production
  pool: 5
  username: sapos
  password: **********
  socket: /var/run/mysqld/mysqld.sock

As you can see, it's a pretty suggestive configuration file. Just fill it with the matching information about your database, and it'll work nicely. Additionally, it is possible to add a production_read_only environment with a username that only has read access for custom queries and notifications. If you do not set it, SAPOS will use the production environment.

Now, we need to use this info and make Rails generate the entire structure for us. To do that, execute:

$ bundle config set --local deployment 'true'
$ bundle config set --local without 'development test'
$ bundle install
$ bundle exec rake db:setup
$ bundle exec rake assets:clean assets:precompile RAILS_RELATIVE_URL_ROOT=/sapos

The bundle install will analyse the file Gemfile.lock and install the dependencies of the project. Notice that we used the bundle config command to set the environment to production and to disable the installation of development and test dependencies.

The rake db:setup will create the database of the current environment, creating all the required tables, and then run the db/seed.rb file to insert default data on the database.

The rake assets:clean clean all compiled assets and the assets:precompile compile all the assets named in the config.assets.precompile.

With this the database should be successfully generated!

But first, in such important environment as production is, you must ensure that your important information, such as database user and password in database.yml will be inaccessible. To do this, just remove the read permission, by doing:

$ chmod o-r  config/database.yml

Now, create a link of this project public folder to the Apache root folder:

$ ln -s /usr/local/sapos/public /var/www/sapos

Run the command above as Super User.

And enable the Passenger on this folder by associating the domain /sapos to the directory /var/www/sapos. To do that, edit the /etc/apache2/conf.d configuration file and insert this:

<Directory /var/www/sapos/>
    Options -MultiViews
    PassengerEnabled on
    RackBaseURI /sapos
</Directory>

Now that you changed the configurations of your server you shall restart it to make all modifications work. Just run:

$ /etc/init.d/apache restart

Run the command above as Super User.

Now you should be ready to start to access the project. To do so, just access http://www.foo.com/sapos , where www.foo.com shall be replaced by the server domain. Sign in using [email protected] user and admin password, and enjoy the application.

Reminder: Go to Configurations and change the email and password of the user [email protected] after signing in. See SAPOS User Guide for more details on how to do this.

If you want to use the Notifications feature of SAPOS, you will also need to create a cron file that will check whether there are pending notifications to be sent.

Create the file /etc/cron.daily/sapos with the following content:

#!/bin/sh
wget --spider --no-check-certificate --quiet https://www.foo.com/sapos/notifications/notify

Remember to replace www.foo.com by your server domain.

Then, add execute permission to the file and make sure it is owned by the root user:

$ cd /etc/cron.daily
$ chmod 700 sapos
$ chown root:root sapos

Now you are ready to go! Enjoy SAPOS!