Rails on Webfaction (Passenger and Nginx) - ruby-on-rails

This is my first time deploying a Rails app to a production server, I have already done almost everything. I'm stuck with the process to make the app run in production mode, I already typed
$ export RAILS_ENV=production
and
$ echo $RAILS_ENV
and the terminal throws that I am in the production mode, but when I go to a url in my app not yet defined by me, the server is still debugging the templates, I just want the server throws the default 404 page.
Be patient to me I'm new in this. :)

You need to change the rails_env setting in nginx/conf/nginx.conf, as follows:
server {
listen <port_number>;
passenger_enabled on;
root /home/<username>/webapps/<app_name>/<app>/public;
server_name localhost;
rails_env production;
}
If you do this and are greeted with a "502 Bad Gateway" error for your efforts, the issue may be that you don't have the SECRET_KEY_BASE environment variable set. You can generate a suitable value with rake secret, and then add export SECRET_KEY_BASE="<secret>" wherever you're setting other environment variables.
Webfaction's Rails deployment documentation has improved substantially, but this is one of a number of non-intuitive steps it still skips over.

Related

How to get the passenger_app_env value from shell?

I am currently working on a shell script to manage a service who is part of a Rails App (Nginx + Phusion Passenger) and needs to be launched with systemd after Rails has started.
As a requirement the script needs the Rails environment value and I would like to get it from Passenger (passenger_app_env value).
# /etc/nginx/sites-enabled/webapp.conf:
server {
...
# Ensures that RAILS_ENV, NODE_ENV, etc are set to "staging"
# when your application is started.
passenger_app_env staging;
}
I was trying with passenger-config about OPTION, env, and printenv to get that value but without luck.
Does anybody know a way to get the value of passenger_app_env from the OS shell?

Nginx "The page you were looking for doesn't exist" after switch to production mode

I´m trying to deploy a rails app to a VPS, using Ubuntu, Nginx and passenger.
Everything runs ok if I set things up on "development" mode, inside /etc/nginx/sites-enabled/testapp:
passenger_app_env development;
As soon as I change to production mode and restart nginx, it starts to give me the "The page you were looking for doesn't exist"
I checked access.log and error.log and there no new register after this error comes up.
Heres my sites-enabled/testapp:
server {
listen 80 default_server;
server_name myvaliddomain.com; *#I´m actually using a valid domain here.*
passenger_enabled on;
passenger_app_env production;
root /home/hal/testapp/public;
}
Thanks in advance for any kind of help.
house9 was almost right. It was a silly database mistake. Although the production database was there, I did not run the latest migrations using RAILS_ENV = production, so they only existed on dev database. Thanks!
I just had to run:
RAILS_ENV=production rake db:migrate

502 Bad Gateway when switching rails app to production

I have an issue when trying to deploy my rails app in production mode on my server using phusion passenger with nginx.
My app runs perfectly fine in development mode, with this configuration file :
server {
listen 80;
server_name domain.co;
root /home/me/projects/myapp/public;
passenger_enabled on;
rack_env development;
}
What I did is simply switch the development with production.
When I do this, I simply get a "502 Bad Gateway" message.
A production.log file is created, but it is 0 bytes.
I could not find any other log that indicate whether there is an issue or not (nothing else in the log folder, nothing in /etc/var/nginx/. . .).
When I try to run passenger-status in my project structure, passenger tells me it is not running here. I get a proper status message when I do the same thing in development mode.
My guess is that passenger tries to start the app but something fails early in the process; the issue is that I can't get to know why because I am unable to find any log.
I have found several other questions on Stack Overflow about the same problem, but most of them get some kind of log. I guess if I could get access to the error message somehow that would help.
Is there anything obvious I miss?
Thanks,
Julien
And as usual, Stack Overflow is great for rubber duck debugging and now I feel extremely stupid.
The issue seems to come from the fact that I put my SECRET_KEY_BASE environment variable in the .bashrc file; which doesn't work on my production environment.
If I put the export statement in **~/.rvm/environment/ruby***** instead, everything works fine. . .

passenger/nginx - environment variable passing, or passenger variable passing

We have 2 rails environments with the same code. One is staging, the other production. Both run using the same 'production' rails settings.
But, we'd like to know what environment we are on. I thought I could set an environment variable on the servers and read that out inside of rails, but, while that works from the console it doesn't work when running behind passenger.
in the admin users .profile file I have:
export MC_TEST=true
But the nginx's/passengers running don't seem to be inside that same environment, and that variable is not set.
I thought I could then maybe put something in the nginx.conf to get passed back to the rails environment, but obviously I am not doing that right either!
So - can anyone tell me how to properly do this? Under no circumstances do we want this variable in ruby/rails/app code.
If you are using nginx with passenger (i.e passenger included in nginx installation), the configuration is as follows:
server {
server_name www.foo.com;
root /webapps/foo/public;
passenger_enabled on;
passenger_env_var DATABASE_USERNAME foo_db;
passenger_env_var DATABASE_PASSWORD secret;
}
https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_env_var
Try passenger_set_cgi_param MC_TEST true in your nginx configuration
passenger_set_cgi_param docs

"502 Bad Gateway" with passenger

I am using rails(2.3.10)/passenger(2.2.15)/nginx(0.7.67) , when i run my application, it give me "502 Bad Gateway" and without any production log, my conf file is :
1 server {
2 listen 80;
3 server_name www.why.bz;
4 root /usr/local/apps/why/pro/public;
5 access_log /usr/local/apps/why/pro/log/access.log;
6 passenger_enabled on;
7 }
who can give me some tips, thanks!
I've done Rails deployments full-time for the last couple of years, so hopefully I can help. You've probably figured this problem out by now, but here are some questions to think about for next time:
Since this is a Ruby on Rails application, can you tell where the error is coming from: Nginx or Rails itself? That sounds more like a Nginx issue, but it would be good to know. A quicky peek into the Rails production.log or Nginx's error_log should elucidate this info.
How are you connecting to the application: web browser or command line? Is the IP and hostname set correctly (try the Linux "dig" command to verify)? Are you trying to hit http://localhost:80 ?
There are error log commands that nginx takes that could be configured to help you debug the issue (much like the access_log line): http://wiki.nginx.org/CoreModule#error_log
Lastly, verify the permissions that are set on your web files: The nginx web server needs to be able to access them. At least "read" permissions are needed.
Good luck,
Harmon
change it to
listen 443;
it could be as simple as that.
It could be that your secret key base is missing for the required stage.
One thing that I sometimes forget, especially if I don't start with a staging environment and then add it later, is to add the following to my secrets.yml file:
staging:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
This is telling Rails to get the secret_key_base value from the environment variable, SECRET_KEY_BASE. You must specify this variable by placing this in your nginx configuration:
passenger_env_var SECRET_KEY_BASE yoursupersecretkeybase;
If you don't have a SECRET_KEY_BASE, you can generate one with:
bundle exec rake secret

Resources