Reputation: 1806
I am trying to get a multistage deployment on my server and I am having trouble getting a staging and production configuration running.
Server: Ubuntu 11.10 (GNU/Linux 3.0.0-19-server x86_64)
Here is what I have done so far:
I created a new project called form_tester on my local machine. I am using a simple database with sqlite3 locally. I wish to use mysql2 on both staging and production.
Setup directories for staging and production on server.
/home/username/form_tester/staging
/home/username/form_tester/production
Made two databases on server for staging and production. I also configured them for access by a user with the correct privileges since I am able to generate the tables in them.
ftstaging
ftproduction
Configured the config/database.yml file for both staging and production.
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
Configured the Gemfile for both staging and production.
source '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
Ran Capify to get deployment files. Deleted everything from the deploy.rb file. Left the Capfile alone.
Created deploy directory and made two files.
config/deploy/production.rb
config/deploy/staging.rb
Added the following details for configuration:
For config/deploy/production.rb:
server "server.com", :app, :web, :db, :primary => true
set :deploy_to, "/home/username/form_tester/production"
set :rails_env, "production"
For config/deploy/staging.rb:
server "server.com", :app, :web, :db, :primary => true
set :deploy_to, "/home/username/form_tester/staging"
set :rails_env, "staging"
For 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.Initialized git repo on server with git --bare init and made sure I have proper access to the directory for the current username.
10.Initialized git repo on dev machine and pushed the files to the server repo at
ssh://[email protected]/home/username/git/form_tester.git.
11.Begin deployment from local machine to staging on server. I run all the following commands with no errors.
To build the directory tree under /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
still ssh'ed into my server
12.I made a symlink from /var/www/staging to my application public directory
/home/username/form_tester/staging/current/public.
$ sudo ln -s /home/username/form_tester/staging/current/public /var/www/staging
13.Modified the /etc/apache2/sites-available/default file to add a subURI for the staging app.
$ sudo vi /etc/apache2/sites-available/default
…
RailsBaseURI /staging
<Directory /var/www/staging>
Options -MultiViews
</Directory>
…
14.At this point (locally) I run cap deploy:restart and it seems to restart the server since there is no error. (logged into the server) I also tried sudo service apache2 restart, which also restarts the server fine.
15.At this point I pull up the url server.com/staging but don't see my app.
Other files:
config/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 and 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
There seems to be nothing ruby related showing up here except phusion passenger, but it's not an error.
[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
Observations:
Running rake schema:db:load appears to build the tables properly for my database but its actually building them for the development.sqlite3 file.
If I open the rails console and run Rails.env, I get "staging" as the environment.
This is most likely tied with #1, rake db:seed doesn't seem to fill my database called "ftstaging" in mysql on the server even though it executes.
cap deploy doesn't seem to do anything different than cap deploy:update.
The end result is a page at server.com/staging that says "We're sorry, but something went wrong."
There doesn't seem to be anything shown in my
Running rake about shows that I am still in development mode.
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
I have tried the following tutorials with little luck:
Questions:
I'm sure there is someone out there that understands the deployment process in and out, what am I missing? What should I check for? From what I have shown here, what can I do differently?
Any help is greatly appreciated at this point. I have been fighting with this issue for two days now and don't know what I'm doing wrong and don't know what else to try. I just started using rails about 3 weeks ago and most of my frustration has been due to not being able to find a clear description of the process for deployment. I hope to gain clarity on this issue and in return help others get from idea to multistage deployment easily.
On another note, why would someone "down vote" this post? I'm asking a legit question with information to back it up. If it's not a good question please tell me why. Thanks
11/1/12 - UPDATE:
I figured out how to get my app into the "staging" environment but it still doesn't load at server.com/staging.
I added "Rails.env = ActiveSupport::StringInquirer.new('staging')" into my config/application.rb file, but I don't know why this forces the environment. Is this the correct way to do it?
I then deployed, ssh'ed into my server as username, and ran rake db:migrate and rake db:seed. I now see my database populated.
What else could be going on? Is there something wrong with my environments/staging.rb file?
/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
Upvotes: 0
Views: 491
Reputation: 1806
I FINALLY discovered the the primary issue: the asset pipeline
First issue: I didn't uncomment the following line form the Capfile
load 'deploy/assets'
AND
Second issue: I never compiled the assets (on development machine or server side. big whoops!)
$ bundle exec rake assets:precompile
References
Followup question to anyone who may know: I noticed that when I compiled the assets, it says RAILS_ENV=production instead of staging. When making the staging.rb file I basically copied the production.rb file. I'm not sure if that has anything to do with it saying production.
Is this expected?
/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
Upvotes: 0