使用mina + unicorn + nginx部署后未加载站点

时间:2018-06-10 06:17:30

标签: ruby-on-rails nginx web-deployment unicorn mina

我是部署新手,我有一个使用Ruby 2.4.1和rails v 5.1的rails应用程序。我使用了独角兽的mina部署作为应用服务器,nginx是web服务器。我使用mina部署工具部署了应用程序,它显示结果是成功的。我在nginx中检查了我的独角兽日志,没有发现错误。我根据不同的教程更新了mina和nginx.conf,但没有给出结果。 nginx运行良好。我已经将gcloud用于部署提供程序。

mina deploy.rb文件

all-open

配置/ unicorn.rb

require 'mina/git'
require 'mina/rbenv'
require 'mina/bundler'
require 'mina/rails'
require 'mina/unicorn'
require 'mina/rvm'

# preset defaults
set :application_name, 'blog_app'
set :domain, 'XXXXXX'
set :deploy_to, 'path'
set :repository, 'url'
set :branch, 'staging'
set :user, 'ggg'
set :rails_env, 'staging'
set :identity_file, "blog_app.pem"

set :forward_agent, true
set :port, '22'
set :unicorn_pid, "#{fetch(:deploy_to)}/shared/pids/unicorn.pid"

set :shared_files, ['config/database.yml', 'log', 'config/secrets.yml', '.env', 'public/uploads']
# rails pid
set :pid_file, "#{fetch(:deploy_to)}/shared/pids/#{fetch(:rails_env)}.pid"

task :remote_environment do
invoke :'rvm:use', 'ruby-2.4.1@default'
end

task :setup => :remote_environment do

command %[mkdir -p "#{fetch(:deploy_to)}/shared/log"]
command %[chmod g+rx,u+rwx "#{fetch(:deploy_to)}/shared/log"]

command %[mkdir -p "#{fetch(:deploy_to)}/shared/config"]
command %[chmod g+rx,u+rwx "#{fetch(:deploy_to)}/shared/config"]

command %[touch "#{fetch(:deploy_to)}/shared/config/database.yml"]
command  %[echo "-----> Be sure to edit 'shared/config/database.yml'."]

command %[touch "#{fetch(:deploy_to)}/shared/config/secrets.yml"]
command %[echo "-----> Be sure to edit 'shared/config/secrets.yml'."]

command %[touch "#{fetch(:deploy_to)}/shared/config/config.yml"]
command %[echo "-----> Be sure to edit 'shared/config/config.yml'."]

command %[touch "#{fetch(:deploy_to)}/.env"]
command %[echo "-----> Be sure to edit '.env"]

# sidekiq needs a place to store its pid file and log file
command %[mkdir -p "#{fetch(:deploy_to)}/shared/pids/"]
command %[chmod g+rx,u+rwx "#{fetch(:deploy_to)}/shared/pids"]
end

desc "Deploys the current version to the server."
task :deploy => :remote_environment do
deploy do

on :prepare do
  command %[kill -9 `cat #{fetch(:pid_file)}`]
end

comment "Deploying #{fetch(:application_name)} to #{fetch(:domain)}:#{fetch(:deploy_to)}"
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
# invoke :'rails:db_seed'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'

on :launch do
  invoke :'unicorn:restart'
  command "cd #{fetch(:deploy_to)}/current ; mkdir -p tmp ; touch tmp/restart.txt"
end
end
end

desc "Restart unicorn using 'upgrade'"
task :restart => :remote_environment do
invoke 'unicorn:stop'
invoke 'unicorn:start'
end

/etc/nginx/nginx.conf

# encoding: utf-8
# Set your full path to application.
app_dir = File.expand_path('../../', __FILE__)
shared_dir = File.expand_path('../../../shared/', __FILE__)

# set unicorn options
worker_processes 2
preload_app true
timeout 30

# fill path to your app
working_directory app_dir

# Set up socket location
listen "#{shared_dir}/sockets/unicorn.sock", backlog: 64

# Loging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"

# Set master PID location
pid "#{shared_dir}/pids/unicorn.pid"

before_fork do |server, worker|
 defined?(ActiveRecord::Base) && 
 ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
 begin
  sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
  Process.kill(sig, File.read(old_pid).to_i)
 rescue Errno::ENOENT, Errno::ESRCH
  # someone else did our job for us
 end
end
end

after_fork do |server, worker|
 defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
end

before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{app_dir}/Gemfile"
end

的/ etc / nginx的/位点可用/ blog_app

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {


    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    upstream app {
    server unix:/path/shared/sockets/unicorn.sock fail_timeout=0;
    }
}

我也有符号链接所有已启用网站。

server {
listen 80;
server_name ip-address;

root /path/current;

try_files $uri/index.html $uri @app;

location @app {
   proxy_pass http://app;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
   proxy_redirect off;
}

error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

测试nginx的状态时

 cd /etc/nginx/sites-enabled
 sudo ln -s ../sites-available/blog_app

也在运行命令时

 sudo /etc/init.d/nginx status

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-06-10 05:55:45 UTC; 56min ago
  Process: 16869 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 15991 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
  Process: 16886 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 16874 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 16894 (nginx)
    Tasks: 2
   Memory: 2.4M
      CPU: 25ms
   CGroup: /system.slice/nginx.service
           ├─16894 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─16897 nginx: worker process

Jun 10 05:55:45 staging systemd[1]: Stopped A high performance web server and a reverse proxy server.
Jun 10 05:55:45 staging systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 10 05:55:45 staging systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
Jun 10 05:55:45 staging systemd[1]: Started A high performance web server and a reverse proxy server.
Jun 10 05:57:01 staging systemd[1]: Started A high performance web server and a reverse proxy server.

也在运行mina deploy之后  将给出以下结果。

sudo nginx -t -c /etc/nginx/nginx.conf

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

所有教程都显示我的配置没有问题。

1 个答案:

答案 0 :(得分:0)

经过2天的调试,我找到了解决方案,默认情况下Google Cloud不允许端口连接。为了测试在终端中运行命令

telnet ipaddress port-number

然后按照GCP提供的以下链接中所述创建新的防火墙规则。

link to GCP doc