UPDATE 28 March 2016
I have submitted the small improvement to the Ubuntu Upstart script, that I proposed below, as a pull request to the NGinX wiki. My pull request has been accepted and the patch has been merged.
I'm on Ubuntu 14.04 LTS and had the same issue; NginX could start fine using sudo service nginx restart
but it did not start automatically at boot up of the server.
I followed the instructions of the NGinX Ubuntu Upstart Wiki page, but it didn't work (initially). So I dug around a bit. First I found that Ubuntu did try to start NGinX at start-up but failed:
$ sudo cat /var/log/boot.log
...
* Starting nginx http daemon [fail]
...
Then I found the log file of the initctl upstart jobs:
$ cat /var/log/upstart/nginx.log
...
nginx: [emerg] host not found in upstream "<mydomain>:443" in /etc/nginx/sites-enabled/mydomain:2
...
So, I figured out that the NGinX start up script was run before the network interface was up. This server fault answer gave me the hints I needed to fix the NginX Ubuntu Upstart script. In summary, a '!' was missing, IFACE=lo
needed to be changed in to IFACE!=lo
. For convenience I have pasted the complete script below:
# nginx
description "nginx http daemon"
author "George Shammas <georgyo@gmail.com>"
start on (filesystem and net-device-up IFACE!=lo)
stop on runlevel [!2345]
env DAEMON=/usr/sbin/nginx
env PID=/var/run/nginx.pid
expect fork
respawn
respawn limit 10 5
#oom never
pre-start script
$DAEMON -t
if [ $? -ne 0 ]
then exit $?
fi
end script
exec $DAEMON
After rebooting I checked if NGinX was running now:
$ sudo initctl list | grep nginx
nginx start/running, process 1551
I will notify the author of this script about my findings.