Launch Nginx on startup - when nginx is installed with passenger-install-nginx-module

Go to /etc/init.d and run sudo vim nginx-passenger.sh, then paste in this script:

================ New Script Start ============
This works in ubuntu 18.04

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

sudo /opt/nginx/sbin/nginx

================ New Script End =============

================ Old Script Start ============
This is an old script This works in ubuntu 16.04 but does not work in Ubuntu 18.04 . So, I updated the script.

#!/bin/bash
# this script starts the nginx process attached to passenger
sudo /opt/nginx/sbin/nginx

================ Old Script End =============

save and exit. Make the file executable by typing sudo chmod +x /etc/init.d/nginx-passenger.sh
You can test to see if the script works by typing sudo /etc/init.d/nginx-passenger.sh this will run all the code in the script. Verify that it launches nginx before continuing.

Then run sudo update-rc.d nginx-passenger.sh defaults while still in the /etc/init.d directory. Once all of this is in place, reboot your server and ngnix should now be automatically spawned on startup.

Source: https://serverfault.com/questions/69350/launch-nginx-on-startup

Topic: