15

Vì vậy, tôi đang cố gắng để có được ứng dụng ray của tôi để triển khai trong phương thức sản xuất, nhưng tôi nhận được lỗi: Thiếu secret_tokensecret_key_base cho 'sản xuất' môi trường, thiết lập các giá trị trong config/secrets.ymlRails Production - Làm thế nào để thiết lập Secret Key Base?

tập tin secrets.yml của tôi là như mong đợi :

development: 
    secret_key_base: xxxxxxx 

test: 
    secret_key_base: xxxxxxx 

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

Nhưng ngay cả sau khi google và nghiên cứu, tôi không biết phải làm gì với cơ sở khóa sản xuất bí mật. Hầu hết thông tin trên mạng đều cho rằng tôi có kiến ​​thức nền tảng nhất định, nhưng thực tế là tôi là một noob.

Có ai có thể giải thích cho tôi cách đặt khóa bí mật của tôi và làm việc này ở chế độ sản xuất không?

+0

có thể trùng lặp của [Làm thế nào để giải quyết lỗi "Thiếu \' bí mật \ _key \ _base \ 'cho môi trường 'sản xuất' 'trên Heroku (Rails 4.1)] (http://stackoverflow.com/questions/23180650/how-to-solve-error-missing-secret-key-base-for- production-environment-on-h) – mbaitoff

Trả lời

11

Các lỗi bạn đang nhận chỉ cho biết rằng biến môi trường cho secret_key_base không được đặt đúng trên máy chủ.

Bạn có thể sử dụng các tập lệnh khác nhau như capistrano để tự động hóa quá trình thiết lập các cài đặt này trước khi ứng dụng được chạy.

Đối với một sửa chữa nhanh chóng thử điều này:

export SECRET_KEY_BASE=YOUR SECRET BASE 

Validate các biến môi trường và kiểm tra xem chúng đã được thiết lập.

Command:

env | grep -E "SECRET_TOKEN|SECRET_KEY_BASE"

Nếu giá trị của bạn bật lên sau đó chúng được thiết lập trên máy chủ sản xuất.

Ngoài ra, cách tốt nhất là sử dụng ENV.fetch(SECRET_KEY) vì điều này sẽ tăng ngoại lệ trước khi ứng dụng thậm chí cố gắng bắt đầu.

+0

Mã thông báo bí mật của tôi nên là gì? Tôi đã sử dụng $ rake bí ​​mật và nó đã cho tôi một chìa khóa, nhưng những gì về cơ sở? – nvrpicurnose

+3

'rake secret' tạo một chuỗi khóa an toàn để sử dụng như' TOKEN' và 'BASE'. Rails chỉ cần những chức năng này đúng và làm một số công cụ bảo mật đằng sau hậu trường. – JensDebergh

+0

Rails mới nhất không còn cần 'secret_token'; chỉ 'secret_key_base' là bắt buộc. –

4

Như bạn có thể thấy, có một giá trị mã hóa cho các môi trường developmenttest, nhưng một cho production xuất phát từ một biến. Trước hết, tại sao theo cách này? Nó là một tính năng bảo mật. Bằng cách này, nếu bạn kiểm tra tệp này trong điều khiển phiên bản như git hoặc svn, các giá trị developmenttest sẽ được chia sẻ, điều này là tốt, nhưng production một giá trị sẽ được sử dụng trên trang web thực sự. không ai có thể nhìn vào nguồn để có được bí mật đó.

Đối với các biến được sử dụng, ENV["SECRET_KEY_BASE"], đây là một biến môi trường từ Rails môi trường được chạy trong (không nên nhầm lẫn với Rails "môi trường", chẳng hạn như development, test, và production). Các biến môi trường này đến từ trình bao. Như đã đề cập trong bài viết JensD 's, bạn có thể thiết lập biến môi trường này tạm thời với:

export SECRET_TOKEN=YOUR SECRET TOKEN 
export SECRET_KEY_TOKEN=YOUR SECRET BASE 

Để tạo mã bảo mật mới, sử dụng lệnh rake secret trong dòng lệnh.

Đó là tạm thời, tuy nhiên, và không phải là giải pháp cuối cùng tốt. Để có giải pháp cuối cùng, hãy xem this article có bit nhanh gần hết khi triển khai dotenv để tải các bí mật cấu hình. Hãy nhớ rằng, nếu bạn sử dụng kiểm soát phiên bản, hãy chắc chắn loại trừ tệp .env của bạn để không bị đăng ký!

Đặt dotenv lên mất một chút công việc, nhưng tôi khuyên bạn nên cố gắng định cấu hình thủ công các biến môi trường này theo cách thủ công.

+1

Có hướng dẫn từng bước về việc đẩy ứng dụng đường ray vào sản xuất không? Tôi không thể kết hợp tất cả những mảnh ghép này vì tôi thiếu kiến ​​thức nền tảng cần thiết – nvrpicurnose

+1

@nvrpicurnose lol cách học là làm điều đó lặp đi lặp lại cho đến khi nó bắt đầu trở nên dễ dàng hơn. Tôi đã xoay vòng và xé nát các máy chủ nhiều lần trong một thời gian dài cho đến khi tôi nhận được nó. Mất nhiều giờ và nhiều hướng dẫn để thực sự có được nó. Ít nhất, đó là cách mà tôi không có ai nắm tay tôi và chỉ cho tôi. Dính vào nó và nó trở nên dễ dàng hơn. Kiểm tra đọc tôi trên ứng dụng demo này tôi đã cố gắng triển khai. Có thể giúp https://github.com/adiakritos/sw-checkin –

21

Bạn có thể tạo ra các phím bằng cách sử dụng lệnh sau

$ irb 
>> require 'securerandom' 
=> true 
>> SecureRandom.hex(64) 
=> "3fe397575565365108556c3e5549f139e8078a8ec8fd2675a83de96289b30550a266ac04488d7086322efbe573738e7b3ae005b2e3d9afd718aa337fa5e329cf" 
>> exit 
6

Câu trả lời này đã giúp tôi rất nhiều. Ông cho thấy bạn làm thế nào để cấu hình các tập tin secrets.yml trong sản xuất và làm thế nào để đọc nó từ môi trường:

gốc liên kết: https://stackoverflow.com/a/26172408/4962760

I had the same problem and I solved it by creating an environment variable to be loaded every time that I logged in to the production server and made a mini guide of the steps to configure it:

https://gist.github.com/pablosalgadom/4d75f30517edc6230a67

I was using Rails 4.1 with Unicorn v4.8.2, when I tried to deploy my app it didn't start properly and in the unicorn.log file I found this error message:

"app error: Missing secret_key_base for 'production' environment, set this value in config/secrets.yml (RuntimeError)"

After some research I found out that Rails 4.1 changed the way to manage the secret_key, so if you read the secrets.yml file located at [exampleRailsProject]/config/secrets.yml you'll find something like this:

Do not keep production secrets in the repository,

instead read values from the environment. production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> This means that rails

recommends you to use an environment variable for the secret_key_base in your production server, in order to solve this error you should follow this steps to create an environment variable for Linux (in my case Ubuntu) in your production server:

1.- In the terminal of your production server execute the next command:

$ RAILS_ENV=production rake secret This returns a large string with letters and numbers, copy that (we will refer to that code as GENERATED_CODE).

2.1- Login as root user to your server, find this file and edit it: $ vi /etc/profile

Go to the bottom of the file ("SHIFT + G" for capital G in VI)

Write your environment variable with the GENERATED_CODE (Press "i" key to write in VI), be sure to be in a new line at the end of the file:

export SECRET_KEY_BASE=GENERATED_CODE Save the changes and close the file (we push "ESC" key and then write ":x" and "ENTER" key for save and exit in VI)

2.2 But if you login as normal user, lets call it example_user for this gist, you will need to find one of this other files:

$ vi ~/.bash_profile $ vi ~/.bash_login $ vi ~/.profile These files are in order of importance, that means that if you have the first file, then you wouldn't need to write in the others. So if you found this 2 files in your directory "~/.bash_profile" and "~/.profile" you only will have to write in the first one "~/.bash_profile", because Linux will read only this one and the other will be ignored.

Then we go to the bottom of the file ("SHIFT + G" for capital G in VI)

And we will write our environment variable with our GENERATED_CODE (Press "i" key to write in VI), be sure to be in a new line at the end of the file:

export SECRET_KEY_BASE=GENERATED_CODE Having written the code, save the changes and close the file (we push "ESC" key and then write ":x" and "ENTER" key for save and exit in VI)

3.- You can verify that our environment variable is properly set in Linux with this command:

$ printenv | grep SECRET_KEY_BASE or with:

$ echo $SECRET_KEY_BASE When you execute this command, if everything went ok, it will show you the GENERATED_CODE from before. Finally with all the configuration done you should be able to deploy without problems your Rails app with Unicorn or other.

When you close your shell terminal and login again to the production server you will have this environment variable set and ready to use it.

And thats it!! I hope this mini guide help you to solve this error.

Disclaimer: I'm not a Linux or Rails guru, so if you find something wrong or any error I will be glad to fix it!

Các vấn đề liên quan