Running a Sinatra server when non-root user - ruby

I am part of a shared linux server, which is hosted on DigitalOcean.
I want to run a Sinatra web server from it, but I am not a root user.
I set the port for 4040, with set :port, 4040 and the server starts successfully with ruby server.rb.
== Sinatra/1.4.5 has taken the stage on 4040 for development with backup from WEBrick
[] INFO WEBrick::HTTPServer#start: pid=18955 port=4040
When I visit the IP address like http://IP_ADDRESS:4040, nothing shows up.
What am I doing wrong?

Related

How to access a web application running as a container in ubuntu from my windows system

I am running a web application(copied from github examples) that is running as a container in a remote ubuntu VM. The application is a Node JS application that is using mysql database. I brought the application up using docker-compose in ubuntu.
The application came up as http://172....:3000 using a network port. The ip address is displayed in the docker-compose terminal. In the ubuntu system, when i do curl http://172....:3000, it gives a proper success response. The ip address is a container network address. It is not the VM's ip address. There is no firewall.
How to access the web application from my windows 7 machine. When I tried accessing using http://VM Ip address:3000, it is not hitting ubuntu system. I am not getting any message in the docker-compose terminal. Can anyone help here ?
ports:
- "3031:3000"
similar line in your docker compose means you have published port 3000 of your container to port 3031 of your Ubuntu VM.
now you can access your client service as http://<ubuntu-ip>:3031 but before this, you need to allow access to port 3031

Can't access Ruby server on VM from host machine

I have a VM set up running Ubuntu server 14.04. In my VM I have created the following Ruby/Sinatra app:
require 'sinatra'
set :environment, :production
set :bind, '0.0.0.0'
get '/' do
"Hello World!"
end
When I execute this using ruby hello.rb I get the following output:
[2015-03-09 16:58:34] INFO WEBrick 1.3.1
[2015-03-09 16:58:34] INFO ruby 2.1.5 (2014-11-13) [x86_64-linux]
== Sinatra/1.4.5 has taken the stage on 4567 for production with backup from WEBrick
[2015-03-09 16:58:34] INFO WEBrick::HTTPServer#start: pid=2258 port=4567
Everything seems to work fine, but when I try to access localhost:4567 from my host machine (Windows 8.1) I get a GET http://localhost:4567/ net::ERR_CONNECTION_REFUSED error (in Chrome).
If I try to access the server from within my VM (ex, by using wget http://localhost:4567) it works fine.
I also have Apache2.4 running in my VM, which works fine, but I disabled it when trying to access my Ruby server by running sudo service apache2 stop.
What could the problem be? I have no problem running regular Ruby files, and I can access my Ruby/Sinatra app if I use Apache2 with Phusion Passenger. But when I simply run ruby hello.rb I can't access it from my host machine.
localhost refers to your local host, which in the case of Windows is not the same as your Ubuntu instance.
You'll need to connect to your Ubuntu instance IP directly, whatever that is. Usually you can find out with ip addr or ifconfig.
If you need a friendly DNS name you can put in your browser, xip.io provides one.
If you're using Vagrant then you can configure port forwarding so you can still use localhost if you want. Without port forwarding you will not be able to connect indirectly.
I have the network adapter for my VM attached to NAT. I was forwarding ports 443, 22 and 80 to my VM, and accessing my server on those ports works fine. Since I was running the Ruby WEBrick server on the default port 4567, I just had to forward port 4567 from my host machine to my VM as well.
After that change, typing http://localhost:4567 into my web browser served up the content from my Ruby file.

Start a new site on port 80

I created a sample demo site in ruby on rails and deployed it on the server. Currently I am using phusion passenger as the app server. I can see it by giving address as abc.net:3000. But instead of port 3000, I have to use port 80. I used the following command to start the app server and the site can be accessed as abc.net.
rvmsudo rails server -p 80
I have to configure phusion passenger with nginx. I did it this way. But when I tired to access the site, but I got the nginx index page. The nginx was started on port 80.
How can I use nginx and phusion passenger and start the app on port 80? I tried to start nginx on port 8080 and passenger on port 80, but my nginx is not starting on port 8080.

Rails app behaving differently in development vs production

Can someone please shed some light on why the app is behaving differently in production vs development mode. I have checked and re-checked config/database.yml and ensured that the username and password are correct. In fact as of writing this I have set both, development and production database to be same. Yet, when I run the server in production environment, Mysql2 complains about access denied, but works fine in development environment.
Same thing happens when running rails c production vs rails c development , no error in development but Mysql2 access denied in production.
Production mode
$ rails s -e production
=> Booting WEBrick
=> Rails 4.0.2 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-03-19 18:20:22] INFO WEBrick 1.3.1
[2014-03-19 18:20:22] INFO ruby 2.1.0 (2013-12-25) [x86_64-freebsd10.0]
[2014-03-19 18:20:22] INFO WEBrick::HTTPServer#start: pid=10800 port=3000
I, [2014-03-19T18:20:30.569167 #10800] INFO -- : Started GET "/" for 192.168.1.102 at 2014-03-19 18:20:30 +0200
F, [2014-03-19T18:20:30.709229 #10800] FATAL -- :
Mysql2::Error (Access denied for user 'root'#'localhost' (using password: YES)):
Development Mode
$ rails s -e development
=> Booting WEBrick
=> Rails 4.0.2 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-03-19 18:22:53] INFO WEBrick 1.3.1
[2014-03-19 18:22:53] INFO ruby 2.1.0 (2013-12-25) [x86_64-freebsd10.0]
[2014-03-19 18:22:53] INFO WEBrick::HTTPServer#start: pid=10898 port=3000
Started GET "/" for 192.168.1.102 at 2014-03-19 18:23:03 +0200
Processing by Rails::WelcomeController#index as HTML
Rendered /home/user/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/templates/rails/welcome/index.html.erb (2.3ms)
Completed 200 OK in 24ms (Views: 11.6ms | ActiveRecord: 0.0ms)
And here is my config/database.yml.
development:
adapter: mysql2
encoding: utf8
database: amo
pool: 5
username: root
password: mypass
host: localhost
production:
adapter: mysql2
encoding: utf8
database: amo
pool: 5
username: root
password: mypass
host: localhost
O/S : FreeBSD 10.0 64bit
Ruby : 2.1.0 (installed using Rbenv)
Rails : 4.0.2
A common problem is that the DB user permissions are setup with % to refer to local access permissions....
...but in the prod environment, where the DB and web servers are on different machines, you need to set the user permission to come from the IP, DNS, etc of the web server machine.
For instance, you may have perms like this:
grant all privileges on mydb.* to myuser#'%' identified by 'mypasswd';
grant all privileges on mydb.* to myuser#localhost identified by 'mypasswd';
But this will only work for your local dev environment. You may have this kind of permission setup scripted, in which case you'd need different setup perms for your prod DB.
In the prod environment, you may have your web service on 168.0.1.2 and your DB on 168.0.1.100. So your prod DB would need:
grant all privileges on mydb.* to myuser#168.0.1.2 identified by 'mypasswd';
If you add another web server, remember to add permissions for users coming from that machine.
If none of this is ringing a bell, post your GRANTS (change the private details). I'll dig out the commands to do this, if you aren't familiar.

HTTP Streaming on Heroku with Rails 3.1 & Unicorn

Inspired by the RailsCast on HTTP Streaming, I've decided to turn it on on my server. However, the listen 3000 :tcp_nopush => false line that Ryan suggests adding to my unicorn.rb doesn't play well with Heroku (they don't let you specify a port). Is streaming supported on Heroku, and how do I turn it on?
Heroku tells your application what port to listen on via the environment variable PORT. Check for this environment variable in your unicorn.rb.
port = ENV["PORT"].to_i
listen port, :tcp_nopush => false

Resources