Puma没有在生产ec2中运行

时间:2018-01-01 12:20:29

标签: ruby-on-rails nginx amazon-ec2 puma capistrano3

我正在尝试使用nginx,puma和capistrano将我的网站部署到amazon ec2我按照本教程https://medium.com/@henslejoseph/deployment-a-rails-app-to-ec2-using-bitbucket-and-semaphoreci-d539bea90db3将应用程序部署到服务器但是当我尝试通过浏览器访问它时通过访问网站url它说这个网站是无法到达的。接下来我尝试的是检查nginx和美洲狮日志我在nginx日志中找不到任何问题,但是在puma_access.log中有错误Errno :: EADDRINUSE:地址已经在使用 - bind(2) for" 0.0.0.0"在此之后我尝试使用lsof -wni tcp:3000和netstat -an检查是否有任何进程正在使用端口3000。 grep 3000但输出为空似乎没有进程连接到端口。 我试图从3天开始调试它,但仍然不知道发生了什么。非常感谢任何帮助我只是附上我的所有配置文件以供参考。

capfile     #加载DSL并设置阶段     要求" capistrano / setup"

# Include default deployment tasks
require "capistrano/deploy"

    require "capistrano/scm/git"
    install_plugin Capistrano::SCM::Git


# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#   https://github.com/capistrano/passenger
#
require "capistrano/rvm"
# require "capistrano/rbenv"
# require "capistrano/chruby"
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/puma"
# require "capistrano/passenger"

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

deploy.rb

# config valid for current version and patch releases of Capistrano
lock "~> 3.10.1"

set :application, "myappname"
set :repo_url, "myapp_url"

# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

set :branch, :deployment_work

# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, "/var/www/my_app_name"
 set :deploy_to, '/home/yogesh/myapp_name'
 # set :scm, :git

# Default value for :format is :airbrussh.
# set :format, :airbrussh

# You can configure the Airbrussh format using :format_options.
# These are the defaults.
# set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto

# Default value for :pty is false
set :pty, true

# Default value for :linked_files is []
# append :linked_files, "config/database.yml", "config/secrets.yml"

set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/application.yml')

set :rvm_type, :user

set :rvm_ruby_version, 'ruby-2.3.3'

set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
set :keep_releases, 3
# Default value for linked_dirs is []
# append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for local_user is ENV['USER']
# set :local_user, -> { `git config user.name`.chomp }

# Default value for keep_releases is 5
# set :keep_releases, 5

# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure

    # Pume config
    set :puma_rackup, -> { File.join(current_path, 'config.ru') }
    set :puma_state, "#{shared_path}/tmp/pids/puma.state"
    set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
    set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"    #accept array for multi-bind
    set :puma_conf, "#{shared_path}/puma.rb"
    set :puma_access_log, "#{shared_path}/log/puma_error.log"
    set :puma_error_log, "#{shared_path}/log/puma_access.log"
    set :puma_role, :app
    set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
    set :puma_threads, [0, 8]
    set :puma_workers, 0
    set :puma_worker_timeout, nil
    set :puma_init_active_record, true
    set :puma_preload_app, false

production.rb

set :stage, :production
    # Replace this EC2 server's public IP with your EC2 server's public IP
    # Replace this user ('sarmad') with the one you created on your EC2 instance
    server 'xx.xxx.xxx.xx', user: 'yogesh', roles: %w{web app db}

    set :ssh_options,{ forward_agent: true, user: 'yogesh', keys: %w(~/.ssh/id_rsa) }

puma.rb

#!/usr/bin/env puma

directory '/home/yogesh/myapp_name/current'
rackup "/home/yogesh/myapp_name/current/config.ru"
environment 'production'

pidfile "/home/yogesh/myapp_name/shared/tmp/pids/puma.pid"
state_path "/home/yogesh/myapp_name/shared/tmp/pids/puma.state"
stdout_redirect '/home/yogesh/myapp_name/shared/log/puma_error.log', '/home/yogesh/myapp_name/shared/log/puma_access.log', true


threads 0,8

bind 'unix:///home/yogesh/myapp_name/shared/tmp/sockets/puma.sock'

workers 0



prune_bundler


on_restart do
  puts 'Refreshing Gemfile'
  ENV["BUNDLE_GEMFILE"] = ""
end

nginxconfig

upstream app {
      # Path to Puma SOCK file, as defined previously
      server unix:///home/yogesh/myapp_name/shared/tmp/sockets/puma.sock fail_timeout=0;
    }
    server {
      listen 80;
      server_name localhost;
      root /home/yogesh/myapp_name/current/public;
      try_files $uri/index.html $uri @app;
      location / {
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Connection '';
        proxy_pass http://app;
      }
      location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
      error_page 500 502 503 504 /500.html;
      client_max_body_size 4G;
      keepalive_timeout 10;
    }

有人可以帮助我,在我出错的地方尝试了所有的stackoverflow问题并且从3天开始搜索了很多,但仍然不知道我的配置是否有错。

1 个答案:

答案 0 :(得分:2)

更新:经过大量研究后我得到了解决方案 你所要做的就是确保sites_available文件夹中的默认文件也应该在sites_enabled文件夹中可用。这个默认文件确保网站已启用。如果默认文件不可用,似乎nginx没有在端口80上侦听。