Configure Apache and Passenger to use websocket - ruby-on-rails

I'm trying to setup apache to use ruby on rails web socket with passenger. I know that Action Cable is not supported on Passenger + Apache, so I'm tryng to use the reverse proxy solution:
https://www.phusionpassenger.com/library/deploy/standalone/reverse_proxy.html
The main application should continue to use the Passenger Apache module while the websocket should use passenger standalone.
I enabled the reverse proxy and changed the vhost config.
<VirtualHost *:80>
...
PassengerRuby /home/rails/.rvm/gems/ruby-2.5.1/wrappers/ruby
<Location "/cable">
ProxyPass "ws://127.0.0.1:4000/cable"
ProxyPassReverse http://127.0.0.1:4000/
ProxyPreserveHost on
</Location>
...
</VirtualHost>
When I start the passenger from the command line
bundle exec passenger start --daemonize --address 127.0.0.1 --port 4000
it works fine, but when I create a service using systemd in Ubuntu 18.04.5 LTS
...
[Service]
Type=forking
WorkingDirectory=/mnt/xfeature/srv/www/f3.xxxx.xxx/current
Environment=RAILS_ENV=feature
User=rails
Group=www-data
UMask=0002
ExecStart=/home/rails/.rvm/bin/rvm-shell -c 'bundle exec passenger start --daemonize -e feature --address 127.0.0.1 --port 4000 --log-level 5'
ExecStop=/home/rails/.rvm/bin/rvm-shell -c 'bundle exec passenger stop --port 4000'
....
it fails usually with this error.
[ E 2021-05-13 09:52:13.3607 21447/Tz age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /mnt/xfeature/srv/www/f3.xxx.xxx/releases/20210513094622: An operating system error occurred while preparing to start a preloader process: Cannot change the directory '/tmp/passenger.spawn.XXXXgMx55z/envdump' its UID to 1001 and GID to 1001: Operation not permitted (errno=1)
Error ID: 847da63a
Error details saved to: /tmp/passenger-error-5S5Syq.html
and the websocket connection cannot be established. I checked the /tmp permission and are OK.
Any suggestion?

We had some strange issues with Apache and Passenger recently. It turns out Apache automatically updated and changed the PrivateTmp setting to true, which caused a similar error message.
I'm not sure if that is what is causing your error, but you could check that to verify that PrivateTmp is set to false.
Our configuration is:
/etc/systemd/system/apache2.service.d/override.conf
override.conf Contents:
PrivateTmp=false
I'm not certain how that override.conf file is loaded. I assume there is a configuration somewhere looking for an override file and it is using it.

Related

Rails: use Capistrano to deploy, how to check log when error happens?

I am deploying a rails add using Capistrano on remote Ubuntu 14.04 server.
Finally when I restart nginx, web page shows an error
We're sorry, but something went wrong.
I hope to know what cause the error, what command can I use to see log from remote server
try
bundle exec tail -f log/production.log
if no error is seen there then first check your nginx logs at
tail -f /var/log/nginx/access.log
or
tail -f /var/log/nginx/error.log
if you see some request logging there then that means request is coming to server and its not passing to puma server.
There can be two reasons about why request is not being passed to puma, either your address of puma process is not correct in nginx file or puma server is not running or there was some error and puma was shutdown when request reached it.
to see puma process use this command
ps aux | grep puma
it should print one line out of many lines
app 22528 0.1 0.5 296532 23912 ? Ssl 16:42 0:00 puma 2.11.1 (tcp://0.0.0.0:8080) [20180110213633]
now using this information I can map address like this in nginx
upstream app {
# Path to Puma SOCK file, as defined previously
server 0.0.0.0:8080;
}
here I bind the puma local ip with port to nginx process.
Make sure your puma.rb binds properly to puma.sock file as for one of my project I am doing like this in config/puma.rb
bind "unix:///Users/Apple/RAILS_PROJECTS/tracker/tmp/sockets/puma.sock"

Starting a rails app to the external ip address

I have a ruby on rails application and I am trying to run it on the external ip of my google compute engine ubuntu 14.04 LTS VM.
I try rails server -e production
and the output is:
=> Booting Puma
=> Rails 4.2.4 application starting in production on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.14.0 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://localhost:3000
I do not want it to be at that location; I want it to be viewable from the external ip address of the server.
Part of the issue is that I do not know if this is a rails, a puma, or a google compute engine question.
Note: I can't see if it actually launching at localhost:3000 because the VM is just a terminal.
(I am assuming you are using nginx, if not, apt-get install nginx; service nginx start)
If possible, show your nginx.conf (/etc/nginx/nginx.conf) and default.conf (/etc/nginx/sites-available/default.conf)
Since you are using puma (I use it too) you should setup nginx conf file and set server upstream equal to puma's binding.
# /etc/nginx/sites-available/default.conf
upstream your_app {
server 127.0.0.0.1:9292;
}
server {
listen 80;
server_name your_domain;
root /usr/share/nginx/your_app/public;
# or, if you are using capistrano
root /usr/share/nginx/your_app/current/public;
location / {
proxy_pass http://your_app; # equal to upstream "name"
...
}
....
}
# config/puma.rb
[...]
bind "tcp://127.0.0.1:9292"
And execute puma's server
$ bundle exec puma RAILS_ENV=production &
After doing this steps and if the application still doesn't work, output your /var/log/nginx/error.log, nginx.conf and default.conf

Debugging Unicorn server remotely with RubyMine

I have Rails (version 4.0.3) application which uses nginx as front-end server to dispatch the requests to actual Unicorn. Whilst developing the app I would like to use Docker on Windows (boot2docker) to run the application and ruby-debug-ide to debug the application remotely.
The original setup works fine (application answers on the host machine) until I replace rails server with
rdebug-ide --port 1234 --host 0.0.0.0 --dispatcher-port 26162 -- bin/rails server
After running this on the Docker container I connect to the remote debugger successfully as it tells on Docker containers bash that breakpoints were added based on what I've set on RubyMine and tells that Unicorn server is now running. I added gem unicorn-rails to make rails server work for Unicorn too.
Now the actual problem is that the nginx can't seem to find the Unicorn when it as ran within the debugger. It just keeps on loading on browser (and with curl) until 504 (gateway timeout) is returned.
The interesting Unicorn configuration contains
app_dir = "/app"
working_directory app_dir
pid "#{app_dir}/tmp/unicorn.pid"
worker_processes 1
listen "/tmp/unicorn.sock", :backlog => 64
I have set up everything as described on JetBrains help pages. On Docker I have all the necessary ports (1234 for debugger, 26162 for dispatcher, 443 for HTTPS) open.
I've crawled Internet and Stack Overflow for hours without any luck and can't find anything to try anymore. Any ideas?

Deploying Rails on Apache on Mac OSX - 403 Forbidden Error

I have browsed over 100 links in the past hour, and nothing has helped. What I'm trying to do is simple: deploy a rails app on apache using passenger. The issue I'm getting is a 403 Forbidden error. My setup:
I run these two commands:
sudo gem install passenger
rvmsudo passenger-install-apache2-module
Then in etc/apache2/other I create a file called Passenger.conf with:
LoadModule passenger_module /Users/maq/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.14/buildout/apache2/mod_passenger.so
PassengerRoot /Users/maq/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.14
PassengerDefaultRuby /Users/maq/.rvm/wrappers/ruby-2.0.0-p247/ruby
Then in /etc/apache2/extra/httpd-vhosts.conf, I have:
<VirtualHost *:80>
ServerName rails.local
DocumentRoot /webapps/coolapp/public
<Directory /webapps/coolapp/public/>
AllowOverride all
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
Then in my /etc/hosts file, I add this entry:
127.0.0.1 rails.local
Then, I change permissions on my webapps directory:
sudo chmod -R 755 /webapps
I then restart Apache:
sudo apachectl restart
NONE OF THIS IS FIXING THE ISSUE. I keep getting the same thing when I access rails.local in my browser:
You don't have permission to access / on this server. Apache/2.2.22
(Unix) DAV/2 Phusion_Passenger/4.0.14 mod_ssl/2.2.22 OpenSSL/0.9.8x
Server at rails.local Port 80
Someone for the love of god tell me what the issue could be.
Apache won't have permission to access /. You should be serving up your websites from /Library/WebServer/Documents/ or—depending on what group Apache is configured to run under (wheel by default in OS X)—you can try sudo chown -R root:wheel /webapps
Edit
My httpd.conf configuration has the following user/group:
User: [my user] # is an admin user
Group: staff

Problem starting Passenger with Nginx

I have just setup Passenger with Nginx and it seems to install fine but when I run it I try to start it by:
passenger start -e production
I get:
=============== Phusion Passenger Standalone web server started ===============
PID file: /root/rails_apps/myapp/tmp/pids/passenger.3000.pid
Log file: /root/rails_apps/myapp/log/passenger.3000.log
Environment: production
Accessible via: http://0.0.0.0:3000/
You can stop Phusion Passenger Standalone by pressing Ctrl-C.
===============================================================================
2011/04/18 07:17:27 [error] 9125#0: *4 "/root/rails_apps/myapp/public/index.html" is forbidden (13: Permission denied), client: 127.0.0.1, server: _, request: "HEAD / HTTP/1.1", host: "0.0.0.0"
and I get "Unable to connect" when I try to access my site in the browser.
Here is configuration in nginx.conf
server {
listen 80;
server_name myapp.com;
root /root/rails_apps/myapp/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
any ideas?
This error seems caused because the user of nginx cannot access the mentioned file. It can be caused not only if the /root/rails_apps/myapp/public is not have a correct permission, but even if one of the parent directories does not have that!
In your nginx.conf you can see something like:
user nginx;
http {
# blah.
}
Sometimes parameter of the user can be different. Be sure to all folder is available by this user in the path.
You can check it by sudo -Hu nginx /bin/bash -l and cat /root/rails_apps/myapp/public/index.html. Test and test it again with this command until you cannot see the content of the file.
A little explanation: with that sudo command you start a shell as an user nginx. And with cat command you can simulate the file reading.
Try this:
sudo passenger start -e production
since the path you specified is in /root (/root/rails_apps/myapp/public), nginx should have enough permissions:
user root; in nginx.conf
you should also start nginx as superuser ( sudo )
but it might be better to just move your rails app somewhere to your user directory and grant needed permissions to default nginx user 'www-data'
user www-data;

Resources