部署 How to generate a new secret token automatically?

匿名 · 2013年03月01日 · 最后由 discovery 回复于 2013年03月02日 · 4022 次阅读

I used Ubuntu server 12.10 to set up a Rails application on Apache with passenger, I export RAILS_ENV=production before rake db:create , then when I tried to run the instance getting this runtime error:

You must set a secret token in config/initializers/secret_token.rb

Well, i executed command "rake secret" and then copied the outputing serial code to secret_token.rb, but that didn't solve my problem. I also tried

$ irb irb(main):007:0>#{SecureRandom.hex(64)}

Any Advice Would be Greatly Appreciated! Thank you!

Paste the content of your secret_token.rb file, you can remove the actual token string if you don't want it to be exposed.

My rough guess is you have secret_token set for Rails.env.development? but not Rakls.env.production?.

匿名 #2 2013年03月01日

#1 楼 @ashchan Thanks a million for your timely help!

匿名 #3 2013年03月01日

#1 楼 @ashchan Here is my default secret_token.rb file:

Definitely change this when you deploy to production. Ours is replaced by jenkins.

This token is used to secure sessions, we don't mind shipping with one to ease test and debug,

however, the stock one should never be used in production, people will be able to crack

session cookies.

#

Generate a new secret with "rake secret". Copy the output of that command and paste it

in your secret_token.rb as the value of Discourse::Application.config.secret_token:

#

Discourse::Application.config.secret_token = "SET_SECRET_HERE"

delete all lines below in production

if Rails.env.test? || Rails.env.development? Discourse::Application.config.secret_token = "6a523af1ac508e67822fa0d270287cddc380248bcb4091ab2234981f1c4c3b9a0557f9965a90aca88e550034260ebedcd37dbf6e8b242804dc41025f2ba8577" else raise "You must set a secret token in config/initializers/secret_token.rb"

Delete all lines below in production OR change if Rails.env.test? || Rails.env.development? to if Rails.env.production? resulted to 500 (Internal Server Error) , need I input a new token which generated from command 'rake secret' again?

Yes run rake secret and paste it to the Discourse::Application.config.secret_token line. Then delete all other lines.

匿名 #5 2013年03月02日

#4 楼 @ashchan 它提示说#delete all lines below in production,貌似在生产环境下不需要 secret_token,把在那一句下面的内容统统删除好了。而我当只保留 Discourse::Application.config.secret_token = "3553012fd9c6bc6784fc940830f73f5f208a9769652f0238a7155c1ec5932d7a77c1a9c04e5a43a3824e879bcc57113ffb0852aa65d130574f020bd832a2a2d1" 保存退出,仍然遭遇内部服务器错误。是否需要执行某命令使这个修改生效?谢谢!

#5 楼 @discovery 需要重启 rails server 使设置生效。

匿名 #7 2013年03月02日

#6 楼 @ashchan Whatever words I use can’t express my thanks to you.

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