我在让我的 rails 应用程序在生产中运行时遇到了一些问题。正如我之前编写过 Rails 代码但没有部署太多,这可能是我的配置和/或部署过程中的问题。
一切都在本地开发中运行良好。所有测试都通过了,我可以看到路由并在 localhost 上访问它们。在生产中(CentOS 上的 rails 4、nginx、unicorn)我在相同的路线上遇到 500 个错误。
当我在服务器上搜索路由时,我看到的所有路由都与开发时相同。服务器上的 Rake 路由返回(从完整的路由列表中缩短):
Prefix Verb URI Pattern Controller#Action
companies GET /companies(.:format) companies#index
POST /companies(.:format) companies#create
new_company GET /companies/new(.:format) companies#new
edit_company GET /companies/:id/edit(.:format) companies#edit
company GET /companies/:id(.:format) companies#show
PATCH /companies/:id(.:format) companies#update
PUT /companies/:id(.:format) companies#update
DELETE /companies/:id(.:format) companies#destroy
您可能猜到来自资源:公司
但是http://sortinghat.foo.com/companies给了我关于生产的 rails 500 页面(foo 显然不是域,但我刚刚替换了真正的域)。
我在这里和网上搜索了一些想法,阅读了很多“如何部署 Rails”的博客文章,并查看了很多配置文件(你会看到这些是我从其他地方复制的,但它一切看起来都很标准)。我可能遗漏了一些明显的东西。我在日志中没有看到任何问题。
我唯一的线索是我没有 /etc/nginx/sites-enabled 目录,看起来 capistrano 想要一个,但在 cap 部署期间我没有看到任何错误(除了我在底部)。在这种情况下,它是唯一正在运行的站点,因此我不需要虚拟主机,并且认为我不需要它,所以不用担心。
提前感谢您的帮助。
这是我的 nginx.config
# Taken from http://www.sitepoint.com/deploying-your-rails-app-to-the-cloud-with-unicorn-nginx-and-capistrano/
upstream unicorn {
server unix:/tmp/unicorn.sortinghat.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name sortinghat;
if ($host = 'sortinghat' ) {
rewrite ^/(.*)$ http://sortinghat.foo.com/$1 permanent;
}
root /var/www/sortinghat/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
我的独角兽.rb
# Taken from http://www.sitepoint.com/deploying-your-rails-app-to-the-cloud-with-unicorn-nginx-and-capistrano/
# Define your root directory
root = "/var/www/sortinghat/current"
# Define worker directory for Unicorn
working_directory root
# Location of PID file
pid "#{root}/tmp/pids/unicorn.pid"
# Define Log paths
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
# Listen on a UNIX data socket
listen "/tmp/unicorn.sortinghat.sock"
# 16 worker processes for production environment
worker_processes 16
# Load rails before forking workers for better worker spawn time
preload_app true
# Restart workes hangin' out for more than 240 secs
timeout 240
还有我的 deploy.rb (注意:我使用chmod 777作为快速破解使一些世界可读的东西。我只是想让它在内部运行以在防火墙后面运行测试。我可能可以把它们拿出来,但我怀疑他们现在正在伤害任何东西。)
require 'capistrano/ext/multistage'
set :application, "sortinghat"
# Need to use gnutar since that is what the server uses
# see https://coderwall.com/p/ypwmpg
set :copy_local_tar, "/usr/bin/gnutar" if `uname` =~ /Darwin/
# remove old releases
set :keep_releases, 3
# Source Control
# You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
set :repository, "git@github.com:me/project.git" # changed here but correct in the actual file
set :scm, :git
set :branch, "master"
set :deploy_via, :copy
# Deployment
# Default deployment directory is /u/apps/#{application}” (where #{application} but alternate can be set
# set :deploy_to, "/var/www"
set :user, "mherschberg"
default_run_options[:pty] = true
set :use_sudo, false
set :ssh_options, {:forward_agent => true}
role :web, "sortinghat.foo.com" # Your HTTP server, Apache/etc
role :app, "sortinghat.foo.com" # This may be the same as your `Web` server
role :db, "lin-db3.foo.com", :primary => true #, :no_release => true # This is where Rails migrations will run
#role :db, "your slave db-server here" # no slave for test
set :rails_env, "production"
set :stages, ["staging", "production"]
set :default_stage, "staging"
# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
sudo "chmod 777 /var/www/sortinghat/current/config/unicorn_ini.sh"
run "/etc/init.d/unicorn_#{application} #{command}" # Using unicorn as the app server
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_ini.sh /etc/init.d/unicorn_#{application}"
sudo "chmod 777 #{current_path}/config/unicorn_ini.sh"
sudo "chmod 777 /var/www/sortinghat/current/config/unicorn_ini.sh"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
# If you are using Passenger mod_rails uncomment this:
# namespace :deploy do
# task :start do ; end
# task :stop do ; end
# task :restart, :roles => :app, :except => { :no_release => true } do
# run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
# end
# end
cap deploy只有一个错误,但这只是清理旧版本的权限问题。(帽部署:冷运行良好)
** [out :: sortinghat.foo.com] rm: cannot remove `/var/www/sortinghat/releases/20130917180205/thout-curl-include=dir/include/build_info': Permission denied