commands to stop & start nginx - ruby-on-rails

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.

Related

How can one shutdown nginx and cron when they are monitored with supervisord?

I am trying to automate deployment of a webapplication, updating the app requires shutting down cron and nginx.
Problem is, when I stop the process via service nginx stop and service cron stop, these are restarted by supervisord.
There is no init.d script for supervisord, furthermore I am not certain if one is to use supervisorctl to manage services.
What is the proper approach?
You need to use supervisorctl for this. But that would only work if you have supervisorctl configured in your supervisorconfig
So you need to use
$ supervisorctl status
This will give you the names of the services and then you can use
$ supervisorctl stop nginx-program
$ supervisorctl stop cron-program
That is how you should be handling it

Rails server start/stop

To run the Rails server, I use $rails server. It says 'To stop, click Ctrl+c'.
I use Putty.
The questions are:
Should I keep the terminal open always? Because if the server stops, my web page wouldn't work. With Apache I just used commands apachectl start/stop.
What if I want to use a command? Should I stop the server, use command, and run again? Because in the same terminal I can't do enything if the server run.
you can run it in background by writing
daemonize true
in your puma.rb file
To stop you need to find your running puma process
ps aux | grep puma
then you need to kill the main process
sudo kill -9 your process id
to start you need to type
puma -C config/puma.rb
You can start a daemonized server by adding -d to your command. For instance:
rails server -d
To stop the server, you can kill it based on its process id:
kill $(cat tmp/pids/server.pid)
Should I keep the terminal open always? Because if the server stops, my web page wouldn't work. With Apache I just used commands apachectl start/stop.
Yes, you should keep it open because if you Ctrl C it will stop the server. Another option is to run it in the background but I'm not sure how to do that in Putty.
What if I want to use a command? Should I stop the server, use command, and run again? Because in the same terminal I can't do enything if the server run.
Can you open another terminal window? If you have two terminal windows you can use one for running the server and another for other tasks.

Unable to start webbrick again without restarting ec2 instance

I am running a rails 3.2 application on amazon ec2 in development environment and detach mode.
$ rails s -d
After this command the ec2 terminal hangs and does not come out of this command but the server starts as I can access the application.I have to close the terminal and the server remains started.
After this I kill the application.
$ lsof|grep 3000
$ kill -9 <pid>
Now if I try to restart the server, it gives error.
A server is already running. Check /home/ubuntu/trade_ship/tmp/pids/server.pid.
Exiting
Now even if I delete the tmp folder and recreate it, the server won't start. Can anyone help me with these two issues?
Even I had faced that issue, try to restart your system and then check.. this solution worked for me at that time.
First of all if you are not able to use port 3000 use rails s -p <port no> command
Second is if you have to kill the RUBY instance which server started so use
ps aux | grep ruby
username 17731 0.1 1.6 3127008 67996 ?? S 2:00PM 0:01.42 /Users/username/.rvm/rubies/ruby-1.9.2-p180/bin/ruby script/rails s -d
and then kill
kill -9 17731
This will definitely solve the issue

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

Unicorn init script - not starting at boot

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'

Resources