Passenger + NGINX: Phusion Passenger is currently not serving any applications - ruby-on-rails

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.

Related

Nginx 502 when running Rails (Puma) with -d (daemon)

Ruby 2.5.1, Rails 5.2.2.1
I'm trying to make nginx get upstream through puma socket.
When I run rails s -e production all is good.
When I run rails s -e production -d Nginx returns 502 Bad Gateway
config/puma.rb
...
app_dir = "/home/user/myapp"
tmp_dir = "#{app_dir}/tmp"
# Set up socket location
bind "unix://#{tmp_dir}/sockets/puma.sock"
# Logging
stdout_redirect "#{app_dir}/log/puma.stdout.log", "#{app_dir}/log/puma.stderr.log", true
...
etc/nginx/sites-enabled/mydomain.com
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:/home/user/myapp/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name mydomain.com;
root /home/user/myapp/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
var/log/nginx/error.log
2019/07/07 13:45:09 [error] 21609#21609: *11391 connect() to
unix:/home/user/myapp/tmp/sockets/puma.sock failed (111: Connection
refused) while connecting to upstream, client: 172.68.11.91, server:
mydomain.com, request: "GET /pages/one HTTP/1.1", upstream:
"http://unix:/home/user/myapp/tmp/sockets/puma.sock:/pages/one", host: "mydomain.com"
(P.S. change from original domain to mydomain.com)
What difference? How to fix it? Please explain and help
UPDATE
Seems to be running with daemon flag it doesn't create puma.sock in /home/user/myapp/tmp/sockets. Why and where is it?
The earlier answer does not work any more. The deamon option -d is deprecated.
You could use a systemd service:
sudo nano /etc/systemd/system/puma.service
Copy this to the file and fill in your YOUR_APP_PATH and FULLPATH:
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
# Requires=puma.socket
[Service]
# Puma supports systemd's `Type=notify` and watchdog service
# monitoring, if the [sd_notify](https://github.com/agis/ruby-sdnotify) gem is installed,
# as of Puma 5.1 or later.
# On earlier versions of Puma or JRuby, change this to `Type=simple` and remove
# the `WatchdogSec` line.
Type=notify
# If your Puma process locks up, systemd's watchdog will restart it within seconds.
WatchdogSec=10
# Preferably configure a non-privileged user
# User=
# The path to your application code root directory.
# Also replace the "<YOUR_APP_PATH>" placeholders below with this path.
# Example /home/username/myapp
WorkingDirectory=<YOUR_APP_PATH>
# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
# SystemD will not run puma even if it is in your path. You must specify
# an absolute URL to puma. For example /usr/local/bin/puma
# Alternatively, create a binstub with `bundle binstubs puma --path ./sbin` in the WorkingDirectory
ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/puma.rb
# Variant: Rails start.
# ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/config/puma.rb ../config.ru
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
# Variant: Specify directives inline.
# ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
Restart=always
[Install]
WantedBy=multi-user.target
Run systemctl daemon-reload to reload your services.
Then you can use sudo systemctl restart puma to restart/start/stop the service
Refer to the puma docs for more information.
Solution
Dont know why, but It works if run puma (not rails server)
RAILS_ENV=production bundle exec puma -C config/puma.rb -d

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?

How to configure nginx with passenger with rails application

I have followed this tutorial https://www.digitalocean.com/community/articles/how-to-install-rails-and-nginx-with-passenger-on-ubuntu
I have installed passenger with nginx on my virtual machine and trying to access the site.
In the root I have specified path as root /var/rails_apps/public/; which give me Welcome to Nginx page,
server{
listen 80;
server_name localhost;
root /var/rails_apps/public/;
passenger_enabled on;
}
As my root page of my site is in the /var/rails_apps/app/views/home/index.html.erb
so I changed the path to root /var/rails_apps/app/views/home/;
server{
listen 80;
server_name localhost;
root /var/rails_apps/app/views/home/;
passenger_enabled on;
}
but still for both root I am getting Welcome to Nginx page.
My request URL is like this -> /#{IpAddressOfVirtualMachine}:80/
When I specified different port for listening eg 1027 then it gives me error Unable to connect
Please explain how I can get my site running using nginx and passenger, is there any other setting required?
I am able run my site just did following changes.
Install nginx init script
nginx init script by Jason Giedymin helps us to administer web server easily.
$ cd
$ git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git
$ sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
$ sudo chown root:root /etc/init.d/nginx
After that rails g controller pages home
And point root to public folder root /var/rails_apps/helloworld/public;
then access my virtual machine through http #{IpAddressOfVirtualMachine}:1027/pages/home
port 80 was busy so I used different which is port 1027
And it works !!!
you can refer this blog for more information
http://ershadk.com/blog/2012/04/05/set-up-rails-3-2-2-with-passenger-rvm-and-ngnix/
Change the root back to public folder, and open the URL without the port number
You need to change the server name in the nginx config to the same IP you are connecting to, and yes keep the root in the public folder, that's how rails work.
server_name 123.456.789.000; # replace with your IP
Instead of localhost, then restart nginx.

NGINX Setup (Rails App in a subdirectory)

I'm using NGINX with Passenger to run a rails application on an Ubuntu server.
However, I'd like to have the rails app served from www.mydomain.com/store , and have
a wordpress install served from www.mydomain.com.
How would one go about setting up the nginx.conf?
From the official manual:
To do this, make a symlink from your Ruby on Rails application’s public folder to a directory in the document root. For example:
ln -s /webapps/mycook/public /websites/phusion/rails
Next, set passenger_enabled on and add a passenger_base_uri option to the server block:
server {
listen 80;
server_name www.phusion.nl;
root /websites/phusion;
passenger_enabled on; # <--- These lines have
passenger_base_uri /rails; # <--- been added.
}

Resources