2013-01-21 39 views
4

我遵循Ryan's screencast並部署到VPS。所以我使用Unicorn + nginx + github + Ubuntu 12.04 LTS + capistrano。另外我用i18n來翻譯應用程序。Rails生產 - 所有圖片在新部署後被破壞

我也想注意到,我使用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, "[email protected]: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

好吧,我找到了解決辦法。出現這個問題是因爲我沒有更改默認文件夾來保存圖像。您可以在public/uploads找到您的默認文件夾。這意味着每個cap deploy都將創建一個不包含舊文件的新空文件夾。

要解決此問題,您應該創建另一個文件夾,該文件夾不在您的應用程序中。我選擇最簡單的方法。我創建了symlinlk。

我的步驟:

1)在您的服務器轉到您應用的共享文件夾(它通過Capistrano的自動生成)。然後創建您的文件夾保存新圖片:

$ mkdir uploads 

2)給出一個創建的文件夾所需的權限:

$ sudo chmod 775 uploads 

3)在本地計算機中配置/ 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 

現在一切正常。

+0

嘿,我知道,很多時間都過去了,但我也有同樣的問題。關於你的步驟。我不明白,在第一步中,爲什麼我們需要創建文件夾,如果它自動創建? –

1

好的!我已經延長了你的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