【问题标题】:Deploying rails with capistrano + Nginx + Passenger + MYSQL to VPS使用 capistrano + Nginx + Passenger + MYSQL 将 Rails 部署到 VPS
【发布时间】:2012-09-08 18:52:03
【问题描述】:

这是我第一次部署 Rails 应用程序,我发现这个过程非常令人沮丧。出于工作相关的原因,我们使用带有 Ubuntu 12.04 LTS(Precise Pangolin)和 MYSQL 的 Rackspace 云,而不是 Heroku 路由。

我已经尝试解决这个问题 2 多天了,我终于向社区寻求帮助。目前我收到“我的服务器上未找到 404 错误”

我已经关注了 Ryan 的“部署到 VPS”、“Capistrano 食谱”、this tutorial 以及 google 上的其他内容等的截屏视频,但我还没有完成。

我设法安装了以下内容:

  • Node.js
  • RVM
  • ruby 1.9.3p194(2012-04-20 修订版 35410)[x86_64-linux]
  • Rails 3.2.8
  • MYSQL
  • 乘客
  • Nginx

我很确定我在这里的 Capistrano Deployer 中遗漏了一些简单的东西:

require "bundler/capistrano"

server "198.101.242.242", :web, :app, :db, primary: true

set :application, "myapp"
set :user, "deployer"
set :deploy_to, "/home/#{user}/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false

set :scm, "git"
set :repository, "git@github.com:xxxx/#{application}.git"
set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

# if you want to clean up old releases on each deploy uncomment this:
after "deploy", "deploy:cleanup"

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

在部署之前我应该​​做些什么吗?

这里是 Nginx 错误日志:

2012/09/16 23:22:17 [error] 13939#0: *1 "/home/deployer/myapp/public/index.html" is not found (2: No such file or directory), client: ip, server: localhost, request: "GET / HTTP/1.1", host: "ip"
2012/09/16 23:22:17 [error] 13939#0: *1 open() "/home/deployer/myapp/public/favicon.ico" failed (2: No such file or directory), client: ip, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "65.61.189.109"
2012/09/16 23:22:19 [error] 13939#0: *1 "/home/deployer/myapp/public/index.html" is not found (2: No such file or directory), client: ip, server: localhost, request: "GET / HTTP/1.1", host: "ip"
2012/09/16 23:22:19 [error] 13939#0: *1 open() "/home/deployer/myapp/public/favicon.ico" failed (2: No such file or directory), client: ip, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "ip"

Nginx 服务器配置文件:

    #user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    passenger_root /home/deployer/.rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.17;
    passenger_ruby /home/deployer/.rvm/wrappers/ruby-1.9.3-p194/ruby;

    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {

      listen 80;
      server_name localhost;
      location / {
            root /home/deployer/myapp/public;   # <--- be sure to point to 'public'!
      }
      passenger_enabled on;

        # listen       80;
        # server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # location / {
        #     root   html;
        #     index  index.html index.htm;
        # }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        # error_page   500 502 503 504  /50x.html;
        # location = /50x.html {
        #     root   html;
        # }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

   # server {
   #    listen 80;
   #    server_name localhost;
   #      location / {
   #            root /home/deployer/myapp/public;   # <--- be sure to point to 'public'!
   #    }
   #      passenger_enabled on;
   # }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

谢谢。

【问题讨论】:

  • 您是否尝试过将应用程序的 nginx 配置放在新的服务器块中?我记得我遇到过类似的事情。
  • 感谢您的建议,我刚刚根据您的评论尝试过,但没有成功。
  • 我不确定日志文件中“客户端”IP 地址的用途。这根本不正确。我不知道这是从哪里来的
  • 我认为您就是从那里访​​问该网站的。你的 cap deploy 命令运行成功了吗?
  • 您的 Bundler 出了点问题,请查看 stackoverflow.com/questions/3737678/…

标签: deployment nginx ruby-on-rails-3.2 capistrano passenger


【解决方案1】:

好的,我最终通过反复试验和Beef Jerky's cmets 找出了问题所在。

第一个问题是 capistrano 无法从我的应用程序中安装一些具有依赖项的 gem。所以我将它们安装在生产环境中……特别是 rvm capistrano gem 和 rmagick。 RVM Capistrano gem 是我部署时遇到错误的主要问题和原因。 rmagick 是在解决了我通过安装它的依赖项解决的 rvm 问题之后出现的。

我遇到的第二个问题是指向我的应用程序文件夹 myapp/public,而不是 Nginx 服务器配置文件中的当前版本 myapp/current/public。所以我保留了现有的服务器块并将我的设置放在里面,然后它就可以工作了。以下是 Nginx 服务器配置文件的设置

server {

      listen 80;
      server_name localhost;
      root /home/deployer/banking_analytics/current/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;

        # listen       80;
        # server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # location / {
        #     root   html;
        #     index  index.html index.htm;
        # }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        # error_page   500 502 503 504  /50x.html;
        # location = /50x.html {
        #     root   html;
        # }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

   # server {
   #    listen 80;
   #    server_name localhost;
   #      location / {
   #            root /home/deployer/banking_analytics/public;   # <--- be sure to point to 'public'!
   #    }
   #      passenger_enabled on;
   # }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

【讨论】:

  • 您的回答和问题都没有变得简洁。您是否有动力为后代编辑它们?
  • “myapp/current/public”文件夹从何而来?我只有“myapp/public”
  • Capistrano 应该在部署时创建 /current /releases 和 /shared。检查您的 capistrano 日志
【解决方案2】:

前段时间我在第一次部署时遇到了类似的错误。

就我而言,我在文件 mod-http-passenger.conf 中设置了错误的路径

我的建议是在 nginx 设置中启用passenger_friendly_error_pages 以获取更多错误信息。在某些情况下,它似乎比日志更好。

在服务器端

sudo vim /etc/nginx/sites-enabled/myApp

添加

passenger_friendly_error_pages on;

【讨论】:

    猜你喜欢
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    相关资源
    最近更新 更多