如何在Ubuntu上设置Unicorn?

时间:2014-06-02 22:27:25

标签: ruby-on-rails ruby nginx capistrano unicorn

我正在尝试在DigitalOcean服务器上设置Rails应用程序。

我有这两个文件:

配置/ unicorn.rb

root = "/home/deployer/apps/myapp-staging/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.myapp-staging.sock"
worker_processes 4
timeout 30

# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
  ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end

config / unicorn_init.sh

set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/myapp-staging/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=deployer
set -u

OLD_PIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$CMD"
  ;;
stop)
  sig QUIT && exit 0
  echo >&2 "Not running"
  ;;
force-stop)
  sig TERM && exit 0
  echo >&2 "Not running"
  ;;
restart|reload)
  sig HUP && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
      echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac

和卡皮斯特拉诺:

deploy.rb:

SSHKit.config.command_map[:rake] = "bundle exec rake"
lock '3.1.0'
set :application, 'myapp'
set :scm, :git
set :repo_url, 'git@bitbucket.org:user/repo.git'

set :keep_releases, 5

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      # execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

end

config / staging.rb

role :app, %w{deployer@IP}
role :web, %w{deployer@IP}
role :db,  %w{deployer@IP}
set :stage, :staging

server "IP", roles: %w{app web db}, ssh_options: {
    user: "deployer",
    forward_agent: true,
    auth_methods: %w(password),
    password: 'password',
    port: 420
}
set :deploy_to, "/home/deployer/apps/myapp-staging"
server 'IP', user: 'deployer', roles: %w{web app}

运行 cap staging deploy 之后,代码已部署,但看起来未安装Gemfile中的gem并且未重新启动Unicorn / Nginx。

如何解决?现在,当我部署代码并检查我的IP时,没有任何反应,只是一个空白页。

提前谢谢。

0 个答案:

没有答案