Rails server on AWS hosting - ruby-on-rails

I have hosted a rails application on AWS. Every time I want to access my website, I have to go through some steps which are quite repetitive.
1. ssh -i <<a>my-keypair-pem> ec2-user#<<a>AWS-IPv4-public-IP>
2. rails s -p <<a>port> -b 0.0.0.0
After some time, I also get this error
'packet_write_wait: Connection to <AWS-IPv4-public-IP> port 22: Broken pipe'
I did some research and can't seem to find a way to keep my application running 24/7 without having to do these steps before accessing every time.
My AWS instance is on 24/7, so the website should run 24/7 as well.
Would assign an elastic IP to my instance help?
Appreciate any guidance.
EDIT: I followed this tutorial initially https://www.youtube.com/watch?v=jFBbcleSPoY and that is where I found the steps mentioned above.

There are many ways to run the rails server as daemon. If you google for "rails server as daemon", you will see many links. Have not added any links as many of good links are by hosting service providers.
If you still want to run the rails server through the shell for some reason, tmux is the way to go. The following excerpt is shamelessly copied from Tmux Wiki.
tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.
You can open a tmux terminal and start rails server. You can detach from the tmux and quit your ssh session. Whenever you wish, you can ssh back to your server and re-attach to the tmux session. You rails server will still be running as you left it. This is great way to run development server in foreground for debugging.

Resolved the issue with https://mosh.org/, for anyone who stumbles on this post in the future.
Download and install mosh (mobile shell)
Run the modified version of the command mentioned in my original question
mosh -ssh="ssh -i <your-keypair.pem>" ec2-user#<AWS-Instance-IP>
This resolved my packet_write_wait issues and I don't have to keep restarting the rails server.

Related

Rails: How to "publish" a website

So, I am almost done with my website. I created it using ruby on rails on the Cloud9 IDE. Is there a way to publish it? To go on it, a user has to be signed on Cloud9 and then I need to run the code on the terminal...
rails server -b $IP -p $PORT
How would I be able to publish my website? I am very new to rails and I have no idea.
To publish your website you need to host it somewhere.
There are multiple ways to do this. For instance you can use Heroku. They provide PaaS (Platform as a service), which means they will handle the installation of the app for you and the maintenance of the virtual machine it resides (updates and whatnot).
Heroku also gives you a very nice admin view for the status of your app, online log display and some other goodies.
Another alternative would be Amazon Web Services, DigitalOcean or Linode (among many others!). They provide IaaS(Infrastructure as a Service). They only give you a virtual machine with your choice of operating system, you will have to manually publish your app and provide manual maintenance of the VM and pretty much anything including the database itself.
Using Heroku would be your best choice for the moment as they are very straightforward with the publishing part. Once you get the grip you can go to the other platforms for customization.
You will also need some deployment tools such as capistrano or mina.
If you're going to host your app in heroku, it's much easier to deploy. As simple as pushing your codes with git.

Thin unable to start after reboot [CentOS 7 + AWS EC2]

I recently acquired an AWS subscription and I'm trying to set up a production environment to host my rails app. I'm using an EC2 instance with CentOS 7 and I'm using mariadb as sql server and thin as my rails server. Everything went fine until I stopped the EC2 instance yesterday. I did not change any configuration and yet I can't get thin to start. I've tried many solutions without success. I'm using a config file to execute thin. If I use the command thin start -e production, it starts successfully, but I've no luck using the config file. The pid and the socket files are not created. I think it is not a problem with my config file, because it worked fine yesterday. Can you give some hints about what the problem may be? Can it be something related to OS configurations, like permissions? Thanks in advance!
After all it was a permissions issue. I wasn't executing the command as root thus it would not run for lack of permissions. Sudo su and it worked like a charm. Thanks anyway!

Use RubyMine and Docker for development

I'm trying to develop a Rails project without having to install Ruby and all server tools in my Windows local machine. I've created my Docker containers (Ruby and MySQL) and installed the Docker plugin on RubyMine 2016.1, however it seems not very practical for the development daily use, I mean the cycle develop, run, debug, just before deployment to test server.
Am I missing something to make this workflow possible? Or isn't Docker suggested for this step in the development process?
I don't develop under Windows, but here's how I handle this problem under Mac OS X. First off, my Rails project has a Guardfile set up that launches rails (guard-rails gem) and also manages running my tests whenever I make changes (guard-minitest gem). That's important to get fast turnaround time in development.
I launch docker daemonized, mounting a local directory into the docker image, with an exposed port 3000, running a never-ending command.
docker run -d -v {local Rails root}:/home/{railsapp} -p 3000:3000 {image id} tail -f /dev/null
I do this so I can connect to it with an arbitrary number of shells, to do any activities I can only do locally.
Ruby 2.2.5, Rails 5, and a bunch of Unix developer tools (heroku toolbelt, gcc et al.) are installed in the container. I don't set up a separate database container, as I use SQLite3 for development and pg for production (heroku). Eventually when my database use gets more complicated, I'll need to set that up, but until then it works very well to get off the ground.
I point RubyMine to the local rails root. With this, any changes are immediately reflected in the container. In another command line, I spin up ($ is host, # is container):
$ docker exec -it {container id} /bin/bash
# cd /home/{railsapp}
# bundle install
# bundle exec rake db:migrate
# bundle exec guard
bundle install is only when I've made Gemfile changes or the first time.
bundle exec rake db:migrate is only when I've made DB changes or the first time.
At this point I typically have a Rails instance that I can browse to at localhost:3000, and the RubyMine project is 'synchronized' to the Docker image. I then mostly make my changes in RubyMine, ignoring messages about not having various gems installed, etc., and focus on keeping my tests running cleanly as I develop.
For handling a console when I get exceptions, I need to add:
config.web_console.whitelisted_ips = ['172.16.0.0/12', '192.168.0.0/16']
to config/environments/development.rb in order for it to allow a web debug console when exceptions happen in development. (The 192.168/* might not be necessary in all cases, but some folks have run into problems that require it.)
I still can't debug using RubyMine, but I don't miss it anywhere near as much as I thought I would, especially with web consoles being available. Plus it allows me to run all the cool tools completely in the development environment, and not pollute my host system at all.
I spent a day or so trying to get the remote debugger to work, but the core problem appears to be that (the way ruby-debug works) you need to allow the debugging process (in the docker container) to 'reach out' to the host's port to connect and send debugging information. Unfortunately binding ports puts them 'in use', and so you can't create a 'listen only' connection from the host/RubyMine to a specific container port. I believe it's just a limitation of Docker at present, and a change in either the way Docker handles networking, or in the way the ruby-debug-ide command handles transmitting debugging information would help fix it.
The upshot of this approach is that it allows me very fast turnaround time for testing, and equally fast turnaround time for in-browser development. This is optimal for new app development, but might not work as well if you have a large, old, crufty, and less-tested codebase.
Most/all of these capabilities should be present in the Windows version of Docker, as well.

What, where and how to upload Ruby on Rails application files to the VPS?

I am using Ruby on Rails 3.0.9 and I would like to publish my web site. I already set my VPS running Ubuntu 10.04 LTS and the capistrano gem (this one I think as well as possible). Now, what I need to do is to upload all files to the www/project_name directory (I am on Mac OS)...
What I have to do to accomplish that?
You don't need your deployment machine to have Capistrano. Capistrano automates a bunch of tasks that I suggest you do at least one manually so you know what's going on. Sooner or later, you'll be debugging some Capistrano task, so you may as well figure out the guts sooner or later.
Coarsely, what you need to do is to basically duplicate your development environment on your production machine. If you have it on version control, you can git clone or svn whateveritis on your production machine. If not, you can scp it over with scp /local/rails/dir remoteuser#remotehost:www/projectname.
At this point, you should actually do the remainder of the work on the server. Since you've managed to install Capistrano, I assume you're familiar with the basics of making your way around SSH.
Once the code's over, you have to install the prerequisites. If you're using 3.0.9 you should be able to run bundle install --deployment, where the deployment flag basically tells bundler to use the identical gem set as on your development machine.
When that's done, actually getting the server online will vary based on your setup. If you're using non-standalone passenger, just follow any of the many guides at this point. If you're running standalone passenger or thin or unicorn or any other standalone rails server, go ahead and start that in daemon mode (so it won't quit on you when you end your SSH session) and make sure you se the production flag. You can either start it in sudo and have it listen on port 80 (e.g., sudo thin start -d -p 80) or have it listen on a higher-number port and use a reverse proxy on your WWW-facing server. The instructions for how to reverse proxy are all over the internet.
Let me know if you have any questions.
You have half of a deployment solution with Capistrano. Commonly Passenger is used as the other half, which sits on the server and loads your app. To accomplish this, usually, SSH keys are used. There are numerous tutorials on how to set this up. One of my favorites written by Dan Benjamin can be found on his blog Hivelogic.
Edited to provide more begginer info:
Capistrano begginer's guide from the Capistrano wiki.
Passenger Stand Alone Guide from the Passenger website.
Be sure to check out the other guides for the webserver of your choice when you're ready.
These guides will give you the background you need to get a local Passenger & Capistrano deployment going. These guides provide the knowledge you need to get achieve what you want.
Simple and short sample of deployment via SSH http://alexeypetrushin.github.com/vfs/ssh_deployment.html

Run thin webserver as a windows service

i'm trying to deploy in production Redmine application. I heard that thin is the fastest ruby on rails webserver so I installed it. Now I have a really simple problem: i must start it every time i reboot the machine via cmd because there isn't a prebuilt windows service or something similar that allow me to autostart it. How could i fix the problem? I saw that there is a bat file, so i tried to make a C# windows service like this and it starts correctly but if I stop it the service stops but the webserver is still active and it will never shutdown. The only way to stop thin is to reboot the machine. Maybe I'm wrong, could someone post an example of how should i run thin as a windows service?
i've written a blogpost about this a while ago, but most of it should still be applicable. Hope it helps.
But to be honest, i always deploy on windows using the mongrel-service gem, and configure an apache in front to load-balance between 3 mongrels. Much easier.
Also the big advantage for me was that if something went wrong with thin-service, it didn't restart automatically, while the mongrel-service guards your mongrel process, and if it for whatever reason goes down, it will restart it again. For me that was something i could not miss.

Resources