Skip to content

Instantly share code, notes, and snippets.

@finist
Last active June 16, 2021 21:52
Show Gist options
  • Save finist/8ace73b872259e2182b9d9359458fbbd to your computer and use it in GitHub Desktop.
Save finist/8ace73b872259e2182b9d9359458fbbd to your computer and use it in GitHub Desktop.
Ruby on Rails setup on Ubuntu 16.04

Ruby on Rails setup on Ubuntu 16.04 (work in progress)

Create user

https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-ubuntu-16-04

# Create new user with sudo
adduser develop
usermod -aG sudo develop

# Configure sudo for call it without password
sudo visudo

# Add line
develop ALL=(ALL) NOPASSWD: ALL

# Copy authorized keys
mkdir /home/develop/.ssh
cp ~/.ssh/authorized_keys /home/develop/.ssh/authorized_keys
chown -R develop:develop /home/develop/.ssh

# Exit and login as develop

Install rbenv and ruby

https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-16-04

# System update
sudo apt-get update
sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev

# Install rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

# Install rbenv vars plugin
cd ~/.rbenv/plugins
git clone git@github.com:sstephenson/rbenv-vars.git

# Install ruby-build
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

# Check rbenv
rbenv -v

# Install ruby
rbenv install 2.3.1
rbenv global 2.3.1

# Config gem
echo "gem: --no-document" > ~/.gemrc

# Install bundler
gem install bundler

# Install Javascript Runtime
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get install nodejs

Install PostgreSQL

# Install PostgreSQL
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

# Enter to postgres console
sudo -u postgres psql

# Create new postgres user
CREATE USER username WITH PASSWORD 'password';
ALTER USER username CREATEDB;

# Save password

# Change connect postgres access
sudo vi /etc/postgresql/9.5/main/pg_hba.conf

# Replace it
local   all             all                                     md5

# Restart it
sudo /etc/init.d/postgresql restart

# Install it for pg gem support
sudo apt-get install libpq-dev

Install and configure Nginx

Install nginx

# Install nginx
sudo apt-get install nginx

# Change default config
sudo vi /etc/nginx/sites-enabled/default

# Restart it
sudo service nginx restart

Simple nginx config

upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix://srv/servername/shared/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    server_name servername;

    # listen 443 ssl;

    # ssl_certificate /home/username/ssl/servicename.chained.crt;
    # ssl_certificate_key /home/username/ssl/server.key;

    root /srv/servername/current/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

Config project directory

sudo mkdir -p "/srv/servername/" && sudo chown -R develop:develop "/srv/servername/"

Setup systemd task

sudo touch /etc/systemd/system/app_puma.service
sudo chmod 664 /etc/systemd/system/app_puma.service
sudo vi /etc/systemd/system/app_puma.service

sudo systemctl daemon-reload
sudo systemctl start app_puma.service

systemctl status app_puma.service

[Unit]
Description=Puma web server for "App"
After=network.target

[Service]
Type=simple
User=develop

Environment=RAILS_ENV=production

WorkingDirectory=/srv/app/current

ExecStart=/home/develop/.rbenv/bin/rbenv exec bundle exec puma -C /srv/app/shared/config/puma.rb
ExecStop=/home/develop/.rbenv/bin/rbenv exec bundle exec pumactl -S /srv/app/shared/tmp/pids/puma.state stop
TimeoutSec=15
Restart=always

[Install]
WantedBy=multi-user.target

Env vars

rbenv-vars is an rbenv plugin. To install it, simply clone git@github.com:sstephenson/rbenv-vars.git to your rbenv installation's plugin directory.

Once installed, it will look for a file called .rbenv-vars in your current working directory or any of its parent directories. To set environment variables, just define them as key/value pairs:

RAILS_ENV=production
SECRET_KEY_BASE=abc
API_KEY=def
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment