Nginx and Unicorn Boot Question - Run On Start up?

June 16, 2014 3.3k views
I finally got my rails server up and running on CentOS with Nginx and unicorn. But I noticed the each time the server reboots or power cycles, I have to run "service nginx start" manually and then go into each site and run "unicorn_rails -c config/unicorn.rb -D" for the site(s) to come up. Is there a way to automate this process when the server boots up? Thank you
1 Answer
On CentOS, to ensure that nginx starts on reboot run:
sudo chkconfig --levels 235 nginx on
Then for Unicorn to start on boot, you'd need to write an init script and save it to /etc/init.d/unicorn Here's an example one: https://gist.github.com/mustafaturan/2762372 Make sure to change the variables at the top of the file to match your project. You'll then be able to start and stop your app with:
service unicorn stop
service unicorn start
And to make it run on boot, set:
sudo chkconfig --levels 235 unicorn on
  • If you get the error service unicorn does not support chkconfig from using the above script, you need to add the following to the top of the script (after the first line):

    # chkconfig: 2345 95 20
    # description: Controls Unicorn rails server
    # processname: unicorn
  • If you use Ubuntu, before using service execute these commands:

    sudo update-rc.d <script_name> defaults
    sudo update-rc.d <script_name> enable
Have another answer? Share your knowledge.