"Access Forbidden" - Passenger, Nginx, Rails - ruby-on-rails

I know there are about 100 questions about this, but after hours of research, I couldn't find my solution. Here's my nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /home/LucyRb/public;
passenger_enabled on;
passenger_app_env production;
index index.html index.htm;
server_name <domain name>;
location / {
try_files $uri $uri/ /index.html;
passenger_enabled on;
}
}
My /public directiory:
root#Lucy:/var/www/LucyRb/public# namei -l *
f: 404.html
-rwxrwxrwx root root 404.html
f: 422.html
-rwxrwxrwx root root 422.html
f: 500.html
-rwxrwxrwx root root 500.html
f: favicon.ico
-rwxrwxrwx root root favicon.ico
f: robots.txt
-rwxrwxrwx root root robots.txt
I don't think it's a permission problem. I've already run rake db:migrate to create the database, and it worked. I'm pretty sure environments are set up properly. All I get is a file listing (since autoindex is on, otherwise I get a 403). But routes, nor does the app default index/root, work. I've read the Passenger documentation, and I tried troubleshooting with the docs and other SO questions, but I can't seem to get this working.
I haven't forgotten to bundle install and all the proper packages are installed, I'm pretty sure (did I forget any? mysql, rvm and its relevant steps for installing rails, nginx, passenger... Probably another 2-3).
I know I'm not supposed to start rails server because it will only allow 1 connection at a time. So nginx is supposed to do that properly, right? Or is that what I'm supposed to do with certain flags?
Thanks in advance.

Okay everyone. With the assistance of Passenger's author on their IRC channel, I was able to resolve the problem.
I had passenger installed twice.
From articles I followed that didn't cover the topic thoroughly enough, I was under the assumption Passenger is an installable module of Nginx, which caused me to install Nginx with apt-get, and Passenger via the gems.
This is an unnecessary redundancy and causes conflicts.
Either use only apt-get or only the gems in these cases.
passenger-install-nginx-module not only installs a module, it also compiles Nginx from scratch, simply because Nginx is not a modulable application. It is either compiled alongside the module, or doesn't have one at all.
To sum it up:
Only use one method to install Nginx && passenger.
Make sure the config points to the right files.
I also had a problem with the paths to ruby. Make sure it points to the wrapper, and not the executable alone:
root#Lucy:/usr/local/rvm# which ruby
/usr/local/rvm/rubies/ruby-2.0.0-p353/bin/ruby
In your config:
passenger_ruby /usr/local/rvm/wrappers/ruby-2.0.0-p353/ruby;

Suggestion-
Try moving your app to someplace that is accessible by normal user.for example home directory

How did you install passenger? With RVM? RVMSUDO or sudo? Chances are you shouldn't be using your app as root. So the permissions on the files aren't allowing passenger to use the rails app.

Assuming your config.ru is in your specified passenger_app_root, try removing your location / {...} directive, restart nginx, and see if your app at least starts up. I'm not saying this is the fix, but a troubleshooting measure.

I was going to place this in my comments section but space and formatting are limited so:
I also see you're specifying passenger_enabled twice, which seems unnecessary. I've never seen it turned on in a location directive before, but that might not be your problem.
What do your logs say? You don't seem to have passenger logging/debugging turned on (in http directive):
http {
...
passenger_debug_log_file '/path/to/passenger_debug.log';
passenger_log_level 5; # default
}
Another thing I'm noticing from your post is that your public directory doesn't seem to contain a rails application??

You have
# nginx config
root /home/LucyRb/public;
# shell
/var/www/LucyRb/public
Do you have a correct app in /home/LucyRb? If so, what is the relevance of /var/www/LucyRb? If this is a mistake in your config, then it if probably going to cause some trouble!

Have you tried to change your files to www-data user?

Every time I've received that error it's been because Nginx wasn't correctly pointing to the passenger gem and executable. Here's what I suggest, to get things working:
Uninstall and/or shut down whatever nginx you've currently got installed.
cd into your app's root directory,
execute the command: passenger-install-nginx-module and let passenger download, compile, and install nginx in /opt.
use the sample config provided by the passenger script for /opt/nginx/conf/nginx.conf.
To get the server to start on boot, just add the one line /opt/nginx/sbin/nginx to your /etc/rc.local script.
This recipe has been repeatable and foolproof for me running RVM + Ruby 1.9, 2.0, and Rails 3 and 4 on Ubuntu 10 and 12.

Related

Trying to set up a simple nginx ruby on rails development. How do I run the app?

I am really struggling to understand this.
I have used PHP / Apache all my life but I just need to have a very basic setup of a Ruby on Rails app on an Nginx server for development.
I have set up an Ubuntu 12.04 server with Vagrant / VirtualBox.
I can see in the web browser the "Welcome to nginx!" message and there's a Ruby on Rails app installed on the server (tested with someone else and working on there server).
The Ruby on Rails application is in an /app/ folder and with these folders:
- assets
- controllers
- helpers
- mailers
- models
- observers
- uploaders
- views
Now, all I need to do is run the thing. Where is my index file?
Are there any other folders I should take note of? There's nothing I can see.
I've tried configuring sites-enabled but none of it makes sense. Surely it should be a simple case of getting my-domain.com to display the index file. Simple, surely? Which file in the /app/ folder am I trying to run?
For development you can use the built-in web-server with rails s in your project folder. It will runs your RoR app on 0.0.0.0:3000. Port can be changed with -p (rails s -p 80).
Read this guide to running rails on nginx using passenger:
Once Ruby on Rails is installed, go ahead and install passenger.
gem install passenger
...
Set the root to the public directory of your new rails project. Your
config should then look something like this:
server {
listen 80;
server_name example.com;
passenger_enabled on;
root /var/www/my_awesome_rails_app/public;
}
(Don't forget the /public after the rails root location!)

Alter Nginx.conf file to route to rails project

Okay, this is my first ever vps, and this is my hello world test. I'm one step away from displaying my rails application
I have a VPS running Ubuntu.
I have a rails project just sitting here:
(I pulled it from git hub using git, and ran 'bundle install' to install the gems on the vps, haven't used Capistrano at all)
root /home/starkers/rails_application ->
test_app
When I visit my vps' ip in a browser I recieve the Welcome to nginx! page.
Finally,
sudo nano /opt/nginx/conf/nginx.conf
Opens my config file.
Now, I haven't made any headway on this in about an hour. All the online guides show a slightly different nginx.conf structure so I'm a little bit in the dark here.
The nginx.conf file is pretty large, what variables do I need to change in order to make a direct http request to the server route to the rails project?
I think this is what matters for my purposes:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
I'd be most grateful if someone could explin this to me a bit more. I'm betting the index.html referenced here is the "Welcome to nginx!" page. Should a public webserver always listen on port 80? Does the server_name matter?
How can I then tweak the nginx.conf file so that a url I purchased will route the project?
Really wouldn't mind some help here.
This is really a multi-step process. You will need to:
Configure a nginx server block for the domain you want to host your app from.
Configure an application server or other "bridge" between your rails app and nginx. I'd recommend the passenger gem.
If you decide to go the passenger route, you could modify the server block above similar to the following:
passenger_root /somewhere/passenger-x.x.x;
passenger_ruby /usr/bin/ruby;
passenger_max_pool_size 10;
server {
# uncomment the line below if you will be serving multiple domains/websites from this box
# server_name example.com www.example.com;
listen 80;
# point this to the path to your rails app's public directory
root /home/starkers/rails_application/test_app/public;
passenger_enabled on;
}
Source: http://www.modrails.com/documentation/Users%20guide%20Nginx.html
This HOW TO install guide from digitalocean may be of interest to you.

403 Error after upgrading passenger (for nginx) is keeping me up at night...where did I go wrong?

I think I've read every question and answer on SO related to passenger/nginx and 403 errors, but none have lead me to a solution, so here I go...
I had Nginx (1.0.6) with Passenger (3.0.9) running beautifully for a rails app for many, many months with no real issues. Tonight I decided to upgrade from Passenger v3.0.9 to v3.0.12 to take advantage of a new feature. After running the install according to the provided instructions (using RVM), I went to the URL served by my rails app and got the dreaded 403 error. The nginx log file first had me thinking it was a permissions error:
directory index of "/home/SimfoUsers/public/" is forbidden, client: , server: , request: "GET / HTTP/1.1", host: ""
But after checking every possible permission, I no longer think that is the issue. I think the problem is actually that passenger is somehow not actually running, and the page is being served as a "normal" webpage by nginx. This is supported by the fact that if I add an index.html file to my rails public directory (/home/SimfoUsers/public/), nginx serves up this file as one would expect. Also, if I run passenger-memory-stats, ZERO passenger processes are running. Shouldn't nginx automatically spawn passenger processes whenever needed, or am I completely missing something here?
Here are the relevant parts of my nginx.conf file:
http {
passenger_root /usr/local/rvm/gems/ruby-1.9.2-p290#Simfo/gems/passenger-3.0.12;
passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.2-p290#Simfo/ruby;
...
server {
listen 80;
server_name simfo.info www.simfo.info;
root /home/SimfoUsers/public;
passenger_enabled on;
}
}
Basically the only thing that I changed from my previously working config file is to update the passenger_root and passenger_ruby directives to reflect the new version of passenger (3.0.12). So if this is a config file issue, I'm really at a loss to understand it...
I'm not sure whether to just delete this question, or leave it in case it can save someone else the same frustration. This definitely is a face palm moment...
In desperation, I finally rebooted the server. After that, everything worked fine. It seems that at some point the passenger-related processes died and were not re-spawned automatically. One would think that after a fresh installation and reboot of nginx, passenger would be restarted, but apparentally this is not the case. My only advice to anyone having a similar problem is to issue a ps aux | grep passenger (or tasklist on Windows?). You should see one or two processes related to passenger. If not, then something strange is going on, and a reboot might help you as well.

Installing Passenger when Nginx is already installed; Possible?

Rather a simple question I believe, is it possible to install passenger when nginx is already installed on your webserver?
If the answer is Yes, I already performed these actions:
At this very moment I already have nginx installed (for my PHP applications) and next I did a checkout of the passenger's git repository:
mkdir /repositories
cd /repositories/
git clone https://github.com/FooBarWidget/passenger.git
cd passenger/
and then add this snippet to /etc/nginx/conf/nginx.conf
http {
...
passenger_root /repositories/passenger;
passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.2-p290/ruby;
...
}
However when I want to restart nginx I get the following error:
* Starting Web Server nginx
nginx: [emerg] unknown directive "passenger_root" in /etc/nginx/nginx.conf:19
Which concludes me to say that there is still some config I need to set, for nginx to be aware that we're using passenger.
My server block
server {
listen 80;
server_name rails.kreatude.com;
root /srv/www/my_test_app;
passenger_enabled on;
}
I think your problem is that the passenger module is not present in nginx.
All the passenger dependent directives you've described (passenger_root, passenger_ruby, passenger_enabled) are available only when the passenger module is attached to nginx. This is why you have to compile nginx with --add-module='/path/to/passenger-3.0.9/ext/nginx'.
Unfortunately, I don't know of any method to enable passenger module without re-installing nginx. But, according to http://wiki.nginx.org/Modules, "Nginx modules must be selected at compile-time.", so there could be a chance that there isn't a way to do that.
In Passenger docs the chapter "Generic installation, upgrade and downgrade method: via RubyGems" discusses this. Basically, once the Passenger gem is installed, nginx needs to be recompiled (and then used instead of the yum/apt-get-installed nginx if one exists). Passenger's compilation/configuration utility "passenger-install-nginx-module" does it for you (it's part of the Passenger gem), and it automatically includes the necessary switches for Passenger. It also gives you the option to add your own switches (such as for extra modules, or to enable/disable NGiNX's built-in features).
With rvm, you could do this simply by running rvmsudo passenger-install-nginx-module.
For more detail: https://www.digitalocean.com/community/tutorials/how-to-install-rails-and-nginx-with-passenger-on-ubuntu.
I confirm ion-br's answer, I'm facing the same kind of problems and PhusionPassenger's site states:
Before you begin, you should know that installing Passenger in its Nginx integration mode involves extending Nginx with code from Passenger. However, Nginx does not support loadable modules. This means that in order to install Passenger's Nginx integration mode, it is necessary to recompile Nginx from source.
The only solution is thus to properly reinstall Nginx, if your machine is an AWS AMI instance the solution lies here.
There is a way install nginx passenger module without reinstalling/recompiling nginx
https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/ownserver/nginx/oss/bionic/install_passenger.html
passenger_enabled on; in server, http, or location block.
http://modrails.com/documentation/Users%20guide%20Nginx.html#_important_deployment_options

Nginx + passenger do not resolve soft link when deploying app to a host root

I am trying different ways of deploying Rails app (redmine, to be specific) with nginx and Passenger. Let's say that I have it installed in /var/local/railapps/redmine-1.1.
When I deploy as sub-uri, I can make the soft link just to public folder:
sudo ln -s /var/local/railapps/redmine-1.1/public /var/www/rails/redmine
and add to server block in nginx.conf:
root /var/www/rails;
passenger_enabled on;
passenger_base_uri /redmine;
With this, redmine can access folders on the same level as public, for example, /var/local/railapps/redmine-1.1/config.
On the other hand, Passenger does not resolve soft links when it is installed to a host root (according to this guide):
sudo ln -s /var/local/railapps/redmine-1.1/public /var/www/rails/redmine
root /var/www/rails/redmine;
passenger_enabled on;
I am getting the error from Passenger: No such file or directory - config/environment.rb in root /var/www/rails, which means that it tries to get relative paths not from the link target, but from the link itself. If I make the link to the main redmine folder, the whole thing works:
sudo ln -s /var/local/railapps/redmine-1.1 /var/www/rails/redmine
root /var/www/rails/redmine/public;
passenger_enabled on;
So, the question is, why is Passenger able to resolve softlinks in "sub-uri" mode, but is not in "host root" mode? Are there some additional settings?
It does not do that in "host root mode" in order to support Capistrano-style deployment directories. This is documented in detail in this section of the documentation of the Apache version. The Apache version allows customizing the symlink resolving behavior (for backward compatibility reasons), while the Nginx version only supports one type of behavior.
Since version 2 of passenger the symlinks are not resolved on document root. You can change that setting with:
PassengerResolveSymlinksInDocumentRoot on

Resources