2

I'm trying to deploy my Rails app to my Digital Ocean VPS. I've done all the steps in the GoRails guide but when I deploy my project and visit my ip adres I'm greeted by a message:

An error occurred.

Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check the error log for details.

Faithfully yours, nginx.

I've checked the nginx error log which shows me:

Exception RuntimeError in Rack application object (Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`) (process 2490, thread 0x007fbd33665ce0(Worker 1)):

On the VPS I've created a secret by running rake secret and then running export SECRET_KEY_BASE= + key. Now when I echo $SECRET_KEY_BASE it shows me my key. But still I'm getting the error message.

In my deploy.rb I have this:

set :linked_files, %w{config/database.yml config/secrets.yml}

And my secrets.yml on the VPS is this:

development:
  secret_key_base: 89dacb16fd905ff4c6352ac93f4676a5dd&^%3f93edce9a5be796712d54b57b91e1335598fd73e3998fddbbdeaf3ee7f65157f2fb01ce1bea5658aa7bf745d1f

test:
  secret_key_base: cf351585b2cb43459f5a073cbfd885b3dd2af44124f13a855522f678c1cf06625c121cd3b7857&&6e7fe2ba11180066753142143231c79c513e71e20372a0462

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

I just restarted nginx, but still no result.

Update

deploy@movieseat:~$ ps aux | grep nginx
root      4545  0.0  0.6  98936  3360 ?        Ss   11:32   0:00 nginx: master process         /usr/sbin/nginx
www-data  4548  0.0  0.9  99304  4608 ?        S    11:32   0:00 nginx: worker process
www-data  4549  0.0  0.9  99304  4608 ?        S    11:32   0:00 nginx: worker process
www-data  4550  0.0  0.9  99304  4608 ?        S    11:32   0:00 nginx: worker process
www-data  4551  0.0  0.9  99304  4608 ?        S    11:32   0:00 nginx: worker process
deploy    4655  0.0  0.3   9436  1660 pts/1    S+   11:33   0:00 grep --color=auto nginx
Peter Boomsma
  • 8,851
  • 16
  • 93
  • 185
  • Make sure that `SECRET_KEY_BASE` is set for the same user as nginx and passenger runs. – blelump Nov 02 '14 at 16:22
  • How would I check that? I log in using `ssh deploy@***.**.***.**` and then I run all the commands. I've also restarted nginx as that user. So I think nginx and passenger run from the deploy user. – Peter Boomsma Nov 02 '14 at 16:25
  • It doesn't show any output. `deploy@movieseat:~$ ls -la | grep nginx deploy@movieseat:~$` – Peter Boomsma Nov 02 '14 at 16:28
  • Sorry, I'm sleeping, I meant: `ps aux | grep nginx` – blelump Nov 02 '14 at 16:30
  • @PeterBoomsma : Try this: 1: run: `RAILS_ENV=production rake secret`, 2: open your bashrc: `vi ~/.bashrc`, put this in it: `export SECRET_KEY_BASE = value_you_get_from_rake_secret_command_without_quote` for example: `export SECRET_KEY_BASE=abjdbjd28hewu83tg`, 3: run: `source ~/.bashrc` or relogin to server, 4: restart the rails server. – Surya Nov 02 '14 at 16:31
  • @blelump I've added the output of that command in my question. – Peter Boomsma Nov 02 '14 at 16:36
  • @User089247 I've tried to run that command but I'm getting back `bundler: command not found: RAILS_ENV=production` I've run it in the root folder of my app on my VPS. – Peter Boomsma Nov 02 '14 at 16:36
  • @PeterBoomsma : If you're not able to run `RAILS_ENV=production rake secret` then check if any other ruby commands working, i.e. bundle etc. – Surya Nov 02 '14 at 16:38
  • @blelump : Don't checkout the secrets.yml in code, if you do then set key in bashrc as only who have access to your server will know the key(which mean you can catch hold of people if somebody's session hijacking), because sessions.yml holds the salt key to set the session(to verify the integrity of signed cookies) of user. That's why you shouldn't be throwing it around in a file. Before this was a static 30 characters used to be stored in config/secret_toke.rb, since Rails 4 they have moved it to be dynamic in production using secret.yml. – Surya Nov 02 '14 at 16:46
  • @User089247 do I have to run `rake secret` with `RAILS_ENV=production`? I tried it without and added `export SECRET_KEY_BASE=+key` (you made a small typo using too many spaces) and restarted my nginx. But still getting the same message in my nginx log. I can do a `bundle install` command. But when I run `rake secret` I get a `You have already activated rake 10.1.0, but your Gemfile requires rake 10.3.2.` message. Not sure if that's of any importance. – Peter Boomsma Nov 02 '14 at 16:57
  • @PeterBoomsma : not necessarily, but why you can't run it `RAILS_ENV=production` is a considerable question. Is your server setup properly? What errors are you getting? Yes, I meant `export SECRET_KEY_BASE=KEY`( I showed that in example, too). – Surya Nov 02 '14 at 17:01
  • @PeterBoomsma : run it with: `bundle exec rake secret`. Also make sure you reload your bash with `source ~/.bashrc` or re login to server and then re start the server. – Surya Nov 02 '14 at 17:02
  • @PeterBoomsma, what if you would restart only passenger? I mean being within your Rails root directory, run `touch tmp/restart.txt`. – blelump Nov 02 '14 at 17:07
  • Doing `touch tmp/restart.txt` in my rails app root folder doesn't give any output. If I try it outside that folder it outputs an error, so I guess "something" is happening. But it's still giving the secret key error in my nginx log. – Peter Boomsma Nov 02 '14 at 17:14
  • Don't know if this has to do with anything but, running `rails c` in my root app folder show a error `WARNING:root:could not open file '/etc/apt/sources.list.d/passenger.list' ` Since we're talking about passenger I thought maybe ... – Peter Boomsma Nov 02 '14 at 17:22
  • Does `ps aux | grep passenger` give any output? If so, on what user does it run (first column)? If it's deploy user, it probably runs within a subshell. Go with @User089247 suggestion then (adding `export SECRET_KEY_BASE=key` to `~/.bashrc`). – blelump Nov 02 '14 at 17:29
  • @blelump It runs as `deploy`. I've added `export SECRET_KEY_BASE=18ea74e2f599887b56d5950bd56d8ea41e48d2c9719c8de8998b26f0fdbd4af82c8bdd532dab5252f5363a62fa31a7e59c7869d6858ae7dee48786c3123****` at the bottom of my `~/.bashrc` file. But no progress. – Peter Boomsma Nov 02 '14 at 17:31
  • Have you restarted after that? – blelump Nov 02 '14 at 17:47
  • Yes, seems that adding the secret directly to the `secrets.yml` file is the way to go. Now I'm getting a error concerning the database. The production database is empty, but when I run `bundle exec RAILS_ENV=production rake db:create` it results in `bundler: command not found: RAILS_ENV=production` although I can do this command locally – Peter Boomsma Nov 02 '14 at 18:04

1 Answers1

0

I would assume that you hadn't checked in the secrets.yml file in the repository as you have defined it as a linked file in deploy.rb i.e

set :linked_files, %w{config/database.yml config/secrets.yml}

You can do the same for the secrets.yml like database.yml. My recommendation is to put the value of secret_key_base itself in the secret.yml file instead of reading it from the ENV as the person who have access to linked file in the deploy server would have access to ENV vars anyway.

But, if you have added the secrets.yml file in the repository, it makes sense to try to read the value from the system environments.

ranendra
  • 2,492
  • 2
  • 19
  • 29
  • Okay I've created a secret on my VPS and added it to my `secrets.yml` also on the VPS. I'm getting a `We're sorry, but something went wrong. If you are the application owner check the logs for more information.` The console shows a `Failed to load resource: the server responded with a status of 500 (Internal Server Error)` error now. And my error log from nginx isn't logging anything when I visit the page. Which could be a good thing. – Peter Boomsma Nov 02 '14 at 17:53
  • `I, [2014-11-02T12:54:45.760056 #10530] INFO -- : Completed 500 Internal Server Error in 2ms F, [2014-11-02T12:54:45.762918 #10530] FATAL -- : ActiveRecord::StatementInvalid (Could not find table 'users'): app/controllers/application_controller.rb:18:in `current_user' app/helpers/sessions_helper.rb:26:in `logged_in?' app/controllers/movies_controller.rb:5:in `index'` is what I'm getting now. So going to check out what's wrong with the database. – Peter Boomsma Nov 02 '14 at 17:56
  • @PeterBoomsma: The recent error is something different. Its complaining that there is no users table. Did you run migration during or after deploy? – ranendra Nov 02 '14 at 17:58
  • Yea I'm seeing that :). I feel like it's almost done. After ~3 days. I've done `rake db:create` and rake db:migrate` but that doesn't do anything. Then I looked in the the db folder and saw that the `production.sqlite3` file is empty. So I guess that's the problem now. Well guess I'm full circle, because I had this earlier, and now again. `bundle exec RAILS_ENV=production rake db:create bundler: command not found: RAILS_ENV=production ` – Peter Boomsma Nov 02 '14 at 18:00
  • Can you check whether ruby version manager is loaded or not before executing the bundle command or do 'which ruby'? – ranendra Nov 02 '14 at 18:08
  • `deploy@movieseat:~/movieseat/current$ which ruby /home/deploy/.rbenv/shims/ruby` and `deploy@movieseat:~/movieseat/current$ rbenv -v rbenv 0.4.0-129-g7e0e85b` – Peter Boomsma Nov 02 '14 at 18:13
  • I found this post > http://stackoverflow.com/questions/23287357/already-activated-rake-version-different-than-what-gemfile-requires-prevents-rak which showed to `gem install rubygems-bundler` and `$ $ gem regenerate_binstubs`. After that I could finally `RAILS_ENV=production rake db:create` and `RAILS_ENV=production rake db:migrate@ and now finally my app is working online. – Peter Boomsma Nov 02 '14 at 18:18