Unicorn init script - not starting at boot - ruby-on-rails

I'm very new to system administration and have no idea how init.d works. So maybe I'm doing something wrong here.
I'm trying to start unicorn on boot, but somehow it just fails to start everytime. I'm able to manually do a start/stop/restart by simply service app_name start. Can't seem to understand why unicorn doesn't start at boot if manual starting stopping of service works. Some user permission issue maybe ??
My unicorn init script and the unicorn config files are available here https://gist.github.com/1956543
I'm setting up a development environment on Ubuntu 11.1 running inside a VM.
UPDATE - Could it be possible because of the VM ? I'm currently sharing the entire codebase (folder) with the VM, which also happens to contain the unicorn config needed to start unicorn.
Any help would be greatly appreciated !
Thanks

To get Unicorn to run when your system boots, you need to associate the init.d script with the default set of "runlevels", which are the modes that Ubuntu enters as it boots.
There are several different runlevels, but you probably just want the default set. To install Unicorn here, run:
sudo update-rc.d <your service name> defaults
For more information, check out the update-rc.d man page.

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'

Related

Starting Nitrogen Web framework at boot time

I have always started nitrogen to run as a daemon using the command below:
sudo /home/someuser/myapp/bin/nitrogen start
It works well but i have to repeat the same activity should the server reboot.
Most web servers by default start at boot time. When nitrogen is started, it starts the underlying Erlang web server. Unfortunately I have not found any single resource talking about starting nitrogen at boot time.
How do you start nitrogen as a daemon at system boot time?
The easiest solution is to use the /etc/rc.local file. By default, it's empty.
Since rc.local runs as root, you can use it as such (though if you prefer to run Nitrogen as a separate user, using su -c "command" username) is good.
Anyway, the simple solution is to add to your rc.local file the following:
To run as root:
/home/someuser/myapp/bin/nitrogen start
To run as another user:
su -c "/home/someuser/myapp/bin/nitrogen start" someuser
That will launch Nitrogen appropriately and will let you connect to the VM using bin/nitrogen attach.
My previous recommendation of using sudo is not sufficient, as it doesn't reset the environment to the user you want.
I'm using this in production on Ubuntu 14.04 and a linode VPS.
I hope that helps.

Unable to bind to port 80, but running on the current shell works without any issues

I get the following error while trying to run "cap production unicorn:start"
F, [2013-07-12T04:36:18.134045 #28998] FATAL -- : error adding listener addr=0.0.0.0:80
/home/ec2-user/apps/foo_prod/shared/bundle/ruby/2.0.0/gems/unicorn-4.6.3/lib/unicorn/socket_helper.rb:147:in `initialize': Permission denied - bind(2) (Errno::EACCES)
Running the following command manually, does work without any issues. What could be the problem here?
rvmsudo unicorn_rails -c config/unicorn/production.rb -D --env production
You need root access to bind to lower ports like port 80. Command rvmsudo executes in root context and therefore it works.
Cap task executes in a normal user context (probably deploy) depending on your configuration. You should add sudo ability to cap deploy user and make sure your cap task uses sudo to start unicorn.
Answer by #Iuri G. gives you reason and possible solution.
I have another suggestion, unless you have extremely compelling reason to run Unicorn with port 80, change that to a higher port (>1024), like 3000. This will solve your problem.
If it is an application that is exposed to public, it is too easy to overwhelm Unicorn and make your application unavailable to end users. In such a case, do put Unicorn behind a proxy (like Nginx). The proxy will be on port 80 and Unicorn on a higher port.
In my development environment, using RubyMine, I ran into this recently.
I used SSH to redirect port 80 to 8080.
sudo ssh -t -L 80:127.0.0.1:8080 user#0.0.0.0
I assume you are running Ubuntu as production server. On your server you need to edit your sudoers file:
First type select-editor and select nano (or another editor you feel confortable with)
Then at the bottom of the file, before the include line, add this line:
%deployer ALL=(ALL)NOPASSWD:/path/to/your/unicorn_rails
You need to replace deployer by the user name you are using with capistrano, and to replace /path/to/your/unicorn_rails with its correct path. This will allow your deployer user to "sudo unicorn_rails" without being prompt for a password.
Finally edit your unicorn:start capistrano task, and add rvmsudo ahead of your command line that start unicorn:
rvmsudo unicorn_rails -c config/unicorn/production.rb -D --env production
If it does not work you can try this instead
bundle exec sudo unicorn_rails -c config/unicorn/production.rb -D --env production

How to start Sidekiq worker on Ubuntu VPS (Digitalocean)

I already setup Redis, Sidekiq and Rails app, I can access it form //url/sidekiq, but how do I start the Sidekiq worker on a VPS? On my local I do:
bundle exec sidekiq -q carrierwave,5 default
What should I do on a VPS hosting?
Thanks
Looks like this is a duplicate of this question: how to detach sidekiq process once started in terminal
You have to run the following command from your Rails root:
bundle exec sidekiq -d -L sidekiq.log -q mailers,5 -q default -e production
This will detach the process so you can quit the ssh session and the command will keep running in the background, logging the output to the sidekiq.log file.
Take care to choose the appropriate position for the log file, and don't forget to setup a logrotate rule for it.

How to kill Rackup process in background?

I managed to host the RedMine using the Rackup and Puma by running the following code in the CMD.
rackup -I "script/rails" -s "puma" -O "-q" -E "production"
But this will keep the CMD still up and running. Thus, I created a windows service to run a .BAT file that will execute this command. It worked and the RedMine is now hosted in the background
And now my problems appears. I am now unable to stop the RedMine. Even if I stopped the service that run the .BAT file, the RedMine is still hosted. This is because I do not know how to kill the rackup process in the OnStop() function of the windows service.
Only way I could kill it is by killing the ruby.exe process. Hope you all could guide me to do this in a better way. Thanks

commands to stop & start nginx

I am developing Rails v2.3.2 app. on a Ubuntu machine.
Now, I would like to have two rake tasks to stop and start *Nginx* respectively , what are the commands for this?
task :start_nginx do
puts "stop Nginx..."
system '...' #What is the command to stop Nginx?
end
task :stop_nginx do
puts "stop Nginx..."
system '...' #What is the command to start Nginx?
end
If you just installed the regular nginx package on Ubuntu, you already have a start script like:
/etc/init.d/nginx (start|stop)
If you don't have that, please have a look into the documentation:
Start the server by running /usr/local/nginx/sbin/nginx as root.
You stop the server by executing
kill `cat /var/run/nginx.pid`
The polically-correct way in Ubuntu is to use service instead of init.d
sudo service nginx stop
sudo service nginx start
sudo service nginx restart
Explanation on the difference
It's system dependent.
If you're using upstart, the commands would be:
start nginx
stop nginx
Otherwise it may be one of:
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/rc.d/nginx start
/etc/rc.d/nginx stop
Or even maybe:
nginx
killall -9 nginx
On debian/ubuntu
/etc/init.d/nginx (start|stop|reload)
On centOS/RHEL
service nginx (start|stop|reload)
You must be logged in as root.

Resources