Search code examples
ruby-on-railsrubydeploymentcapistranocentos7

Deploying RoR on Centos 7 and Capistrano doesn't seem to start the server anywhere


Background info: CentOS 7, Ruby 2.2.3, Rails 4.2.4, Capistrano 3.5.0 - And I am a mere Rails developer, not a sys-admin, so much of the server stuff is over my head and I understand it JUST enough to get by occasionally...

The system admin I'm working with decided to use this tutorial to set up rails on a fresh Centos 7 VM: https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-centos-7

I then pointed him to the middle of this tutorial in order to set up a deployers user that I could use in capistrano: https://www.digitalocean.com/community/tutorials/how-to-automate-ruby-on-rails-application-deployments-using-capistrano

I then used capistrano to try and push Ruby on Rails app code to the VM.

It seems when he set up centos (not sure if it was him or the service that provided the VM), the IP address port 80 is already displaying some non-rails content. Because of this, I'm guessing the best bet is to try to deploy to port 3000 and have the sys-admin forward that to port 80? Regardless, I have no idea how to get capistrano to start rails with that specific binding. When I run cap production deploy it succeeds without error, but I have no idea where it is running/how to access it in a browser. If I SSH into the VM, cd /home/deployer/rails/railsapp/current and then run rails s --binding=0.0.0.0 --environment=production I can then access the site via http://the-public-ip:3000. Obviously this is only good to test that the code works while I have the command line up and running, but is no good for real deployment.

So my question is: how do I get capistrano to deploy my rails app, in production mode, to port 3000? Or should I tell the sys-admin to do something that enables capistrano to start rails the out-of-the-box way (without having to do anything special in the capistrano tasks or bind specifically to 0.0.0.0 port 3000).

Here is my deploy.rb

lock '3.5.0'

set :application, 'xxxxxxx'
set :repo_url, 'https://xxxxxxx:[email protected]/xxxxxxx/xxxxxxx.git'

set :deploy_to, '/home/deployer/rails/railsapp/'

set :scm, :git
set :branch, "staging-deploy"

set :use_sudo, false
set :rails_env, "production"
set :deploy_via, :copy
set :ssh_options, {:forward_agent => true}

set :rails_env, "production"

set :keep_releases, 5

ipaddresses = [
    'xx.xx.xxx.xxx'
]

role :web, ipaddresses
role :app, ipaddresses

set :log_level, :info
set :pty, true

namespace :deploy do
  after :publishing, :restart
end

And here is the deploy/production.rb:

ipaddresses = [
    'xx.xx.xxx.xxx'
]

ipaddresses.each do |ip|
  server ip,
         user:        'deployer',
         roles:       %w{web app},
         ssh_options: {
             forward_agent: false,
             auth_methods: %w(password),
             password: 'thepasswordhere',
             user: 'deployer'
         }
end

Additional tidbit: everything I'm doing as far as deployment/SSH and accessing the site via browser is done while connected via VPN through Cisco AnyConnect. Not sure how this affects things.

In my deploy.rb, I naively tried this, of course met with no success:

namespace :deploy do
  desc 'Restart application'
  task :restart do
    on roles(:all), in: :sequence, wait: 5 do
      execute "rails s --binding=0.0.0.0 --environment=production"
    end
  end

  after :publishing, :restart
end

Solution

  • I think there's a misunderstanding. What capistrano will do for you is basically copying your Rails app into a directory on the server. You would still need a web server running your app. In theory, you could ssh to your server and run rails s --binding=0.0.0.0 --environment=production (as you wrote), but this would use Ruby's built in webrick server which is for development only (it's not suitable to run in production for various reasons).

    There are several options to host your app in production, e.g. Nginx or Apache together with Passenger.