用capistrano+Nginx+Passenger+MYSQL部署rails到VPS。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用capistrano+Nginx+Passenger+MYSQL部署rails到VPS。相关的知识,希望对你有一定的参考价值。

这是我第一次部署rails应用,我发现这个过程非常令人沮丧。由于工作原因,我们使用的是Rackspace云计算,Ubuntu 12.04 LTS(Precise Pangolin)和mysql,而不是Heroku路线。

我已经想了2天多,终于向社区求助。目前我得到的是 "404 Not Found Error on my server"(在我的服务器上找不到错误)。

我关注了Ryan的 "部署到VPS"、"Capistrano Recipes "的屏幕直播。本教程和其他在谷歌等,我仍然没有完全达到。

我设法安装了以下内容。

  • Node. js
  • RVM
  • ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
  • Rails 3.2.8
  • Rails 3.2.8
  • 乘客
  • 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;
    #    }
    #}

}

谢谢。

答案

好吧,所以我最终用试错的方式找出了问题,并从 牛肉干的 评论。

第一个问题是capistrano无法从我的应用程序中安装一些有依赖关系的gem。所以我在生产上安装了这些......特别是rvm capistrano gem和rmagick。RVM Capistrano gem是主要问题,也是我在depoloyed时出现错误的原因。rmagick 是在解决了 rvm 的问题之后出现的,我通过安装它的依赖程序解决了这个问题。

我遇到的第二个问题是指向我的应用程序文件夹myapppublic,而不是Nginx服务器配置文件中的当前版本myappcurrentpublic。所以我保留了现有的服务器块,并将我的设置放在里面,然后就可以了。以下是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;
    #    }
    #}

}
另一答案

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

在我的例子中,我在mod-http-passenger.conf文件中设置了一个错误的路径。

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

在服务器端

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

添加

passenger_friendly_error_pages on;

以上是关于用capistrano+Nginx+Passenger+MYSQL部署rails到VPS。的主要内容,如果未能解决你的问题,请参考以下文章

ruby Capistrano 2为乘客提供Nginx的部署脚本

capistrano 部署 - 基本命令失败

Capistrano + Puma + Apache

rails应用的部署

利用Fabric+Capistrano实现Python自动化部署

/usr/bin/env ruby​​ 没有这样的文件或目录:使用 capistrano 3、capistrano/rbenv、capistrano/bundler 和 capistrano/rail