How to deploy a Rails application to VPS (or a dedicated server)?

How can I deploy a Rails application to a VPS (virtual private server) or dedicated server? It would be nice to have a convenient guide for you.

I know about scripts to automate the process, but I think it's best to have everything under control in order to better understand the process.

+7
source share
2 answers

I have successfully deployed the heavy Rails application for Linode or Digital Ocean using the following technologies:

  • rbenv to install Ruby
  • nginx + passenger for application server
  • PostgreSQL for the database server
  • Capistrano , to automate the deployment (first configure this on your machine with your server IP address and settings, I will not describe it here)

These are the steps that work for me:

Virtual machine setup

Create a new virtual machine

Follow the instructions to install your hosting service, which is Linode or Digital Ocean, to create a node and configure it.

Setting date

  • dpkg-reconfigure tzdata li>

Update Packages

  • apt-get update
  • apt-get upgrade

Security

Create user

  • adduser deploy
  • usermod -a -G sudo deploy
  • Exit

Configure SSH Key Authentication

On local:

  • SSH serial
  • copy the public key:
    • scp ~ / .ssh / id_rsa.pub deploy@example.com : ~

On server:

  • ssh deploy@example.com
  • include alias for file list:
    • vim ~ / .bashrc
    • uncomment all aliases
  • mkdir.ssh
  • mv id_rsa.pub.ssh / authorized_keys
  • chown -R deploy: deploy.ssh
  • chmod 700.ssh
  • chmod 600.ssh / authorized_keys
  • logout (check new authentication)

SSH setup

  • sudo vim / etc / ssh / sshd_config
  • Switch PermitRootLogin to no
  • sudo service ssh restart

Firewall setup

Configure fail2ban

Configure if you have enough free memory, as it usually consumes it.

  • sudo apt-get install -y fail2ban

Ruby setup

Install git

  • sudo apt-get install -y git

Install rbenv

Install Ruby

  • sudo apt-get install -y curl gnupg build-essential
  • rbenv install -l (find the latest version)
  • rbenv install 2.3.3 (or the latest available version at the moment)
  • rbenv global 2.3.3
  • rbenv rehash
  • vim.gemrc
    • Paste this: gem: -no-document

Installation servers

Install nginx + passenger

Install PostgreSQL

Setting libraries

Install node.js

Asset precompilation required.

  • sudo apt-get install -y nodejs

Install picker

  • get installation package
  • rbenv rehash

Application Setup

Create a user in PostgreSQL

  • createuser username --pwprompt
  • createdb -Ousername -Eutf8 db_name
  • Check this:
    • psql db_name - username --password

Deploy the code * On the server: * sudo mkdir -p / srv / yoursite.com * sudo chown deploy: deploy / srv / yoursite.com * On your machine dev: * bundle exec cap production deploy: check (it will throw an error because cannot find the database) * On the server: * cd / srv / yoursite.com / shared / config * vim database.yml (insert the configuration of your database) * vim secrets.yml (insert your secret config) * On your dev machine: * deployment of exec cap package * bundle exec cap production whenever: update_crontab

Configure logrotate

+43
source

I deployed the rails application on my production servers (this is a cluster) with Capistrano before, but I found that Capistrano is a bit complicated, and sometimes even created problems ... So I wrote my deployment script on a bash shell script.

I put it on github with a quick guide: deploy_rails

+1
source

All Articles