简体   繁体   中英

How deploy ruby on rails application on nginx with Passanger

I spent lot of hours and just stuck :(

  1. Build app in ruby on rails
  2. On dev environment working with Apache
  3. Have to install production on Ubuntu 18.04 LTS with nginx and passenger
  4. I add Passenger to nginx how it's described by Passenger Library here

App not working and logs showing:

  1. 403 forbidden error for root ( https://cennik.leczymed.pl )

    2019/02/08 21:46:19 [error] 4743#4743: *2 directory index of "/var/www/html/cennik/public/" is forbidden, client: XXX.XXX.XXX.XXX, server: cennik.leczymed.pl, request: "GET / HTTP/2.0", host: "cennik.leczymed.pl"

  2. 404 not found for others sublinks (ex. https://cennik.leczymed.pl/rtg )

    2019/02/08 21:46:25 [error] 4743#4743: *2 open() "/var/www/html/cennik/public/rtg" failed (2: No such file or directory), client: XXX.XXX.XXX.XXX, server: cennik.leczymed.pl, request: "GET /rtg HTTP/2.0", host: "cennik.leczymed.pl"

My configurations:

/etc/nginx/sites-available/cennik.conf

server {
    listen 80;
    server_name cennik.leczymed.pl;

    include snippets/letsencrypt.conf;
    return 301 https://cennik.leczymed.pl$request_uri;
}


server {
    listen 443 ssl http2;
    server_name cennik.leczymed.pl;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/html/cennik/public;

    # log files
    access_log /var/log/nginx/cennik.leczymed.pl.access.log;
    error_log /var/log/nginx/cennik.leczymed.pl.error.log;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /usr/bin/ruby2.5;

    ssl_certificate /etc/letsencrypt/live/cennik.leczymed.pl/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cennik.leczymed.pl/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/cennik.leczymed.pl/chain.pem;
    include snippets/letsencrypt.conf;
    include snippets/ssl.conf;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    location ^~ /assets/ {
     gzip_static on;
     expires max;
     add_header Cache-Control public;
    }

}

All files of application are in /var/www/html/cennik catalog.

drwxr-xr-x 16 cennik cennik 4096 Feb  8 20:53 .
drwxr-xr-x  6 root   root   4096 Feb  8 14:23 ..
drwxrwxr-x 10 cennik cennik 4096 Jul 27  2018 app
drwxr-xr-x  2 cennik cennik 4096 Jul 27  2018 bin
drwxr-xr-x  2 cennik cennik 4096 Feb  8 20:33 .bundle
drwx------  5 cennik cennik 4096 Feb  8 20:48 config
-rw-rw-r--  1 cennik cennik  130 Jul 27  2018 config.ru
drwx------  2 cennik cennik 4096 Jul 27  2018 db
-rw-rw-r--  1 cennik cennik 2371 Feb  8 19:58 Gemfile
-rw-r--r--  1 cennik cennik 5544 Feb  8 20:42 Gemfile.lock
drwxrwxr-x  7 cennik cennik 4096 Feb  8 19:47 .git
-rw-rw-r--  1 cennik cennik  687 Feb  8 19:47 .gitignore
drwxrwxr-x  4 cennik cennik 4096 Jul 27  2018 lib
drwxrwxr-x  2 cennik cennik 4096 Feb  8 20:53 log
drwxr-xr-x  2 cennik cennik 4096 Feb  8 20:53 node_modules
-rw-rw-r--  1 cennik cennik   64 Jul 27  2018 package.json
drwxrwxrwx  3 cennik cennik 4096 Feb  8 20:54 public
-rw-rw-r--  1 cennik cennik  227 Jul 27  2018 Rakefile
-rw-rw-r--  1 cennik cennik    5 Jul 27  2018 .ruby-version
drwxrwxr-x  2 cennik cennik 4096 Jul 27  2018 storage
drwxrwxr-x  9 cennik cennik 4096 Jul 27  2018 test
drwxrwxr-x  6 cennik cennik 4096 Jul 27  2018 tmp
drwxrwxr-x  3 cennik cennik 4096 Feb  8 20:33 vendor
-rw-r--r--  1 cennik cennik   86 Feb  8 20:53 yarn.lock

All files and catalogs owner is cennik user (as described in recipe).

Catalog /public inside RoR app is with all privileges (chmod 777).

Any ideas guys ?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Update:

passenger-config --ruby-command

passenger-config was invoked through the following Ruby interpreter:
  Command: /usr/bin/ruby2.5
  Version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
  To use in Apache: PassengerRuby /usr/bin/ruby2.5
  To use in Nginx : passenger_ruby /usr/bin/ruby2.5
  To use with Standalone: /usr/bin/ruby2.5 /var/lib/gems/2.5.0/gems/passenger-6.0.1/bin/passenger start

The following Ruby interpreter was found first in $PATH:
  Command: /home/artur/.rbenv/shims/ruby
  Version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
  To use in Apache: PassengerRuby /home/artur/.rbenv/shims/ruby
  To use in Nginx : passenger_ruby /home/artur/.rbenv/shims/ruby
  To use with Standalone: /home/artur/.rbenv/shims/ruby /var/lib/gems/2.5.0/gems/passenger-6.0.1/bin/passenger start

So, I think that nginx configuration cennik.conf declare correct place of catalog for ruby : /usr/bin/ruby2.5

Any other ideas how to solve it ?

************************ UPDATE *******************************

Ok. So I found few things which can help others. Problem as mine is only for Ubuntu upgraded to 18.04 from previous versions. Instalation of nginx is not overwrite confs for nginx (this can be solved manually). Bigger problem is that nginx package which is delivered for Ubuntu 18.04 is not compiled with passenger support. So I'm looking now for solution how to compile nginx with passenger module in it and still use it my systemctl functions - installation from sources work, but you can't work with it as a service :(

I found some special receipe for comapiling Ubuntu package with changes inside compilation process but any of my try to put correct add-on for passenger already fail.

Anybody can help me ?

I add Passenger to nginx how it's described by Passenger Library here

Looks like Passenger in not configured. As you're using RVM, you should have another location for ruby executable. To get ruby path, go to application root and run the following command and replace passenger_ruby directive.

> cd /var/www/html/cennik
> passenger-config --ruby-command
# It shows output like this

passenger-config was invoked through the following Ruby interpreter:
  Command: /home/deploy/.rvm/gems/ruby-2.3.5/wrappers/ruby
  Version: ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-linux]
  To use in Apache: PassengerRuby /home/deploy/.rvm/gems/ruby-2.3.5/wrappers/ruby
  To use in Nginx : passenger_ruby /home/deploy/.rvm/gems/ruby-2.3.5/wrappers/ruby
  To use with Standalone: /home/deploy/.rvm/gems/ruby-2.3.5/wrappers/ruby /usr/bin/passenger start

Catalog /public inside RoR app is with all privileges (chmod 777).

Making public directory executable isn't good idea. It may contains files uploaded by users. Just set permissions to 755 and chown to user or group running nginx (www-data).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM