How to start Thin server automatically when System Re boots - ruby-on-rails

How to start thin Server automatically when Server Reboots.
I have a Rails 3 project which uses Thin Server.I can manually control the thin Server from terminal.Is it possible to start thin server as background process when system reboots.
Thanks in advance.

You can use Scheduled Tasks. There's a specific trigger option for starting a task when the computer starts.
To start the process in background mode you can use the -d option of the rails command.

I suppose you need to do this:
sudo thin install #to create init.d entry for thin
sudo /usr/sbin/update-rc.d -f thin defaults # to setup it
sudo thin config -C /etc/thin/<appname>.yml -c /var/rails/<appdir> --servers 4 -e production # to generate congig file for it. If you already got config file, you can just copy it to /etc/thin/ instead of creating.
If you use rvm on your server - browse this: RVM and thin, root vs. local user.
You can also take a look at: https://github.com/opscode-cookbooks/runit

Related

How to get systemd to restart Rails App with Puma

I've been struggling with this a week now and really can't seem to find an answer. I've deployed my Rails App with Capistrano. I use Puma as a server.
When I deploy, everything works ok. The problem is to get Puma to start at reboot and/or when it crashes.
To get the deployment setup, I've used this tutorial. I'm also using RVM. The problem I seem to get is to get the service to start Puma. Here's what I've used (service file):
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
#User=my-user
WorkingDirectory=/home/my-user/apps/MyApp/current
ExecStart=/home/my-user/apps/MyApp/current/sbin/puma -C /home/my-user/apps/MyApp/shared/puma.rb
Restart=always
[Install]
WantedBy=multi-user.target
That doesn't work. I was starting to think the problem was Ruby not being installed for all users, so I've installed RVM for all users and still get the same problem. My server has only root and my-user.
Looking at how Capistrano deploys, the command it runs is: cd /home/my-user/apps/MyApp/current && ( RACK_ENV=production /home/my-user/.rvm/bin/rvm default do bundle exec puma -C /home/my-user/apps/MyApp/shared/puma.rb --daemon ). If I use the aforementioned command, I get an error from Systmd complaining about missing parameters. So I've written a script with it and got the service file to call this script to start the app.
That doesn't work either. Note that if I call the script from anywhere on the server the script does start the App, so its an issue on configuring Systemd, but I can't figure out what's wrong and I'm not sure how to debug it. I've seen the debug page on System's website, but it didn't help me. If I run systemctl status puma.service all it tells me is that the service is in failed state, but it doesn't tell me how or why.
Also worth noting: If I run bundle exec puma -C /home/my-user/apps/MyApp/shared/puma.rb from my App folder it works ok, so how I could duplicate this command with Systemd service?
At the end the problem was twofold: 1) rvm wasn't installed properly for all users, which meant the deployer user didn't have ruby/bundle/etc available and secondarily the script was also wrong. For reference below is the revised script that worked for me:
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
User=deployer
WorkingDirectory=/var/www/apps/MRCbe/current
ExecStart=/bin/bash -lc 'bundle exec puma -C /var/www/apps/MRCbe/shared/puma.rb'
Restart=always
[Install]
WantedBy=multi-user.target
Have you looked into Foreman ?
Foreman makes it easy to start and stop your application if it has multiple processes.
Incidentally it also provides an export function that can generate some systemd or upstart scripts for you to (re)start and stop your application.
As you are already using capistrano you can use capistrano-foreman to integrate all this nicely with capistrano.
I hope you find some use in these resources

EC2 : Rails deployment gives blank pages

I have deployed my rails application on EC2. It runs on two servers. One for rails application and second for DB.
When I start application using "rails s -e production&" and if I stay connected using SSH,
I can see the webpages.
As soon as I disconnect SSH I can not see the pages.
There are no errors thrown. One weird thing is "Production.log" file does not have anything.
everything is spit out on console.
You are running rails in the current ssh session. Any programs you have running during that session will stop if you disconnect. You need to set up your rails app to run as a daemon using something like Phusion Passenger.
You are basically running the built in WEBrick server that is not really meant for production so it's likely that the process is getting killed after the parent process (your ssh process) gets terminated.
You can probably tweak the configuration to make WEBrick not quit, or you can simply run your session using screen or tmux
Screen:
$ screen
$ rails s -e production &
$ screen -d
When you want to reattach:
$ screen -r
Tmux:
$ tmux
$ rails s -e production &
$ # Hit <ctrl-b><ctrl-d> to detach
When you want to reattach:
$ screen attach -t 0
Or like #datasage mentioned you can run your Rails with an actual production web server like Passenger Phusion or Unicorn.

How to keep server running on EC2 after ssh is terminated

I have an EC2 instance on which I installed rails server. The server also runs fine when I do
rails server
But after I close the ssh connection the server also stops. How can I keep the server running even after closing the ssh connection.
screen rails s
did the trick
after that CTRL + A + D and I left and the server is running fine
Try this. We have to start rails server as daemon.
rails s -d &
run at as server means thu nginx or apache or what ever this development server not mean run as server
user this is need more info https://www.digitalocean.com/community/articles/how-to-install-rails-and-nginx-with-passenger-on-ubuntu
also if want advance sololtion use rubber https://github.com/rubber/rubber
I needed mine running everything not, just rails in the background. Install Screen which makes a sub terminal that isn't affected by your ssh connection. sudo apt-get install screen Open screen screen Then start rails rails server &.
Press 'Crtl + A' then 'D' to escape and type screen -r to get back in to the screen terminal.
I will recommend using apache or something else instead of the regular rails server but you can probably add & at the end and feel free to leave
rails server &
These steps worked for me. MY OS is Description: Ubuntu 16.04.4 LTS
sudo apt-get install screen
screen rails s
CTRL + A + D from terminal to detached the existing process and let it run.
Here's a production proof version using RVM and Systemd. Will keep the server alive if it gets terminated for any reason.
[Unit]
Description=Puma Control
After=network.target
[Service]
Type=forking
User=user
WorkingDirectory=/var/www/your_project_name
PIDFile=/var/www/your_project_name/shared/tmp/pids/puma.pid
ExecStart=/home/user/.rvm/bin/rvm default do bundle exec puma -C /var/www/your_project_name/shared/puma.rb --daemon
ExecStop=/home/user/.rvm/bin/rvm default do bundle exec pumactl -S /var/www/your_project_name/shared/tmp/pids/puma.state -F /var/www/your_project_name/shared/puma.rb stop
Restart=always
# RestartSec=10
[Install]
WantedBy=default.target

How to stop (and restart) the Rails Server?

I'm following the instructions here http://railsinstaller.org/mac to get up and running with Rails on a Mac running OS X 10.8.2
At step 8 I'm asked to restart Rails server but how?
I'm assuming via a command line, but from within the already open ruby terminal window or a new one?
Now in rails 5 you can do:
rails restart
The output of rails --tasks:
Restart app by touching tmp/restart.txt
I think that is usefully if you run rails as a demon
Press Ctrl+C
When you start the server it mentions this in the startup text.
On OSX, you can take advantage of the UNIX-like command line - here's what I keep handy in my .bashrc to enable me to more easily restart a server that's running in background (-d) mode (note that you have to be in the Rails root directory when running this):
alias restart_rails='kill -9 `cat tmp/pids/server.pid`; rails server -d'
My initial response to the comment by #zane about how the PID file isn't removed was that it might be behavior dependent on the Rails version or OS type. However, it's also possible that the shell runs the second command (rails server -d) sooner than the kill can actually cause the previous running instance to stop.
So alternatively, kill -9 cat tmp/pids/server.pid && rails server -d might be more robust; or you can specifically run the kill, wait for the tmp/pids folder to empty out, then restart your new server.
In case that doesn't work there is another way that works especially well in Windows: Kill localhost:3000 process from Windows command line
if you are not able to find the rails process to kill it might actually be not running. Delete the tmp folder and its sub-folders from where you are running the rails server and try again.
I had to restart the rails application on the production so I looked for an another answer. I have found it below:
http://wiki.ocssolutions.com/Restarting_a_Rails_Application_Using_Passenger
I just reboot the server with
Ctrl + c
That worked for me

How to run thin in the vps after the terminal window is closed

Hey guys
After several weeks of local testing, I'm now setting up a VPS, and try to run rails on it. At this point, I can open up a Terminal session and ssh to the VPS, run the thin start, then I the server is running ok, But as soon as I closed the terminal the thin is down.
How can I make thin server running in the VPS all the time?
another question how to change from test mode to production mode in rails.
Thanks
Demonize thin, run it with -d flag.
thin -d
For the task at hand you will want to use a tool called Screens
Install it:
sudo apt-get install screen
Then to run it you run:
screen -d executable
To put screen to background: Ctrl+D
To recall a screen: screen -r.
You should be all good now.
You don't really want to launch and stop thin by hand. You want it to be a daemon to be started when your system start and to be managed like any other daemons (e.g. nginx, syslog, sshd, etc.). How to do this is very distribution-dependent, but you should definitely have a look at /etc/init.d/ or /etc/rc.d/ and /etc/rc.conf.
To go in production mode within the command line you use thin -e production, but the preferred way should be to specify it in thin's configuration files. You should have (or create) an /etc/thin/ folder, with one .yml file for each application you're deploying.

Resources