PUMA RoR¶
puma.rb:
puts "RAILS_ENV=#{ENV["RAILS_ENV"]}"
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/tmp"
environment ENV.fetch("RAILS_ENV") {"production"}
threads_count = ENV.fetch("RAILS_MAX_THREADS") {4}
workers threads_count
threads threads_count, threads_count
preload_app!
daemonize false
if ENV["RAILS_ENV"] == "production"
daemonize true
bind "unix://#{shared_dir}/sockets/puma.sock"
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/puma.state"
#stdout_redirect "#{app_dir}/log/puma_stdout.log", "#{app_dir}/log/puma_stderr.log"
end
activate_control_app
on_worker_boot do |worker_index|
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
plugin :tmp_restart
nginx:
upstream puma {
server unix:///usr/local/www/app/tmp/sockets/puma.sock;
}
server {
set $host_path /usr/local/www/app;
root $host_path/public;
location / {
root $host_path/public;
try_files $uri/index.html $uri.html $uri @ruby;
}
location @ruby {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_read_timeout 300;
proxy_cache off;
proxy_pass http://puma;
}
}
init app:
#!/usr/bin/env bash
PATH=/bin:/usr/bin:/usr/local/bin:~/bin
export PATH
test -f /etc/profile.d/rvm.sh && source /etc/profile.d/rvm.sh
RAILS_ENV="production"
APP_ROOT="/usr/local/www/redmine"
APP_USER="git"
DAEMON_OPTS="-C $APP_ROOT/config/puma.rb"
PID_PATH="$APP_ROOT/tmp/pids"
WEB_SERVER_PID="$PID_PATH/puma.pid"
NAME="redmine"
DESC="Redmine service"
RAILS_MAX_THREADS=2
check_pid(){
if [ -f $WEB_SERVER_PID ]; then
PID=$(cat $WEB_SERVER_PID)
STATUS=$(ps aux | grep $PID | grep -v grep | wc -l)
else
STATUS=0
PID=0
fi
}
execute() {
su $APP_USER -c "$1"
}
start() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
echo "Error! $DESC $NAME is currently running!"
return 1
else
if [ $(whoami) = root ]; then
execute "export RAILS_ENV=$RAILS_ENV && export RAILS_MAX_THREADS=$RAILS_MAX_THREADS && bundle exec puma $DAEMON_OPTS"
echo "$DESC started"
fi
fi
}
stop() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
## Program is running, stop it.
kill -QUIT $(cat $WEB_SERVER_PID)
rm "$WEB_SERVER_PID" >> /dev/null
echo "$DESC stopped"
return 0
else
## Program is not running, exit with error.
echo "Error! $DESC not started!"
exit 1
fi
}
restart() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
echo "Restarting $DESC..."
kill -USR2 $(cat $WEB_SERVER_PID)
echo "$DESC restarted."
exit 0
else
echo "Error, $NAME not running!"
start
exit 1
fi
}
status() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
echo "$DESC / Puma with PID $PID is running."
exit 0
else
echo "$DESC is not running."
exit 1
fi
}
## Check to see if we are running as root first.
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root"
exit 1
fi
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload|force-reload)
echo -n "Reloading $NAME configuration: "
kill -HUP $(cat $PID)
echo "done."
;;
status)
status
;;
*)
echo "Usage: sudo service redmine {start|stop|restart|reload|status}" >&2
exit 1
;;
esac
exit 0