running a Rails app on a fresh installation of Ngi

2019-08-30 22:37发布

问题:

I have a VPS running Ubuntu 12.04. I've installed Nginx and Passenger on it by following these and these instructions, and as per the instructions, I've added to the http block of file /etc/nginx/nginx.conf the following lines:

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/ruby;

and the following block (where I've replaced the actual domain name by www.example.com):

server {
    listen 80;
    server_name www.example.com;
    root /srv/rails/myapp/public;
    passenger_enabled on;
}

The Rails application lives in directory /srv/rails/myapp/, which is also a Git project directory. The directory and all its contents are owned by user rails. I've made the directory world-writeable, and, as user rails, run the following commands in it:

git pull
gem install bundler
bundle install
rake db:migrate
RAILS_ENV=production bundle exec rake assets:precompile

After this, running the application in development mode succeeds: when I command rails s, and open http://www.example.com:3000, the application works fine.

However, after doing the all the above, and commanding sudo service nginx restart, opening http://www.example.com yields a Passenger error screen, which says:

no such file to load -- bundler/setup (LoadError)
  /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
  /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `require'
  /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:245:in `run_load_path_setup_code'
  /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:348:in `running_bundler'
  /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:243:in `run_load_path_setup_code'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:100:in `preload_app'
  /usr/share/passenger/helper-scripts/rack-preloader.rb:158

There's lots of debug output below that, and I'm not sure which of it is useful, but here's an excerpt:

Application root

    /srv/rails/myapp

Environment (value of RAILS_ENV, RACK_ENV, WSGI_ENV and PASSENGER_ENV)

    production

Ruby interpreter command

    /usr/bin/ruby

User and groups

    uid=1001(rails) gid=1002(rails) groups=1002(rails)

Environment variables

    SHELL = /bin/bash
    PASSENGER_DEBUG_DIR = /tmp/passenger.spawn-debug.XXXX2fPMPM
    USER = rails
    PWD = /srv/rails/myapp
    SHLVL = 0
    HOME = /home/rails
    LOGNAME = rails
    SERVER_SOFTWARE = nginx/1.4.6
    IN_PASSENGER = 1
    PYTHONUNBUFFERED = 1
    NODE_PATH = /usr/share/passenger/node
    RAILS_ENV = production
    RACK_ENV = production
    WSGI_ENV = production
    NODE_ENV = production
    PASSENGER_APP_ENV = production
    SERVER_PROTOCOL = HTTP/1.1
    SCGI = 1
    DOCUMENT_ROOT = /srv/rails/myapp/public
    QUERY_STRING =
    SERVER_NAME = www.example.com
    REMOTE_PORT = 49316
    REMOTE_ADDR = [redacted]
    SERVER_PORT = 80
    REQUEST_METHOD = GET
    SERVER_ADDR = [redacted]
    REQUEST_URI = /

...

General Ruby interpreter information

    RUBY_VERSION = 1.8.7
    RUBY_PLATFORM = x86_64-linux
    RUBY_ENGINE = nil
    RubyGems version = 1.8.15

The application uses Ruby v2.0.0p353 while Passenger seems to use v1.8.7; I'm not sure if that has anything to do with this problem. (The Ruby used by the app lives in /home/rails/.rvm/rubies/ruby-2.0.0-p353/.)

I've searched for other problems where Passengers outputs this error, but nothing I've tried so far fixes the problem.

回答1:

I don't really know if this will help, but try, in your projects folder:

which ruby # for example /home/rails/.rvm/rubies/ruby-2.0.0-p353/bin/ruby

And this will output which ruby your bundle is using. Put that value, replacing this:

passenger_ruby /usr/bin/ruby;

to this

passenger_ruby /home/rails/.rvm/rubies/ruby-2.0.0-p353/bin/ruby

end

edit:

this Linux command:

echo $MY_RUBY_HOME/bin/ruby

might be the easiest and most correct way to find your ruby binary.



回答2:

After trying various things, I gave up on having nginx installed from Debian packages. I removed it, and also removed my rubies and RVM, then reinstalled everything, following these directions. The article advises how to install nginx using the passenger-install-nginx-module command that comes with the passenger gem. It checks all dependencies, and if it can proceed, downloads and compiles nginx. By default, it's installed in /opt/nginx/.

This did not work immediately. I also had to create an nginx startup script; instructions here. Furthermore, I had to edit the /opt/nginx/conf/nginx.conf file to add a reference to my application, and also had to comment out the location / block. After all this, and commanding sudo service nginx restart, the site is up.