Skip to content

Instantly share code, notes, and snippets.

@mirzalazuardi
Forked from acfatah/01.md
Created March 18, 2024 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirzalazuardi/a527a42bede3d88c79bb79a9d09a44b9 to your computer and use it in GitHub Desktop.
Save mirzalazuardi/a527a42bede3d88c79bb79a9d09a44b9 to your computer and use it in GitHub Desktop.
How to set up Rails 6 production on Digital Ocean One Click Ruby on Rails Droplet

How to set up Rails 6 production on Digital Ocean One Click Ruby on Rails Droplet

1. Clone The Source

Clone the source repository.

2. Generate Secret Key

Run rails secret to generate secret key and set SECRET_KEY_BASE to the value later in ~/.profile.

3. Update Configurations

Update ~/.profile with the following configurations

# REQUIRED Production configurations example
export RAILS_ENV=production
export SECRET_KEY_BASE=value from `rails secret`
export RAILS_SERVE_STATIC_FILES=1

The following files require root access to edit them,

/etc/systemd/system/rails.service
/etc/nginx/sites-enabled/rails

Run the following commands (as root) to restart services,

systemctl daemon-reload
systemctl restart rails.service
service nginx restart

/etc/nginx/sites-enabled/rails example

  [Unit]
+ Description=Project Name
- Description=ExampleApp
  Requires=network.target

  [Service]
  Type=simple
  User=rails
  Group=rails
+ WorkingDirectory=/home/rails/project-directory/
- WorkingDirectory=/home/rails/example/
  ExecStart=/bin/bash -lc 'bundle exec puma'
  TimeoutSec=30s
  RestartSec=30s
  Restart=always

  [Install]
  WantedBy=multi-user.target

/etc/nginx/sites-enabled/rails example

  server {
      listen   80;
+    root /home/rails/project-directory/public;
-    root /home/rails/example/public;
      server_name _;
      index index.htm index.html;

          location ~ /.well-known {
              allow all;
          }

          location / {
          proxy_pass http://localhost:3000;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      }
  }

Installation Steps

Install Gems and Yarn Packages

May need to run rvm install "ruby-x.x.x" if ruby version is different.

We need to update the bundler with bundle update --bundler if we change the ruby version.

Then run bundle install and followed by yarn install --check-files.

Setup Database

Run rails db:setup && rails db:migrate

Precompile Assets

bin/webpack

rails assets:precompile

Additional Notes

Debugging

On production, errors are not visible. We need to read the log files to trace errors.

# Rails log
~/project-directory/log/production.log

# Nginx log 
/var/log/nginx/error.log

Assets

On production, relative path to assets does not work unless they reside in /public/assets directory. We need to use asset_path or asset_url helpers to generate path or url (with fingerprints) to the assets.

@Monkey-D1-Luffy
Copy link

TQuyw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment