新手问题 [已解决] 关于部署的小白问题

chairy11 · 2014年11月18日 · 最后由 Rei 回复于 2014年12月08日 · 15389 次阅读

问题

跟了好多教程,装了 passenger + nginx(本机第二个,因为之前已经有另一个 nginx,passenger 无法在原 nginx 上装), 配置了 nginx 文件

user  root;         
server {
    listen       8888;
    server_name  219.141.189.60;
    charset utf-8;
    root  /var/www/weekly.com/public;
    passenger_enabled on;
    rails_env production;
  }

也已经

 ./nginx -s reload
touch tmp/restart.txt

也已经

rake db:migrate RAILS_ENV=production
rake db:seed RAILS_ENV=production
rake assets:precompile RAILS_ENV=production

可是打开网址,还是说: 『An error occurred. Sorry, the page you are looking for is currently unavailable. Please try again later.』 但因为 public 目录下有 404.html 嘛,这个是可以打开的。

我在想,我是遗漏了什么步骤吗?是 rails 没有跑起来? 我本来猜想是不是应该像本机 development 那样,来一句:

rails server -e production 

可是这样打开的还是 Booting WEBrick,我要用的是 nginx 呀……

查了很多教程貌似都没有提到要额外的什么命令……我这是少了什么啊?google 半天不得其解……

问题根源 + 解决方案

查看 error.log,发现问题在于:

Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`

解决方案是

  • 生成 key shell rake secret RAILS_ENV=production
  • 复制生成的 key, 修改 config/secrets.yml 中的 production 中的: <%= ENV["SECRET_KEY_BASE"] %>

谢谢各位:)

nginx 日志呢? /var/log/nginx/error.log 同新,但我依照 wiki 尝试 nginx+passenger 就成功了

你要用 passenger 的话,需要用 passenger 这个 gem 提供的的命令行来安装 nginx,这样安装完成后,nignx 会自带 passenger 模块。 你也说了,你机子本身是有一个 nginx 的,要么卸掉这个 nginx,用 passenger 重装;要么换 thin、unicorn。

#2 楼 @kungs 没有卸掉原有的,是因为那个已经有人在用了。貌似一个服务器同时用两个 nginx 是可以的,我们用不同端口,他用 80 的,我用 8888.

@chairy11 使用 docker 吧,这样就可以部署 nginx 了。每一个服务使用一个 docker image 吧

看 nginx/passenger/rails 的 Log。

#4 楼 @floger 方案暂时就不改了吧,折腾这么久了。就是想着为什么现在没跑起来,我少了哪些步骤……

#3 楼 @chairy11 不是你这个用法,一个 nginx 就可以代理两个网站,端口一个 80,一个 8888,而不是装两个 nginx 来解决问题。 现在既然已经有一个 nginx 在用了,你又不想卸掉,比较简单的解决方案是用 thin 替代 passenger,在原来的 nginx 上修改配置,将 8888 端口代理到 thin 就 ok 了。

#7 楼 @kungs 我知道一个 niginx 可以代理两个网站,但问题是我想装 passenger,装 passenger 中说一定要再装一个平行的(因为 niginx 不能动态加载模块),或者把原有的卸载,所以我只能再装一个啊! 反正方案是选定了的,我现在就解决现在这个方案中遇到的问题而已啊!不一定一遇到问题就要换方案的啊!

#1 楼 @although2013 #5 楼 @Rei 没看懂呀……

2014/11/18 09:18:18 [error] 8971#0: *48 upstream prematurely closed connection while reading response header from upstream, client: xxx, server: xxx, request: "GET / HTTP/1.1", upstream: "passenger:/tmp/passenger.1.0.6206/generation-6/request:", host: "xxx:8888"

用这个

netstat -lnp | grep 8888
tcp        0      0 0.0.0.0:8888                0.0.0.0:*                   LISTEN      6206/nginx   

#1 楼 @although2013 #5 楼 @Rei 找到上上一条的错误,这个提示好像能找到问题

*** Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`) 

问题在于,这个,我打开 config/secrets.yml, 看到

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

可是我没有明白 ENV["SECRET_KEY_BASE"] 在哪设置啊?

ENV 是环境变量,可以在 .bashrc 里面设置,适合用在 heroku 部署。

简单的说,用 rake secret 生成一条随机字符串,替换掉那个 <%= %>

#11 楼 @Rei 直接就在 config/secrets.yml 里写就行了?我看到这个教程,貌似就有点复杂,需要跟着做么?

#12 楼 @chairy11 直接写行了。你有几个应用部署在一台服务器,还不知道区分用户了没有,用环境变量来做应用设置可能会互相干扰。

#13 楼 @Rei 有几个人在用这个服务器,但每个都用 root。本来我想给自己单建个超级用户的,结果这错那错整天说我没权限,就又用 root 了……

#14 楼 @chairy11 WoW!任何一个应用有漏洞都会导致整个服务器被攻破。

#15 楼 @Rei 没事,我先练练手,这是个测试服务器,哈哈……

#16 楼 @chairy11 “可是这样打开的还是 Booting WEBrick,我要用的是 nginx 呀……”运行 Rails 分两层,一个是 rake 服务器,一个是 http 服务器,我不确定 passager 是什么情况……但是 rails s 之后肯定不会显示 nginx 在运行……

#17 楼 @cassiuschen 哈哈,解决了,不用运行那句的,我只是 production: secret_key_base:没设置好。

这个存在两个 nginx 的问题,是因为你没有仔细阅读 passenger 官方的文档。是可以在原有的 nginx 上加 module 的。secret_key 不建议用环境变量,可以生成后复制进去. 另外 nginx 的 user 不赞成用 root, 可另外创建一个用户。别小看这种随意的写法。等到数据不可恢复时,就痛苦了. nginx 的 error log 是可以配置的。最好是配置到好使的地方。

#20 楼 @hanluner 不能在原有 nginx 上加 module,你看这段

Nginx is a different from other web servers in that it does not support loadable modules. The only way to extend Nginx is to recompile it entirely from source. Since Phusion Passenger consists of some external executables plus an Nginx module, you must recompile Nginx when first installing Phusion Passenger, but also when upgrading Nginx itself or when upgrading the Phusion Passenger version.

If you’ve already installed Nginx before, but without Phusion Passenger support, then you should uninstall it first. You don’t have to, because you can also install another Nginx with Phusion Passenger support, in parallel to the existing Nginx. We merely recommend uninstalling the existing in order to avoid user confusion, but the choice is yours.

#21 楼 @chairy11 实际上是可以的,https://www.phusionpassenger.com/documentation/Users%20guide%20Nginx.html#install_on_debian_ubuntu 通过为测试也成功了

sudo apt-get install nginx-extras passenger

#/etc/nginx/nginx.conf
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /home/deploy/.rvm/wrappers/default/ruby;

#22 楼 @qingxp9 这应该是替换了 Ubuntu 官方源的 nginx。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号