Puma does not start after server reboot - ruby-on-rails

I deployed project with capistrano, but puma does not start after server reboot..
I shoul do -> cap production puma:start every time
I tried it:
/etc/init.d/myscript
#!/bin/sh
/etc/init.d/puma_start.sh
puma_start.sh
#!/bin/bash
puma -C /root/project/shared/puma.rb
but, I have error
/usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems.rb:270:in `find_spec_for_exe': can't find gem puma (>= 0.a) (Gem::GemNotFoundException)
from /usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems.rb:298:in `activate_bin_path'
from /usr/local/rvm/gems/ruby-2.3.3#project/bin/puma:22:in `<main>'
from /usr/local/rvm/gems/ruby-2.3.3#project/bin/ruby_executable_hooks:15:in `eval'
from /usr/local/rvm/gems/ruby-2.3.3#project/bin/ruby_executable_hooks:15:in `<main>'
if I put in the console root#host:~# puma -C /root/project/shared/puma.rb it work, and all okey.
I think I have not correct path to gem puma
How can I do puma autostart after server reboot
Thank You

Beginning with Ubuntu 16.04 recommended use systemctl. Before I used upstart.
I created this instruction for myself. Maybe it will be useful to someone.
https://gist.github.com/DSKonstantin/708f346f1cf62fb6d61bf6592e480781
Instruction:
Article: https://github.com/puma/puma/blob/master/docs/systemd.md
#1 nano /etc/systemd/system/puma.service
#2 paste from puma.service
Commands:
# After installing or making changes to puma.service
systemctl daemon-reload
# Enable so it starts on boot
systemctl enable puma.service
# Initial start up.
systemctl start puma.service
# Check status
systemctl status puma.service
# A normal restart. Warning: listeners sockets will be closed
# while a new puma process initializes.
systemctl restart puma.service
puma.service file
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=<path_to_project>/current
Environment=SECRET_KEY_BASE='<SECRET KEY>'
ExecStart=/usr/local/rvm/bin/rvm <ruby_version>#<gemset_name> do bundle exec puma -C <path_to_project>/shared/puma.rb --daemon
ExecStop=/usr/local/rvm/bin/rvm <ruby_version>#<gemset_name> do bundle exec pumactl -S <path_to_project>/shared/tmp/pids/puma.state -F <path_to_project>/shared/puma.rb stop
#Restart=always
Restart=on-failure
[Install]
WantedBy=multi-user.target

I found this http://codepany.com/blog/rails-5-puma-capistrano-nginx-jungle-upstart/
it was help for me ->
cd ~
$ wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma-manager.conf
$ wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/upstart/puma.conf
Open downloaded puma.conf file and set your system’s user account for setuid and setguid. (in our case we use root account, but it’s recommended to use a less-priviliged account):
vim puma.conf
setuid root
setgid root
Move downloaded upstart files to /etc/init and create another puma.conf
$ sudo cp puma.conf puma-manager.conf /etc/init
$ sudo touch /etc/puma.conf
Open /etc/puma.conf and add path to app:
/root/name_of_your_app/current
Open /etc/init/puma.conf , and to find something similar
exec bundle exec puma -C /root/project/shared/puma.rb
and replace path to your file puma.rb
Thank you

There's actually a pretty straightforward way of diagnosing and solving this problem:
1. Locate your rvm executable.
which rvm
In my case it was:
/usr/share/rvm/bin/rvm
...but yours may be different! So you have to do that first to find out where your executable is.
2. Find out what Ruby version your server is running.
ruby --version
For me it was 2.6.2. All you need is that version number. Nothing else.
3. Try something like what Konstantin recommended but do this instead:
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory= /var/www/your/current
ExecStart=/usr/share/rvm/bin/rvm 2.6.2 do bundle exec pumactl -S /var/www/your/shared/tmp/pids/puma.state -F /var/www/your/shared/puma.rb start
ExecStop=/usr/share/rvm/bin/rvm 2.6.2 do bundle exec pumactl -S /var/www/your/shared/tmp/pids/puma.state -F /var/www/your/shared/puma.rb stop
# Restart=always
Restart=on-failure
[Install]
WantedBy=multi-user.target
4. Then it's a simple matter of:
systemctl daemon-reload
systemctl enable puma.service
systemctl start puma.service
systemctl status puma.service
Then that's it! Next time you boot your server, puma should boot fine.

Related

reboot script to start rails server

My rails server is running under a deployer user and I normally start it like this
#restart.sh
#!/bin/bash
sudo -u deployer -H bash -l
cd /var/www/html/cms/
./server -e staging start
So when I login over SSH i run ./restart.sh and that works great.
I'm trying to do this automatically after a reboot so I've added a reboot_site.service in
/etc/systemd/system/reboot_site.service
# /etc/systemd/system/reboot_site.service
[Unit]
Description=reboot_site
After=syslog.target network.target
[Service]
Type=simple
WorkingDirectory=/var/www/html/cms
# If you use rbenv:
# ExecStart=/bin/bash -lc '/home/deploy/.rbenv/shims/bundle exec sidekiq -e production'
# If you use the system's ruby:
ExecStart=/home/ec2-user/restart.sh
[Install]
WantedBy=multi-user.target
The service is enabled using sudo systemctl enable reboot_site.service
However when I now reboot the server the script stops execution after the following line
sudo -u deployer -H bash -l
Server is an AWS EC2 AIM instance
What's wrong with my configuration to make this work and how can I fix this?
It stops on sudo -u deployer -H bash -l because your script just changes user and will not continue until you logout as this user. So the remaining commands in your script are not executed.
I would suggest the following version which uses runuser:
#!/bin/bash
runuser -l deployer -c 'cd /var/www/html/cms/ && ./server -e staging start'
if you just need to run a script on startup you can configure a crontab for this
#crontab -e
#reboot /home/ec2-user/restart.sh

Starting god as deploy user

I have an init.d script to start up god on my server after a reboot.
I've run sudo chmod +x /etc/init.d/god and sudo update-rc.d -f god defaults and when I run /etc/init.d/god start as the deploy user I have no issues and god starts.
However, when I reboot the server god doesn't start.
When I try and start god manually as root I get this error:
Your Ruby version is 1.9.3, but your Gemfile specified 2.3.0
I believe the issue is something to do with root not having rvm or ruby 2.3.0. Is there a way to run the init.d script as deploy?
I'm on Ubuntu 14.04, ruby 2.3.0 and god 0.13.7
You can run any command (or execute a script) as any user with the sudo command; just use the -u flag to specify the user. Example:
sudo -u deploy /etc/init.d/god
See more here: http://www.sudo.ws/man/1.8.15/sudo.man.html

Why I am not able to run Unicorn RoR

I am trying to install a RoR server on Amazon EC2 using Amazon Linux and Ansible. Everything goes through fine, at the end I am not able to run unicorn. I have checked that unicorn gem is installed
`gem list | grep unicorn`
showed unicorn installed.
When I type
`unicorn_rails`
I get command not found error. I checked my gem file and it has the line gem unicorn then I run bundle install and the output has unicorn in the list. Still not able to run it.
Thanks.
I'm not sure that you can run unicorn_rails. Just use:
bundle exec unicorn -p <port> -c <path_to_config_file>
For example:
bundle exec unicorn -p 3000 -c ./config/unicorn.rb
If you are using bundler run unicorn with bundle exec:
$ bundle exec unicorn_rails
Find out more here.

master failed to start, check stderr log for details

I'm trying to start unicorn and I keep getting this error constantly.
My unicorn.rb (https://gist.github.com/anonymous/d1f3d9bcdd1a6c4d8435)
Command that I'm using to start unicorn:
/home/app/adsgold/# unicorn_rails master -c config/unicorn.rb -D -E production
Commands that I've already tried:
/home/app/adsgold/# unicorn_rails -c config/unicorn.rb -D -E production
/home/app/adsgold/# unicorn_rails master -c config/unicorn.rb -D -E production -p 3000
/home/app/adsgold/# bundle exec master unicorn -c unicorn.cnf -E production -D
Complete error beeing shown: https://gist.github.com/anonymous/828d9677f928fa671762
It looks you have RVM and Ruby installed system-wide. Generally it may cause lots of issues. Even RVM documentation warns about that. Try to install RVM and Ruby as user, which owns app directory. In that case you will get consistent system.
By the way, do you have this directory /home/deploy/apps/shared on your environment? Is it writable for your App? According Unicorn config following things depend on it:
pid "/home/deploy/apps/shared/pids/unicorn.pid"
stderr_path "/home/deploy/apps/shared/log/unicorn.stderr.log"
stdout_path "/home/deploy/apps/shared/log/unicorn.stdout.log"
If you do have all this stuff, content of /home/deploy/apps/shared/log/unicorn.stderr.log also would be helpful.
Are the necessary permissions in place? (I notice the read error comes from a globally-installed gem, so I'm wondering what all is where.)

start unicorn app server when the ubuntu server starts

I am running my rails application using ruby enterprise edition with unicorn as app server. I run this command
bundle exec unicorn -D -c /home/ubuntu/apps/st/config/unicorn.rb
I need to run this command soon after the system reboots or starts. I am running the app on ubuntu 10.04 LTS EC2 instance. I tried couple of examples which are mentioned on this site as well as this site but it’s not working for me. Any heads up
Try it as an Upstart. To do so, you need to create a myapp.conf file into the directory /etc/init/ with the contents below:
description "myapp server"
start on runlevel [23]
stop on shutdown
exec sudo -u myuser sh -c "cd /path/to/my/app && bundle exec unicorn -D -c /home/ubuntu/apps/st/config/unicorn.rb"
respawn
After that, you should be able to start/stop/restart your app with the commands below:
start myapp
stop myapp
restart myapp
Use ps -aux | grep myapp to check if your app is running.
You can use this file as a template, set appropriate paths mentioned in this file, make it executable and symlink into /etc/init.d/my_unicorn_server. Now you can start the server using:
sudo service my_unicorn_server start
Then you can do:
sudo update-rc.d my_unicorn_server defaults
To startup the unicorn server on system reboot automatically.
In my case, I just wanted it quick so I place the startup command in /etc/rc.local like below. Note that i'm using RVM.
# By default this script does nothing.
cd <your project dir>
/usr/local/rvm/gems/ruby-2.2.1/wrappers/bundle exec unicorn -c <your project dir>/config/unicorn.conf -D
test -e /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server
exit 0
Make sure your startup command is above the exit 0. After you reboot, check whether it is running or not by directly hitting the url of your application or use ps -aux | grep unicorn command.
Note* Previously I use Phusion Passenger but I'm having trouble to see its error log, so I move back to unicorn. I also tried #warantesbr without success, which I guess it fails because my whole environment where setup using root access.
If you are using unicorn_init script
You can configure a cron job to start the unicorn server on reboot
crontab -e
and add
#reboot /bin/bash -l -c 'service unicorn_<your service name> start >> /<path to log file>/cron.log 2>&1'

Resources