Notice
This article is old, and some of the information may be outdated or no longer valid today.
If you have any questions or need to confirm any details, we recommend contacting our support team for up-to-date information and personalized assistance.
Installing RVM on Ubuntu 16.04
You need to use the following package:
# apt-add-repository -y ppa:rael-gc/rvm
# apt update
# apt install rvm nodejsCreate the user ror and add it to the rvm group:
# adduser ror --ingroup rvmAdd the user ror to the sudo group:
# usermod -G sudo rorEdit the file /etc/ssh/sshd_config and on the last line add the user ror to AllowUsers:
AllowUsers root rorFinally, restart the ssh service:
# service ssh restartInstalling Ruby on Rails on Ubuntu 16.04
Log in as the user ror and install ruby 2.1.3:
$ rvm install 2.1.3
$ rvm use 2.1.3Verify that this version has actually been activated as follows:
ruby 2.1.3p242 (2014-09-19 revisión 47630) [x86_64-linux]Finally, install Rails 3.2.19 while still logged in as the user ror:
$ gem install rails -v 3.2.19Installing Unicorn on Ubuntu 16.04
First, as the user ror you must install unicorn:
$ gem install unicornSecond, as the user root, create the various folders you will use and then make the user ror their owner:
# mkdir -p /var/ror/app/shared /var/ror/unicorn/config /var/log/unicorn /var/run/unicorn /etc/unicorn/
# chown -R ror:rvm /var/ror/.
# chown -R ror:rvm /var/ror/*
# chown -R ror:rvm /var/log/unicorn/.
# chown -R ror:rvm /var/run/unicorn/.As the user ror again, create the file /var/ror/unicorn/config/unicorn.rb as follows:
# set path to the application
app_dir = File.expand_path("/var/ror/app/current", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir
# Set unicorn options
worker_processes 2
preload_app true
timeout 30
# Path for the Unicorn socket
listen “/var/run/unicorn/unicorn.sock”, :backlog => 64
# Set path for logging
stderr_path “/var/log/unicorn/stderr.log”
stdout_path “/var/log/unicorn/stdout.log”
# Set proccess id path
pid “/var/run/unicorn/unicorn.pid”Next, as the user root, create the file /etc/init.d/unicorn with the following content:
### BEGIN INIT INFO
# Required-Start
# Required-Stop
# Default-Start
# Default-Stop
# Short-Description
# Description: Enable at boot time
### END INIT INFO
# This is /etc/init.d/unicorn_init (without .sh)
# init.d script for single or multiple unicorn installations. Expects at least one .conf
# file in /etc/unicorn
#
# Forked from https://gist.github.com/zewelor/2623205
# based on modified version by http://github.com/killercup
# which based on modified version by jay@gooby.org http://github.com/jaygooby
# which is based on http://gist.github.com/308216 by http://github.com/mguterl
#
## A sample /etc/unicorn/my_app.conf
##
## RAILS_ENV=production
## RAILS_ROOT=/var/apps/www/my_app/current
## UNICORN=”/usr/local/rvm/bin/<WRAPPED_NAME>” #see rvm wrapper above
## APP_USER=”some_user”
## UNICORN_CONFIG=”/var/apps/www/my_app/shared/config/unicorn.rb”
#
# This configures a unicorn master for your app at /var/apps/www/my_app/current running in
# production mode. It will read config/unicorn.rb for further set up.
#
# You should ensure different ports or sockets are set in each config/unicorn.rb if
# you are running more than one master concurrently.
#
# If you call this script without any config parameters, it will attempt to run the
# init command for all your unicorn configurations listed in /etc/unicorn/*.conf
#
# /etc/init.d/unicorn start # starts all unicorns
#
# If you specify a particular config, it will only operate on that one
#
# /etc/init.d/unicorn start my_app
export rvmsudo_secure_path=1
RVM_FILE_TO_SOURCE=”/usr/share/rvm/scripts/rvm”
sig () {
test -s “$PID” && kill -$1 `cat “$PID”`
}
oldsig () {
test -s “$OLD_PID” && kill -$1 `cat “$OLD_PID”`
}
cmd () {
case $1 in
start)
sig 0 && echo >&2 “Already running” && return 0
eval $CMD
echo “Starting”
;;
stop)
sig QUIT && echo “Stopping” && return 0
echo >&2 “Not running”
;;
force-stop)
sig TERM && echo “Forcing a stop” && return 0
echo >&2 “Not running”
;;
restart|reload)
sig USR2 && sleep 5 && oldsig QUIT && echo “Killing old master” `cat $OLD_PID` && return 0
echo >&2 “Couldn’t reload, starting ‘$CMD’ instead”
eval $CMD
;;
upgrade)
sig USR2 && echo Upgraded && return 0
echo >&2 “Couldn’t upgrade, starting ‘$CMD’ instead”
eval $CMD
;;
rotate)
sig USR1 && echo rotated logs OK && return 0
echo >&2 “Couldn’t rotate logs” && return 1
;;
*)
echo >&2 “Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>”
exit 1
;;
esac
}
setup () {
CONFIG=$1
export APP_NAME=`basename $1|cut -f 1 -d ‘.’`
echo “Launching ${APP_NAME}”
if [ -z $APP_USER ]; then
if [ `/bin/egrep -i “^${APP_NAME}” /etc/passwd|wc -l` -eq 1 ]; then
export APP_USER=$APP_NAME
else
echo “The is no user defined in ${CONFIG}, and the is no user in the system called ${APP_NAME}”
return 1
fi
fi
if [ -z $RAILS_ROOT ]; then
if [ -d “/home/${APP_USER}/www/current” ]; then
export RAILS_ROOT=”/home/${APP_USER}/www/current”
else
echo “Rails root is not defined in ${CONFIG}”
return 1
fi
fi
if [ -z $RAILS_ENV ]; then
export RAILS_ENV=”production”
fi
# If unicorn binary was not defined in config
if [ -z “$UNICORN” ]; then
UNICORN=”unicorn_rails”
fi
# If unicorn config was not defined in config
if [ -z $UNICORN_CONFIG ]; then
UNICORN_CONFIG=${RAILS_ROOT}/config/unicorn/${RAILS_ENV}.rb
fi
cd $RAILS_ROOT || return 1
export PID=$RAILS_ROOT/tmp/pids/unicorn.pid
export OLD_PID=”$PID.oldbin”
CMD=”rvmsudo -u ${APP_USER} ${UNICORN} -E ${RAILS_ENV} -c ${UNICORN_CONFIG} -D”
return 0
}
# either run the start/stop/reload/etc command for every config under /etc/unicorn
# or just do it for a specific one
# $1 contains the start/stop/etc command
# $2 if it exists, should be the specific config we want to act on
source $RVM_FILE_TO_SOURCE
if [ $2 ]; then
. /etc/unicorn/$2.conf
setup “/etc/unicorn/$2.conf”
if [ $? -eq 1 ]; then
exit
fi
cmd $1
else
for CONFIG in /etc/unicorn/*.conf; do
# clean variables from prev configs
unset APP_USER
unset RAILS_ROOT
unset RAILS_ENV
unset UNICORN
unset UNICORN_CONFIG
# import the variables
. $CONFIG
setup $CONFIG
if [ $? -eq 1 ]; then
continue
fi
# run the start/stop/etc command
cmd $1
done
fiAnd make it executable as follows:
# chmod a+rx /etc/init.d/unicornNow create the file /etc/unicorn/unicorn.conf with the following content:
RAILS_ENV=production
RAILS_ROOT=/var/ror/app/current
UNICORN="/usr/share/rvm/gems/ruby-2.1.3/bin/unicorn" #see rvm wrapper above
APP_USER="ror"
UNICORN_CONFIG="/var/ror/unicorn/config/unicorn.rb"Finally, enable unicorn at every startup:
# update-rc.d unicorn defaults
# update-rc.d unicorn enableInstalling and configuring NGINX on Ubuntu 16.04
First, install Nginx:
# apt install nginxEdit the file /etc/nginx/nginx.conf and add the following to the HTTP section:
upstream rails {
# Path to Unicorn socket file
server unix:/var/run/unicorn/unicorn.sock fail_timeout=0;
}Then delete the default vhost configuration file:
# rm /etc/nginx/sites-enabled/defaultAnd create a new one named midominio.com with the following content:
server {
listen 80 default;
server_name midominio.com;
root /var/ror/app/current;
try_files $uri/index.html $uri @rails;
location @rails {
proxy_pass http://rails;
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;
}Next, enable the site and restart Nginx:
# ln -s /etc/nginx/sites-available/midominio.com /etc/nginx/sites-enabled
# service nginx restartConfiguring logrotate
Create the file /etc/logrotate.d/unicorn with the following content:
/var/log/unicorn/*.log {
daily
missingok
rotate 14
compress
dateext
delaycompress
notifempty
copytruncate
create 0666 ror rvm
maxage 14
}Optionally, you can install a demo app to check that everything works correctly. We explain how to do this.
Installing a demo app
Log in as the user ror and run the following commands:
$ cd /var/ror/app
$ rails new current
$ cd current
$ rails generate scaffold Task title:string note:text
$ RAILS_ENV=production rake db:migrateThen start unicorn as the user root:
# /etc/init.d/unicorn startFinally, check that everything works correctly by visiting the URL:
http://ip.de.vm/public
That's it, you can now deploy your application in the /var/ror/app/current folder