Adding init script for Nginx on Ubuntu

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
Important: if you get error something like ‘You don’t have permissions to execute nginx‘, then you have not followed step 2 correctly.  

With above steps nginx init script will get added and it will get booted with System.

Advertisements

2 thoughts on “Adding init script for Nginx on Ubuntu

Add your thoughts...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.