Rails - Nginx, Passenger is not responding - ruby-on-rails

I have a problem to get rails running. I'm using nginx, passenger for first time and cannot get it working.
This site can’t be reached
I followed this tutorial on phusionpassenger, and checked 3 times. Just instead of pulling theirs demoapp I made a new one.
Ubuntu 16.04 (Xenial)
node -v = v4.8.0
ruby -v = ruby 2.4.0p0 (2016-12-24 revision 57164)
[x86_64-linux]
rails -v = Rails 5.0.2
Passenger Installation:
sudo /usr/bin/passenger-config validate-install
* Checking whether this Passenger install is in PATH... ✓
* Checking whether there are no other Passenger installations... ✓
Memory Stats:
---------- Nginx processes -----------
PID PPID VMSize Private Name
--------------------------------------
28956 1 174.3 MB 0.5 MB nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
28961 28956 174.3 MB 0.6 MB nginx: worker process
### Processes: 2
### Total private dirty RSS: 1.16 MB
----- Passenger processes -----
PID VMSize Private Name
-------------------------------
28936 441.1 MB 1.2 MB Passenger watchdog
28939 654.1 MB 2.9 MB Passenger core
28945 449.3 MB 1.3 MB Passenger ust-router
### Processes: 3
### Total private dirty RSS: 5.43 MB
passenger.conf
passenger_ruby /usr/local/rvm/gems/ruby-2.4.0/wrappers/ruby;
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
added line to nginx.conf
http {
...
include /etc/nginx/passenger.conf;
...
}
nginx app setting: (quant.conf)
server {
listen 80;
server_name (AWS PUBLIC IP);
# Tell Nginx and Passenger where your app's 'public' directory is
root /home/ubuntu/quant/public;
rails_env production;
# Turn on Passenger
passenger_enabled on;
passenger_ruby /usr/local/rvm/gems/ruby-2.4.0/wrappers/ruby;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Thanks for any help with this.

Related

Nginx, Puma, Ubuntu 20.04 error 111: Connection refused

When I try to use NGINX and Puma I get the following error:
[error] 13416#13416: *3 connect() to unix:///home/deploy/app/tmp/pids/puma.sock failed (111: Connection refused) while connecting to upstream, client: ip.address.redacted, server: myapp.com, request: "GET / HTTP/1.1", upstream: "http://unix:///home/deploy/app/tmp/pids/puma.sock:/", host: "ip.address.redacted"
Here's just a quick breakdown of what I've done to set up this server:
I'm using RBENV
I'm using Ruby 3.1.0 and Rails 6.1
I am not using Capistrano
this server is set up to disable root access and instead use a sudo-privileged user named deploy
When I run sudo service puma status I get the following
● puma.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/puma.service; disabled; vendor preset: enabled)
Active: activating (start) since Thu 2022-01-27 23:59:45 UTC; 28s ago
Main PID: 1365 (bundle)
Tasks: 12 (limit: 2274)
Memory: 155.1M
CGroup: /system.slice/puma.service
└─1365 puma 4.3.10 (tcp://0.0.0.0:3000,unix:///home/deploy/app/tmp/pids/puma.sock) [app]
Jan 27 23:59:45 localhost systemd[1]: Starting Puma HTTP Server...
Jan 27 23:59:48 localhost rbenv[1365]: Puma starting in single mode...
Jan 27 23:59:48 localhost rbenv[1365]: * Version 4.3.10 (ruby 3.1.0-p0), codename: Mysterious Traveller
Jan 27 23:59:48 localhost rbenv[1365]: * Min threads: 5, max threads: 5
Jan 27 23:59:48 localhost rbenv[1365]: * Environment: production
Jan 27 23:59:51 localhost rbenv[1365]: * Listening on tcp://0.0.0.0:3000
Jan 27 23:59:51 localhost rbenv[1365]: * Listening on unix:///home/deploy/app/tmp/pids/puma.sock
Jan 27 23:59:51 localhost rbenv[1365]: Use Ctrl-C to stop
ls -l /home/deploy/app/tmp/pids/puma.sock produces: srwxrwxrwx 1 deploy users 0 Jan 28 00:05 /home/deploy/app/tmp/pids/puma.sock
Here's the Nginx config file found at: /etc/nginx/sites-enabled/default
upstream myapp {
server unix:///home/deploy/app/tmp/pids/puma.sock;
}
server {
listen 80;
# server_name myapp.com;
# ~2 seconds is often enough for most folks to parse HTML/CSS and
# retrieve needed images/icons/frames, connections are cheap in
# nginx so increasing this is generally safe...
keepalive_timeout 5;
# path for static files
root /home/deploy/app/public;
access_log /home/deploy/app/log/nginx.access.log;
error_log /home/deploy/app/log/nginx.error.log info;
# this rewrites all the requests to the maintenance.html
# page if it exists in the doc root. This is for capistrano's
# disable web task
if (-f $document_root/maintenance.html) {
rewrite ^(.*)$ /maintenance.html last;
break;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
# If the file exists as a static file serve it directly without
# running all the other rewrite tests on it
if (-f $request_filename) {
break;
}
# check for index.html for directory index
# if it's there on the filesystem then rewrite
# the url to add /index.html to the end of it
# and then break to send it to the next config rules.
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
# this is the meat of the rack page caching config
# it adds .html to the end of the url and then checks
# the filesystem for that file. If it exists, then we
# rewrite the url to have explicit .html on the end
# and then send it on its way to the next config rule.
# if there is no file on the fs then it sets all the
# necessary headers and proxies to our upstream pumas
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://myapp;
break;
}
}
# Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
# BUT there's a chance it could break the ajax calls.
location ~* \.(ico|css|gif|jpe?g|png|js)(\?[0-9]+)?$ {
expires max;
break;
}
# Error pages
# error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/deploy/app/public;
}
I've added a user directive to nginx.conf thinking this would attempt to run the NGINX connection as the deploy user. However, this doesn't have any effect. Here's the first few lines of my Nginx.conf file
user deploy;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
At this point I'm not sure what to do. I don't have any experience with Puma so Im not sure if I'm doing something wrong with the config. As far as I know the puma.sock file is auto generated, Im not sure how to change the permissions/owner of the sock file or if thats even the right thing to do.
Has anyone encountered this before? What mistake am I making here?
I think you have too many / in the upstream configuration entry. It's trying to connect as if it was an HTTP url.
Try changing:
upstream myapp {
server unix:///home/deploy/app/tmp/pids/puma.sock;
}
to
upstream myapp {
server unix:/home/deploy/app/tmp/pids/puma.sock;
}
Source: nginx ngx_http_upstream_module documentation

Nginx passenger integration mode always require Passengerfile.json file?

I've followed this installation guide to setup nginx and passenger integration mode:
https://www.phusionpassenger.com/library/install/nginx/install/oss/el7/
I'm aware that "Passenger in its Nginx integration mode is to be configured via the Nginx configuration file. There is no configuration file that is specific to Passenger only" => No Passengerfile.json in root application directory.
But after configured passenger.conf and virtual host config and started nginx, the errors occurred:
2019/05/29 09:13:22 [alert] 16126#0: *3 Error opening '/home/deploy/my-app/current/Passengerfile.json' for reading: Permission denied (errno=13); This error means that the Nginx worker process (PID 16126, running as UID 997) does not have permission to access this file
I've tried to give permissions to nginx worker process following this instruction: https://www.phusionpassenger.com/library/admin/nginx/troubleshooting/ruby/#upon-accessing-the-web-app-nginx-reports-a-permission-denied-error
But can't solve the problem.
My passenger.conf inside /etc/nginx/conf.d
passenger.conf
# To enable the Phusion Passenger application server (www.phusionpassenger.com),
# install the following package:
#
# yum install passenger
#
# Then uncomment these options:
passenger_root /usr/share/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /home/deploy/.rbenv/shims/ruby;
passenger_instance_registry_dir /var/run/passenger-instreg;
My app configuration file:
server {
listen 80;
listen [::]:80;
server_name myapp.com;
root /home/deploy/my-app/current/public;
passenger_enabled on;
passenger_ruby /home/deploy/.rbenv/shims/ruby;
rails_env production;
location /cable {
passenger_app_group_name myapp_websocket;
passenger_force_max_concurrent_requests_per_process 0;
}
# Allow uploads up to 100MB in size
client_max_body_size 100m;
location ~ ^/(assets|packs|uploads) {
expires max;
gzip_static on;
}
}
passenger-memory-stats
---------- Nginx processes -----------
PID PPID VMSize Private Name
--------------------------------------
16121 1 113.5 MB 0.6 MB nginx: master process /usr/sbin/nginx
16125 16121 113.7 MB 0.8 MB nginx: worker process
16126 16121 113.7 MB 0.8 MB nginx: worker process
### Processes: 3
### Total private dirty RSS: 2.13 MB
----- Passenger processes -----
PID VMSize Private Name
-------------------------------
16109 355.0 MB 2.1 MB Passenger watchdog
16112 923.4 MB 4.0 MB Passenger core
### Processes: 2
### Total private dirty RSS: 6.11 MB
passenger-config restart-app /home/deploy/my-app
There are no Phusion Passenger-served applications running whose paths begin with '/home/deploy/my-app'.
Thanks in advance!
We were having the same issue; solved by adding missing execute permissions to the user home directory. Hope this helps
chmod o+x $HOME
All directories above your application directory need to have world execute permissions.
This is the permission error to resolve it run the following command in you ec2 instance or any other machine.
1. Become root user:
sudo -i
Go to home directort:
cd /home
Give execute permission to your user home directory:
chmod g+x,o+x ec2-user
Now restart you server nginx
sudo kill $(cat /opt/nginx/logs/nginx.pid)
sudo /opt/nginx/sbin/nginx

Rails/Nginx/Passenger ERROR: Phusion Passenger doesn't seem to be running

I was setting up Passenger and Nginx for my Rails App (Mac OS X 10.11).
I used these commands:
gem install passenger
rvmsudo passenger-install-nginx-module
All these gets installed perfectly.
Added nginx path in .bash_profile:
export PATH=$PATH:/opt/nginx/sbin/
My nginx conf:
passenger_root /Users/MyUserName/.rvm/gems/ruby-2.2.5#rails4115/gems/passenger-5.3.4;
passenger_ruby /Users/MyUserName/.rvm/gems/ruby-2.2.5#rails4115/wrappers/ruby;
server {
listen 443;
ssl on;
server_name app1-local.staging.com;
rails_env development;
passenger_enabled on;
root /Users/MyUsername/Desktop/Github/MainApp/public;
ssl_certificate /opt/nginx/ssl/mainapp.com.crt;
ssl_certificate_key /opt/nginx/ssl/mainapp.com.key;
}
server {
listen 443;
ssl on;
server_name app2-local.staging.com app3-local.staging.com app4-local.staging.com local.staging.com;
rails_env development;
passenger_enabled on;
root /Users/MyUsername/Desktop/Github/MainApp2/public;
ssl_certificate /opt/nginx/ssl/mainapp.com.crt;
ssl_certificate_key /opt/nginx/ssl/mainapp.com.key;
}
server {
listen 80;
server_name app1-local.staging.com;
rails_env development;
passenger_enabled on;
root /Users/MyUsername/Desktop/Github/MainApp/public;
}
server {
listen 80;
server_name app2-local.staging.com app3-local.staging.com app4-local.staging.com local.staging.com;
rails_env development;
passenger_enabled on;
root /Users/MyUsername/Desktop/Github/MainApp2/public;
}
After this I started nginx: sudo nginx, it worked. Now if I visit local.staging.com, I get 502 internal server error.
Nginx error logs displays:
upstream prematurely closed connection while reading response header from upstream
If I do passenger-status, it outputs:
Phusion Passenger is currently not serving any applications.
Note: on running, passenger-config validate-install command, it says Everything looks good. :-)
EDIT
Output of sudo passenger-memory-stats:
-------- Apache processes --------
----------- Nginx processes ------------
PID PPID VMSize Resident Name
62366 1 2411.6 MB 1.3 MB nginx: master process nginx
63016 62366 2411.6 MB 1.5 MB nginx: worker process
63008 28339 2377.8 MB 0.3 MB tail -f /opt/nginx/logs/error.log
------ Passenger processes ------
PID VMSize Resident Name
36415 0.0 MB 0.0 MB (PassengerAgent)
63010 2416.8 MB 3.3 MB Passenger watchdog
63014 2417.6 MB 3.3 MB Passenger ust-router
63602 2457.3 MB 5.7 MB Passenger core
EDIT 2
Now in nginx error.log, getting:
Process aborted! signo=SIGSEGV(11), reason=#0, signal sent by PID 0 with UID 0, si_addr=0x0, randomSeed=1534502322
And
upstream prematurely closed connection while reading response header from upstream
Any help here?

moving from development to production, passenger fails to connect to application

when I change
passenger_enabled on;
rails_env development;
to following passenger documentation that does state Default: rails_env production;
passenger_enabled on;
rails_env production;
followed by sudo service nginx restart on Ubuntu 16.04 with nginx, the nginx error.log registers
Could not spawn process for application [...]/current: An error occurred while starting up the preloader
Message from application: undefined method `skip_authorization_check' for SessionsController:Class
the motive regarding a sessions_controller method skip_authorization_check which is a method used by devise authentication gem... I do not believe that is the real source of the error, as passenger does its job just fine in development mode. In fact reverting to development runs all functions as expected, served up un https.
What I am missing?
update as suggested in answer, the following is obtained for monitoring nginx processes
ps aux|grep nginx root 10373 0.0 0.2 180124 5756 ? Ss 14:26 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 10378 0.0 0.6 180480 13336 ? S 14:26 0:00 nginx: worker process
www-data 10381 0.0 0.5 180124 11404 ? S 14:26 0:00 nginx: worker process
deploy 16182 0.0 0.0 12944 896 pts/0 S+ 16:49 0:00 grep --color=auto nginx
This passenger readme could be helpful. https://github.com/phusion/passenger/wiki/Debugging-application-startup-problems.
I personally would check to make sure there are no stale nginx processes floating around with ps aux|grep nginx and fire a kill -9 or -15 if 9 is too harsh.
Then try running in development mode and tail -f log/development.log just to ensure everything is working. Also depending on your rails version, rails_env may be deprecated. Try rack_env or passenger_app_env from this answer: https://stackoverflow.com/a/20845689/1679747. Cheers.

Passenger + NGINX: Phusion Passenger is currently not serving any applications

Phusion Passenger + NGINX in production environment
I have done everything as usual with my ROR-application, but passenger not working with it. Have no idea whats wrong...
sudo passenger-config restart-app
Phusion Passenger is currently not serving any applications.
/etc/nginx/sites-available/myapp
server {
listen 80;
server_name _;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/myapp/public;
passenger_app_root /var/www/myapp;
rails_env production;
passenger_enabled on;
passenger_ruby /home/myuserapp/.rvm/gems/ruby-2.3.1/wrappers/ruby;
}
In sites-enabled have a soft symlink myapp to /etc/nginx/sites-available/myapp
sudo passenger-status
Version : 5.1.4
Date : 2017-05-25 06:56:30 +0300
Instance: byXevAbZ (nginx/1.10.3 Phusion_Passenger/5.1.4)
----------- General information -----------
Max pool size : 6
App groups : 0
Processes : 0
Requests in top-level queue : 0
----------- Application groups -----------
But have some passenger instances running...
sudo passenger-config list-instances
Name PID Description
--------------------------------------------------------------------------
byXevAbZ 1085 nginx/1.10.3 Phusion_Passenger/5.1.4
I ran sudo nginx -t command
nginx: [warn] conflicting server name "_" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
First warning cofused me, so i decide the problem is in conflict myapp-site and defult one.
So i just turn of default site and restart nginx. And everything worked!
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart
Final config file:
/etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.com;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/myapp/current/public;
rails_env production;
# Turn on Passenger
passenger_enabled on;
passenger_ruby /home/myuserapp/.rvm/gems/ruby-2.3.1/wrappers/ruby;
}
P.S. Correct me if you understand the subject deeper.

Resources