Starting a rails app to the external ip address - ruby-on-rails

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

Related

Configure Apache and Passenger to use websocket

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.

Multiple concurrent rails servers with binding

I have a rails staging server available to my LAN as follows:
rails server --binding=0.0.0.0 -p 3000
I would now like to open up a second, concurrent rails server to my LAN as follows:
rails server --binding=0.0.0.0 -p 3001
Unfortunatly, I am getting this error message:
...
=> Booting Puma
=> Rails 5.1.1 application starting in development on http://0.0.0.0:3001
=> Run `rails server -h` for more startup options
A server is already running. ...
The error only exists if I use --binding=0.0.0.0 on both servers, which is necessary if I want it to be accessible to my LAN.
How can I open up multiple rails servers on the same machine to the LAN, not just localhost?
EDIT:
After trying Vasfed's solution, e.g.
rails server --binding=0.0.0.0 -p 3000 --pid=tmp/pids/server0.pid
rails server --binding=0.0.0.0 -p 3001 --pid=tmp/pids/server1.pid
the problem persists, but this time I have more information. It seems related to a port 9292 being opened...
=> Booting Puma
=> Rails 5.1.1 application starting in development on http://0.0.0.0:3001
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.9.1 (ruby 2.4.1-p111), codename: Private Caller
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:9292
Exiting
C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `initialize': Only one usage of each socket address (protocol/network address/port) is normally permitted. - bind(2) for "0.0.0.0" port 9292 (Errno::EADDRINUSE)
Rails checks if a pid file is already present. To run two copies of single app, you should alter pids too:
rails server --binding=0.0.0.0 -p 3000 --pid=tmp/pids/server1.pid
rails server --binding=0.0.0.0 -p 3001 --pid=tmp/pids/server2.pid

What is localhost and where is it defined?

I just changed from thin to puma at the recommendation of Heroku. When I start my rails app using the puma server it responds:
=> Booting Puma
=> Rails 4.2.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.11.3 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:3000
However if I go to http://0.0.0.0:3000 in the browser, which was my old localhost with the thin server, it does not respond. However, if I open http://localhost:3000, then it works. It appears that the definition of localhost has changed.
So, what is localhost? In particular, what sort of object is it, how is it defined, how do I see the actual ip address, and why does puma change it?
If you're trying to get Rails to bind to a different ip, the way to do that is with the -b option. To bind it to 0.0.0.0 instead of the Rails-default localhost you'd want to run something along the lines of rails s -b 0.0.0.0
Note: To be explicit, it may not be a bad idea to throw the -p 3000 option in there too (sets the port), even though that default is not likely to change. More info on the available options can be found by running rails s -h as well.
Localhost is the IPv4 loopback IP address 127.0.0.1. It is used instead of the hostname of a computer. Localhost can sometimes mean this computer.
For example, directing a web browser installed on a system running an HTTP server to http://localhost will display the home page of the local website.
Here's an interesting Wikipedia article
https://en.wikipedia.org/wiki/Localhost

NGinx sock connect Bad Gateway

I'm having an issue setting up Nginx to work with Puma server for a Rails 4 application.
The problem seems to be in my Nginx configuration since I keep getting 502 Bad Gateway error and error log states the following:
*1 connect() to unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock failed
(2: No such file or directory) while connecting to upstream, client: XX.XXX.XX.XXX,
server: mysitename.com, request: "GET /favicon.ico HTTP/1.1", upstream:
"http://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:/favicon.ico",
host: "mysitename.com"
Here is the Nginx site configuration I'm using:
upstream mysitename {
server unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock;
}
server {
listen 80;
server_name mysitename.com;
root /srv/vhosts/rumysitename/www/mysitename/public;
location / {
proxy_pass http://mysitename;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
}
Needless to say that Puma can't connect to that sock because it isn't there:
rails s -e production -b unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock=> Booting Puma
=> Rails 4.1.0 application starting in production on http://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.8.2 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:3000
Exiting
/srv/vhosts/rumysitename/.rvm/gems/ruby-2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `initialize': getaddrinfo: Name or service not known (SocketError)
I am no system admin and have absolutely no experience with Nginx so excuse me if I'm missing something obvious.
It seems that starting rails server and asking it to bind to a unix socket doesn't work. The -b option with rails server doesn't behave the same was as with the puma command. Basically it wants to bind to an IP address:
rails server --help
Usage: rails server [mongrel, thin, etc] [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=ip Binds Rails to the specified ip.
Default: 0.0.0.0
You can run puma directly:
puma -e production -b unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock
The socket will be created by running puma like this. It doesn't need to exist already. You'll need to have permission to create it in the location specified but you'll get a different error if that is not the case.
Another alternative is to create a config/puma.rb file which can include the binding:
config/puma.rb:
bind 'unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock'
and then run puma referencing that:
puma -C config/puma.rb -e production
You can put a lot more in the config file than just the sock. The puma example config file is a good starting point.
Try following steps.
Stop the puma server.
Delete the .sock file used by puma.
Start the puma server.

Production mode is not running on port 80 (Rails)

I don't understand why it's not running on port 80 instead of port 3000 when I run the command RAILS_ENV=production rails s on the same line. I want it to run in production mode but it's not running on the correct port. Anyone know why? I'm trying to use Rubber but I haven't ran any commands for it only just changed some of the files like it says in this tutorial.
root#ip-000-00-00-000:/home/ubuntu/Git/# RAILS_ENV=production rails s
=> Booting Thin
=> Rails 3.2.11 application starting in production on \http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
\>> Thin web server (v1.5.1 codename Straight Razor)
\>> Maximum connections set to 1024
\>> Listening on 0.0.0.0:3000, CTRL+C to stop
^C>> Stopping ...
Exiting
webrick runs on port 3000 by default(even in production mode). Pass the port number explicitly if you want to run on a different port.

Resources