We often need to start nginx manually if we reboot the system for some reason, In this post I’m going to illustrate how to add init script for nginx, so that nginx will start on system reboot.
Step 1: Install script from remote source with following command
sudo wget <a tabindex="-1" href="https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx" target="_blank">https://raw.github.<wbr />com/JasonGiedymin/ng<wbr />inx-init-ubuntu/mast<wbr />er/nginx</a> -O /etc/init.d/nginx
with above command init script will get installed in /etc/init.d/nginx
Note: If nginx is installed in /usr/local/nginx, you can skip step 2
step 2 : Change NGINX_CONF_FILE path in init file
Most of the Rails developers installs nginx with passenger, while installing nginx as a common practice developers use installation directory as /opt/nginx or some/custom/path
if you open /etc/init.d/nginx and search NGINXPATH, you should see the following lines
DESCRIPTION=${DESCRIPTION:-"Nginx Server..."} # process description NGINXPATH=${NGINXPATH:-<em>/usr/local/nginx</em>} # root path where installed DAEMON=${DAEMON:-$NGINXPATH/sbin/nginx} # path to daemon binary NGINX_CONF_FILE=${NGINX_CONF_FILE:-$NGINXPATH/conf/nginx.conf} # config file path
Find NGINXPATH variable in script file and change its value to /opt/nginx or some/custom/path instead of /usr/local/nginx, if its not some/custom/path
After above change following line
NGINXPATH=${NGINXPATH:-<em>/usr/local/nginx</em>}
will change to
NGINXPATH=${NGINXPATH:-/opt/nginx} or NGINXPATH=${NGINXPATH:/<em>some/custom/path</em>}
step 3 : Make script executable with following command
sudo chmod +x /etc/init.d/nginx
step 4: Update OS init with
sudo update-rc.d nginx defaults
if something goes wrong, you can always remove init entry from OS init with
sudo update-rc.d -f nginx remove
If above steps are correctly followed. you can check installation with following commands
sudo service nginx status # to poll for current status sudo service nginx stop # to stop any servers if any sudo service nginx start # to start the servers
With above steps nginx init script will get added and it will get booted with System.
You can also use monit(http://mmonit.com/monit/) to start nginx, after system is rebooted.
Yes, we can do this with monit but if you want to install nginx as a ‘service’ you need init script.