在Ubuntu 11.10上使用rails多级部署时出现问题

时间:2012-11-01 08:57:20

标签: ruby-on-rails deployment capistrano ubuntu-11.10

我正在尝试在我的服务器上进行多阶段部署,但我无法正常运行暂存和生产配置。

服务器:Ubuntu 11.10(GNU / Linux 3.0.0-19-服务器x86_64)

这是我到目前为止所做的:

  1. 我在本地计算机上创建了一个名为form_tester的新项目。我在本地使用sqlite3的简单数据库。我希望在staging和production上使用mysql2。

  2. 在服务器上设置登台和制作目录。

    /home/username/form_tester/staging
    /home/username/form_tester/production
    
  3. 在服务器上创建了两个数据库,用于暂存和生产。我还将它们配置为具有正确权限的用户访问,因为我能够在其中生成表。

    ftstaging
    ftproduction
    
  4. 为staging和production配置了config / database.yml文件。

    staging: 
      adapter: mysql2 
      encoding: utf8 
      reconnect: false
      database: ftstaging
      pool: 5 
      username: dbuser 
      password: pass 
      host: server.com
    
    production: 
      adapter: mysql2 
      encoding: utf8 
      reconnect: false
      database: ftproduction
      pool: 5 
      username: dbusr 
      password: pass 
      host: server.com
    
  5. 为暂存和生产配置了Gemfile。

    来源' https://rubygems.org'

    gem 'rails', '3.2.8'
    gem 'jquery-rails'
    gem 'capistrano'
    
    group :development do
      gem 'sqlite3'
    end
    
    group :staging do
      gem 'activerecord-mysql2-adapter'
      gem 'mysql2'
    end
    
    group :production do
      gem 'activerecord-mysql2-adapter'
      gem 'mysql2'
    end
    
    # Gems used only for assets and not required
    # in production environments by default.
    group :assets do
      gem 'sass-rails',   '~> 3.2.3'
      gem 'coffee-rails', '~> 3.2.1'
      gem 'uglifier', '>= 1.0.3'
    end
    
  6. Ran Capify获取部署文件。从deploy.rb文件中删除了所有内容。单独留下Capfile。

  7. 创建了部署目录并制作了两个文件。

    config/deploy/production.rb
    config/deploy/staging.rb
    
  8. 添加了以下配置详细信息:

  9. 对于config / deploy / production.rb:

        server "server.com", :app, :web, :db, :primary => true
        set :deploy_to, "/home/username/form_tester/production"
        set :rails_env, "production"
    

    对于config / deploy / staging.rb:

        server "server.com", :app, :web, :db, :primary => true
        set :deploy_to, "/home/username/form_tester/staging"
        set :rails_env, "staging"
    

    对于config / deploy.rb:

        set :stages, ['production', 'staging']
        set :default_stage, 'staging'
        require 'capistrano/ext/multistage'
    
        # Set application name
        set :application, 'form_tester'
        set :domain, 'server.com'
        set :user, 'username'
    
        # Use Git source control
        set :scm, :git
        set :repository, "ssh://#{user}@#{domain}/home/#{user}/git/#{application}.git"
        set :branch, 'master'
        set :deploy_via, :remote_cache
        set :scm_verbose, true
    
        default_run_options[:pty] = true
        set :use_sudo, false
    
        namespace :deploy do
          task :start do ; end
          task :stop do ; end
    
          desc "Restart application"
          task :restart, :roles => :app, :except => { :no_release => true } do
            run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
          end
        end
    

    9.使用git --bare init在服务器上初始化git repo,并确保我可以正确访问当前用户名的目录。

    10.在dev机器上初始化git repo并将文件推送到服务器repo         SSH://username@server.com/home/username/git/form_tester.git

    11.Begin从本地计算机部署到服务器上的登台。我运行以下所有命令,没有错误。

    在/ home / username / form_tester / staging下构建目录树

        $ cap deploy:setup
        $ cap deploy:check
        $ cap deploy:update
    
        *ssh'ed into my server*
    
        $ rake schema:db:load
        $ rake db:seed
        $ rails console
        > app.get('/')
        I get a '200' returned
        > exit
    

    仍然ssh到我的服务器

    12.我从/ var / www / staging创建了一个符号链接到我的应用程序公共目录

        /home/username/form_tester/staging/current/public.
    
        $ sudo ln -s /home/username/form_tester/staging/current/public /var/www/staging
    

    13.修改/ etc / apache2 / sites-available / default文件,为登台应用程序添加一个subURI。

        $ sudo vi /etc/apache2/sites-available/default
    
        …
                RailsBaseURI /staging
                <Directory /var/www/staging>
                        Options -MultiViews
                </Directory>
        …
    

    14.在这一点(本地)我运行cap deploy:restart,它似乎重新启动服务器,因为没有错误。 (登录到服务器)我也试过sudo service apache2 restart,这也重新启动了服务器。

    15.在这一点上,我拉出了url server.com/staging但是没有看到我的应用程序。

    其他文件:

    配置/ application.rb中:

        require File.expand_path('../boot', __FILE__)
    
        require 'rails/all'
    
        if defined?(Bundler)
          Bundler.require(*Rails.groups(:assets => %w(development test)))
        end
    
        module FormTester
          class Application < Rails::Application
            config.encoding = "utf-8"
            config.filter_parameters += [:password]
            config.active_support.escape_html_entities_in_json = true   
            config.active_record.whitelist_attributes = true
            config.assets.enabled = true
            config.assets.version = '1.0'
          end
        end
    

    config / environments / staging.rb和production.rb

        FormTester::Application.configure do
          config.cache_classes = true
          config.consider_all_requests_local       = false
          config.action_controller.perform_caching = true
          config.serve_static_assets = false
          config.assets.compress = true
          config.assets.compile = false
          config.assets.digest = true
          config.i18n.fallbacks = true
          config.active_support.deprecation = :notify
        end
    

    /var/log/apache2/error.log

    除了幻觉乘客外,似乎没有任何红宝石出现在这里,但这不是错误。

        [Thu Nov 01 01:16:11 2012] [notice] caught SIGTERM, shutting down
        PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626/gd.so' - /usr/lib/php5/20090626/gd.so: cannot open shared object file: No such file or directory in Unknown on line 0
        PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626/mcrypt.so' - /usr/lib/php5/20090626/mcrypt.so: cannot open shared object file: No such file or directory in Unknown on line 0
        [Thu Nov 01 01:16:12 2012] [notice] Apache/2.2.20 (Ubuntu) DAV/2 SVN/1.6.12 PHP/5.3.6-13ubuntu3.7 with Suhosin-Patch Phusion_Passenger/3.0.17 configured -- resuming normal operations
        [Thu Nov 01 01:16:20 2012] [error] [client 68.6.38.254] File does not exist: /var/www/favicon.ico
        [Thu Nov 01 01:16:22 2012] [error] [client 68.6.38.254] File does not exist: /var/www/favicon.ico
        [Thu Nov 01 01:23:07 2012] [error] [client 68.6.38.254] File does not exist: /var/www/favicon.ico
    

    观察:

    1. 运行rake架构:db:load似乎正在为我的数据库正确构建表,但它实际上是为development.sqlite3文件构建的。

    2. 如果我打开rails控制台并运行Rails.env,我会得到&#34; staging&#34;作为环境。

    3. 这很可能与#1相关联,rake db:seed似乎没有填满我的数据库&#34; ftstaging&#34;在服务器上的mysql中,即使它执行。

    4. 上限部署似乎与上限部署不同:更新。

    5. 最终结果是server.com/staging上的一个页面显示&#34;我们很抱歉,但出了点问题。&#34;

    6. 我的

    7. 似乎没有显示任何内容
    8. 运行rake about显示我仍然处于开发模式。

      About your application's environment
      Ruby version              1.9.3 (x86_64-linux)
      RubyGems version          1.8.24
      Rack version              1.4
      Rails version             3.2.8
      JavaScript Runtime        Node.js (V8)
      Active Record version     3.2.8
      Action Pack version       3.2.8
      Active Resource version   3.2.8
      Action Mailer version     3.2.8
      Active Support version    3.2.8
      Middleware                ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000002796948>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport
      Application root          /home/username/form_tester/staging/releases/20121101080752
      Environment               development
      Database adapter          sqlite3
      Database schema version   20121030011807
      
    9. 我尝试了以下教程,运气不佳:

      问题:

      我确定有人了解进出的部署过程,我错过了什么? 我应该检查什么? 根据我在这里展示的内容,我可以采取哪些不同的做法?

      此时非常感谢任何帮助。我已经和这个问题争了两天了,不知道我做错了什么,也不知道还有什么可以尝试。我刚刚开始使用rails大约3周前,我的大部分挫折都是由于无法找到部署过程的清晰描述。我希望能够清楚地了解这个问题,并且可以帮助其他人轻松地从想法转变为多阶段部署。

      另一方面,为什么会有人&#34;向下投票&#34;这个帖子?我问一个有问题的合法问题来支持它。如果这不是一个好问题,请告诉我原因。感谢


      11/1/12 - 更新:

      我想出了如何让我的应用进入&#34; staging&#34;环境,但它仍然没有在server.com/staging加载。

      我添加了&#34; Rails.env = ActiveSupport :: StringInquirer.new(&#39; staging&#39;)&#34;进入我的config / application.rb文件,但我不知道为什么这会强制环境。这是正确的方法吗?

      然后,我将ssh&s进入我的服务器作为用户名,并运行rake db:migrate和rake db:seed。我现在看到我的数据库已填充。

      还有什么可以发生?我的environment / staging.rb文件有问题吗?


      /etc/apache2/httpd.conf

      ServerName server.com
      
      LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.17/ext/apache2/mod_passenger.so
      PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.17
      PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby
      

1 个答案:

答案 0 :(得分:0)

我最终发现了主要问题:资产管道

第一期:我没有从Capfile中取消注释以下行

load 'deploy/assets'

第二个问题:我从未编译资产(在开发机器或服务器端。大呐喊!)

$ bundle exec rake assets:precompile

<强>参考

对任何可能知道的人的跟进问题: 我注意到,当我编译资产时,它表示RAILS_ENV =生产而不是分期。在制作staging.rb文件时,我基本上复制了production.rb文件。我不确定这与生产有什么关系。

这是预期的吗?

/usr/local/rvm/rubies/ruby-1.9.3-p194/bin/ruby /usr/local/rvm/gems/ruby-1.9.3-p194@global/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets