How to Deploy Rails App to AWS - ruby-on-rails

I am trying to host a Rails app in AWS cloud where I have an EC2 instance and apache and mysql . Here I have uploaded my app but I am unable to bind it with IP. For a testing I am using this blog post https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-unicorn-and-nginx-on-ubuntu-14-04 as a reference .
When I am trying to run this command :
RAILS_ENV=production rails server --binding=server_public_IP
I am getting this error :
/home/ubuntu/.rbenv/versions/2.2.3/lib/ruby/2.2.0/socket.rb:206:in `bind': Cannot assign requested address - bind(2) for 52.24.103.139:3000 (Errno::EADDRNOTAVAIL)
Is there anyone help me understand what is this problem and how to deploy it on AWS apache .

In AWS the machine is not directly assigned the IP, i.e. it is routed using NAT. hence you can not use the public IP to start your rails server directly.
To start server just boot it without the binding parameter rails s production
Or you can use 0.0.0.0 to bind your server, this will start your rails on all the interfaces.
Tip: For production you should ideally server using some web server like nginx/apache using passenger/unicorn

Looking from error it says it cannot bind with ipadd 52.24.103.139:3000
What I would suggest it to open a 'custom TCP port 3000' and try running the same thing again.
May be your app is working on port 3000 not 80.
Hope that helps.

Related

Ruby on Rails - Starting server as a live website. [Linode]

I have made an account on Linode website and have a "linode" running. I have a server running using ruby on rails. The command I used is the following:
rails server --binding=<ip_adress>
The server starts up without issue. The question I have is why can't I visit the the side I created using my browser, just by putting the ip_address in the browser? The server logs display the following
Environment: development Listening on tcp:<ip_address>:3000
I can visit this ip_address on my browser but I need to add the ":3000" in the browser so I can view the site.
Shouldn't I be able to just visit the stand alone ip_address without entering ":3000"? I also wanted to say I am just learning ruby on rails as well.
I haven't tried anything more than described above
An IP address is a way to identify a machine on the internet.A port is a number assigned to uniquely identify a connection endpoint and to direct data to a specific service.
Therefore, your rails service is a combination of an IP address and a Port number. Since you can have different services running on the same machine at the same IP address.
HTTP has a default port of 80 which is what your browser will try to access when you don't provide a port.
Most likely, you will want a Reverse Proxy hosted at port 80 that forwards traffic to your rails app.
This post provides a better answer of how this works. https://superuser.com/questions/394078/how-can-i-map-a-domain-name-to-an-ip-address-and-port
Not Recommended
If you don't want to use a reverse proxy, you can host the rails server at port 80 itself.
rails server -p 80
Note that this requires you to have root permissions on the machine.

How to connect rails application(locally run on zeus) in another machine using IP?

I am using two rails applications, running both with rails s and zeus s -p3001 locally. When I am trying to run my application in another machine, the server started with webbrick is running fine on another system but couldnot run zeus. PLease help me out.
I just gave the url in browser 192.168.1.111:3000 (running). And
`192.168.1.111:3001` (server not found).
I tried zeus s -b 192.168.1.111:3001 but didn't help me.
The problem is, that the zeus bind to the local address (127.0.0.1) prohibiting connections from remove hosts. The proper solution would be to setup the reverse proxy using apache2 or nginx.
http://glasz.org/2013/10/25/reverse-proxy-a-rails-app.html
https://www.digitalocean.com/community/tutorials/how-to-deploy-rails-apps-using-unicorn-and-nginx-on-centos-6-5
Alternatively you may also want to use firewall to redirect requests.
http://forum.slicehost.com/index.php?p=/discussion/2497/iptables-redirect-port-80-to-port-8080/p1
But a quick and dirty solution is to use SSH port forwarding like this:
Start zeus on remote machine on port 3000
On local machine
local-machine$ ssh -L 3001:localhost:3000 remote-machine
On local machine: Connect to localhost:3001 instead of to remote-machine:3000
http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html

Domain setting using ruby rails

I am planning to have a web application.
To do so, I studied about ruby and ruby on rails. I am using linux server from amazon clouding system.
I bought a domain from godday, and I put the IP address on DNS setting. When I run 'rails s' command, I can connect to the wep page through port 3000 in such a way that domain.com:3000. However, I cannot directly connect to domain.com. How can I my domain works without port 3000?
And Do I have to run 'rails s' every time to make the wep page work? Actually I tried to use 'rails s &' to make it run in background. But it fails. How can I make the server run even though I am not connected to the linux server?
Thank you!
usually you use rails s just in development. there are quite a few ruby web servers you can choose from for your production environment: puma, passenger or unicorn to name a few.
of course all of them have their own tutorials how to set them up. for starters, i'd go with with passenger because it's integrated with nginx and apache and easily set up.
You need to specify a port, if you don't see the port it can be either 80 (http) or 443 (https).
rails server -p 80
On linux you have to be root to bind to port less than 1000, so just append sudo in front.

After installing rails, how to run rails server on amazon ec2 instance?

I installed rails on an instance on amazon AMI. I installed all the dependencies for rails. I even copied the code through scp. When i ssh to the instance i ran
rails s
The server is running. How can I view it from the browser?
from the Public DNS???
In summary, sure that your security group have a rule for TCP 3000.Then, you can use the Public DNS:
rails server -b ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com -p 3000
And access to: ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com:3000
This works for me.
Your amazon ec2 instance has a public ip address and domain name, see
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html
use this domain name as the URL in your browser.
You will also have to set inbound traffic / port connections in the security group of the instance.
A word of warning: with "rails s" you are starting webrick, a server that is only meant to be used in development. You probably should use a production server like apache + mod_passenger.
For more advanced usage of ec2 and ruby on rails see
http://railscasts.com/episodes/347-rubber-and-amazon-ec2?view=asciicast
move to folder from rails aplication and typerails s -b 0.0.0.0

Accessing Ruby Server on amazon instance from my browser

I have started the Ruby on Rails tutorial and am on my first app. I did all of this on an instance I have on amazon web service (aws). A while ago I installed apache on there so when I put in the public address (port 80) it tells me 'It works'. I want to access this server from my web browser at home.
However when I put in the server address :3000 (the port the app should be running at) I get nothing. Do I need to tell the box to open port 3000 or something of the like?
You need to open the instance's security group to accept traffic on port 3000. Check out the docs for more information.

Resources