Setting up Rails on Amazon AMI EC2 - ruby-on-rails

I have recently created an Amazon AMI(linux) instance on an EC2.
after doing all the yum stuff, when I do
rails server, the cmd shows no errors at all, but when i try to access http:public_ip:3000 through Firefox I get "Unable to connect"
I have already installed Ruby, NodeJs, Rails, and rvm.
I have asked on IRC and have tried rails s -b http://public_ip:3000
with
/home/ec2-user/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/socket.rb:205:in >`bind': Cannot assign requested address - bind(2) for 52.48.217.79:3000 >(Errno::EADDRNOTAVAIL)
in return.
I've also tried ssh to the elastic IP, and when I do rails s -b http://public_ip:3000 I get:
[2016-01-17 01:43:23] INFO ruby 2.3.0 (2015-12-25) [x86_64-linux]
Exiting
/home/ec2-user/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/socket.rb:231:in >`getaddrinfo': getaddrinfo: Name or service not known (SocketError)
this is all just to make sure that rails was installed well and running(I want to see the default rails page). Can anyone help?

You want to bind to 0.0.0.0.
Rails used to do this for you by default, but starting with 4.2 it only binds to localhost by default, so the command will be rails s -b 0.0.0.0.
You'll also need to make sure you have a rule set up allowing TCP traffic to port 3000 in your EC2 security group

Related

Running a Production Rails Server

I am new to Rails programming. I did some research, and decided to start with Rails 7, and Ruby 3.1.0.
I am following a simple online tutorial, using the standard Rails 7 stack: Sprockets, no Webpacker, Tailwind (starting to learn it), and MySQL.
I have a local MySQL server setup on my Mac, and i use mysqlworkbench to peer into the local DB.
Locally, I don't need to do anything to get the server started on my development machine. From the command line, I only need to do:
rails s
and a local web server is started on port 3000.
I have an old laptop, that I set up as an Ubuntu 20.04 machine, and is my home network, on port 10.0.0.253. I figured i can use it as a test server, so I can learn how to deploy in the real world. I installed Ruby 3.1, and Mysql on the Ubuntu machine. I also have nginx installed on it, but I don't have it configured to do anything.
On the Ubuntu machine, I can do
rails s
and then from mt Mac, I can go to:
http://10.0.0.253:3000
and I get the rails app.
Of course, when I close the terminal on the Ubuntu machine, the app stops working.
If I don't do rails s on the Ubuntu machine, if I do:
http://10.0.0.253
I get an error (can't find the site you're looking for).
I looked online, and there are a few articles about configuring nginx with Passenger, but no two recipes are the same.
My question is: When I push my code to the Ubuntu server, and I do:
bundle install
rake db:create
rake db:migrate
rake db:seed
What next? to start the server, and I can access it directly at: http://10.0.0.253

SSL Certificate getaddrinfo: Name or service not known (SocketError) Rails-4.2.0

I have Rails 4.2.0 application with ruby version 2.2.0. I have a requirement where I need to create an SSL certificate for the test environment.
To set-up an SSL certificate I followed the below steps:
step-1: brew install mkcert
step-2: Added entry on /etc/hosts for the localhost domain i.e 127.0.0.1 localhost.application_name
step-3: Created the key and certificate files for domain which I created by mkcert localhost.application_name
step-4: Then move those certificate files into the config\ssl folder
step-5: And then I start my rails server like bundle exec rails server -p 3000 -b 'ssl://127.0.0.1:3000?key=config/ssl/localhost.application_name-key.pem&cert=config/ssl/localhost.application_name.pem'
When I run the rails s with the below command I am facing an error 'getaddrinfo': getaddrinfo: Name or service not known (SocketError)
For more clarity, I am attaching the screenshot after rails s execute.
I already tried a lot of solution which is being already present in StackOverflow but no luck as of now. Any help will be appreciated!
Note: I have rails application setup with Docker
Binding syntax you provided (with ssl:// scheme) is used with puma application server.
WEBrick, which was the default application server with Rails 4.x, doesn't support this binding scheme.
So you can jump to puma by simply adding to Gemfile
gem 'puma'
and then run
bundle install
Then you'll be able to start your app as you're used to.

Refusing to connect: Ruby on Rails not connecting to any ports

My OS: Windows 10
Shell: Ubuntu (from the Microsoft Store)
Rails: 5.0.6
Ruby: 2.4.2
I installed Ruby on Rails using the Ubuntu Shell for Windows. After installing Ruby on Rails, I created a new project (testapp), and I am trying to connect to localhost:3000.
After a lot of searching, checking my /etc/hosts, checking my host file on windows, uninstalling eset, uninstalling XAMPP, Ruby on Rails still refuses to connect to localhost:3000.
I tried to bind it to a different port (multiple times), for example I tried to change it to port 3001, or port 4000, and it still wouldn't work. I tried rails s -b 0.0.0.0, this still doesn't work. I also tried 127.0.0.1, 127.0.0.1:3000, etc.
In the image, I am just showing that I tried rails s to connect to localhost:3000.
Can anybody please help me on why this isn't working? I am confused.
me trying to connect rails to port 3000
Ruby and Rails Version

WEBrick doesn't make things appear in the web browser with Rails 4.2.0 version

I've been using Amazon EC2 for my development exercises.(I'm not that experienced and learning now by myself) Usually, I could do my tasks with ruby version 2.0.0 and rails 4.0.1 in Amazon linux EC2. My first tutorial instructed me to do exercises with those versions.(for ruby and rails) There was no problem when I ran rails server, which made WEBrick works and I could see my works on web browsers.
Few days ago I knew Ruby on Rails Tutorial, so I wanted to learn rails with Ruby on Rails Tutorial(by Michael Hartl). So I made another Amazon EC2 and accessed with putty and downloaded what I need. Here's what I have done.
#for rvm
gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable
#with rvm
source .bash_profile
rvm install ruby
#and for rails
gem install rails -v 4.2.0.rc3 --no-ri --no-rdoc
#then I made my sample_app
rails new sample_app --skip-bundle
#made 'therubyracer' works with deleting the "#" sign
#and with "sudo yum install sqlite-devel"(I don't know what this part means but server said I need it, so I just did)
then I ran my WEBrick with rails server
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-12-28 05:54:29] INFO WEBrick 1.3.1
[2014-12-28 05:54:29] INFO ruby 2.0.0 (2014-11-13) [x86_64-linux]
[2014-12-28 05:54:29] INFO WEBrick::HTTPServer#start: pid=25353 port=3000
this very Usual comments were coming out but I couldn't access my server with browser through my "IP:3000"(what I used for access with putty, so I don't think this IP is wrong) and Chrome browser was just saying that "This webpage is not available". Also my security group for new EC2 is exactly the same with the one I used for my other exercises before, so it is also not the reason for this error. I did those work many times with other AMIs that Amazon supports for free but I failed when I patched those high version rails(4.2.0).
Very bizarre because the terminal is saying the WEBrick is running but actually i can't access. Can you give me any help or advice for me to find out what's the problem? I'm so inexperienced and any help or comments will be a huge lesson to me and make me continue to learn. Thank you.
Rails 4.2 no longer listens on all interfaces by default: when running in the same machine you need to connect to
http://127.0.0.1:3000
(Localhost should work too).
However since it sounds like you are connecting to a remote server so you need to use the -b option to bind to extra ip addresses, eg
rails s -b 192.168.1.1
As Frederick notes, the -b option is now required, and you can typically use the public ip, and ensure you specify the port number you've opened in your security group.
rails s -b PUBLIC_IP_HERE -p 3000
Additionally, if you are using the Amazon's VPC, you should use the Private IP available in the EC2 console with "rails s -b", not the public IP.
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
This works for me.
You need to bind rails to your AWS instances Private IP address in order to access the server remotely on the instance's public IP. You will find the Private IP within the AWS Instance dashboard Description tab.
e.g.>> rails s -b -p 3000

Start rails server on a domain I mapped in my host file

I want to map:
www.example.com
in my host file, how can I start 'rails server' so it uses this domain?
Edit the hosts file (instructions for OSX Snow Leopard)
sudo nano /etc/hosts
Add a line 127.0.0.1 www.example.com
Refresh DNS settings with sudo dscacheutil -flushcache
Start rails on the correct port
rails server will now work on http://www.example.com:3000/. To get rid of :3000, start Rails with: sudo rails server --port=80 (or rvmsudo if you are using RVM)
For production use, you might want to see Kevins answer.
I'd recommend setting up Passenger on either Nginx or Apache to do this. Doing a quick search on your operating system should come up with a tutorial on how to set everything up correctly.

Resources