7

我跟随Ryan 的截屏视频并部署到 VPS。所以我使用 Unicorn + nginx + github + Ubuntu 12.04 LTS + capistrano。我也使用 i18n 来翻译应用程序。

我还想注意我使用Carriarewave进行图片上传。Carriarewave 将图片保存在本地 VPS 上。当我上传图片时效果很好,上传的图片就会出现。

但是每次当我将新的更改部署到服务器时,我的所有图片都会损坏。实在是太可怕了。我尝试手动重启nginx:

sudo service nginx restart

我试图重新启动独角兽:

/etc/init.d/unicorn_Chirch_app restart

它也没有帮助。

当我尝试手动打开损坏的页面时,它说:

The page you were looking for doesn't exist.
You may have mistyped the address or the page may have moved.

当我尝试在控制台中查找图片时:

> Photo.all
> => [#<Photo id: 3, description: nil, created_at: "2013-01-21 11:14:01", updated_at: "2013-01-21 11:14:01", image: "1320700703588.jpg">, #<Photo id: 4, description: nil, created_at: "2013-01-21 11:14:01", updated_at: "2013-01-21 11:14:01", image: "Seasonscape_by_alexiuss.jpg">, #<Photo id: 5, description: nil, created_at: "2013-01-21 11:30:30", updated_at: "2013-01-21 11:30:30", image: "Seasonscape_by_alexiuss.jpg">]

据我了解,它们应该存在。

日志中的错误:

Started GET "/ru/uploads%2Fphoto%2Fimage%2F4%2FSeasonscape_by_alexiuss" for 89.178.205.47 at 2013-01-21 11:31:17 +0000

ActionController::RoutingError (No route matches [GET] "/ru/uploads%2Fphoto%2Fimage%2F4%2FSeasonscape_by_alexiuss"):
  actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.4) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.4) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.4) lib/rack/lock.rb:15:in `call'
  rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
  rack-cache (1.2) lib/rack/cache/context.rb:245:in `fetch'
  rack-cache (1.2) lib/rack/cache/context.rb:185:in `lookup'
  rack-cache (1.2) lib/rack/cache/context.rb:66:in `call!'
  rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
  railties (3.2.8) lib/rails/engine.rb:479:in `call'
  railties (3.2.8) lib/rails/application.rb:223:in `call'
  railties (3.2.8) lib/rails/railtie/configurable.rb:30:in `method_missing'
  unicorn (4.5.0) lib/unicorn/http_server.rb:552:in `process_client'
  unicorn (4.5.0) lib/unicorn/http_server.rb:628:in `worker_loop'
  unicorn (4.5.0) lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
  unicorn (4.5.0) lib/unicorn/http_server.rb:511:in `maintain_worker_count'
  unicorn (4.5.0) lib/unicorn/http_server.rb:277:in `join'
  unicorn (4.5.0) bin/unicorn:121:in `<top (required)>'
  /home/deployer/apps/My_app/shared/bundle/ruby/1.9.1/bin/unicorn:23:in `load'
  /home/deployer/apps/My_app/shared/bundle/ruby/1.9.1/bin/unicorn:23:in `<main>'

我的配置/deploy.rb

require "bundler/capistrano"

server "my_ip_here", :web, :app, :db, primary: true

set :application, "My_app"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false

set :scm, "git"
set :repository, "git@github.com:MyName/#{application}.git"
set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

after "deploy", "deploy:cleanup" # keep only the last 5 releases

namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_#{application} #{command}"
    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_init.sh /etc/init.d/unicorn_#{application}"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.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
4

3 回答 3

8

好的,我找到了解决方案。出现问题是因为我没有更改默认文件夹以保留图像。您可以在 中找到您的默认文件夹public/uploads。这意味着每个cap deploy人都会创建一个新的空文件夹,其中不包含您的旧文件。

要解决此问题,您应该创建另一个不在您的应用程序中的文件夹。我选择最简单的方法。我创建了符号链接。

我的步骤:

1)在您的服务器上转到您的应用程序的共享文件夹(它通过 capistrano 自动生成)。然后创建您的文件夹以保留新图像:

$ mkdir uploads

2) 为创建的文件夹赋予必要的权限:

$ sudo chmod 775 uploads

3) 在本地机器上的 config/deploy.rb 添加:

task :symlink_config, roles: :app do
  ...
  run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
end

4)然后推送git并部署:

$ git push
$ cap deploy:symlink
$ cap deploy

现在一切正常。

于 2013-01-21T12:54:18.590 回答
2

好一个!我已经扩展了你的 capistrano 食谱。

# config/recipes/carrierwave.rb

namespace :carrierwave do 
  task :uploads_folder do
    run "mkdir -p #{shared_path}/uploads"
    run "#{sudo} chmod 775 #{shared_path}/uploads"
  end
  after 'deploy:setup', 'carrierwave:uploads_folder'

  task :symlink do 
    run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
  end
  after 'deploy', 'carrierwave:symlink'
end
于 2013-02-21T00:34:34.520 回答
0

@ExiRe 和 @Charlie 回答适用于 Capistrano 2.x。在 Capistrano 3.x 中, command run替换为 command execute

所以,我通过以下步骤解决了这个问题:

  1. 在目录中创建一个 rake 文件,lib/capistrano/tasks/carrierwave.rake内容如下:
命名空间:carrierwave 做
  任务:uploads_folder 做
    在角色(:应用程序)上做
      执行“mkdir -p #{shared_pa​​th}/uploads”
      执行“#{sudo} chmod 775 #{shared_pa​​th}/uploads”
    结尾
  结尾
  
  任务:符号链接做
    在角色(:应用程序)上做
      执行“ln -nfs #{shared_pa​​th}/uploads #{release_path}/public/uploads”
    结尾
  结尾
结尾
  1. Capfile如果没有 ,请在您的末尾添加以下行。Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

  2. 将这些任务config/deploy.rb与其他任务一起添加。

  • after 'deploy:publishing', 'carrierwave:uploads_folder'
  • after 'deploy:publishing', 'carrierwave:symlink'
命名空间:部署做
  ..
  在'部署:发布'之后,'carrierwave:uploads_folder'
  在“部署:发布”之后,“载波:符号链接”
  ..
结尾
  1. 推送 git 并部署

shared/uploads现在,即使是新部署,您上传的图像也将保存在文件夹中。

于 2020-08-15T08:33:13.717 回答