22

I've moved from a built-from-src nginx 1.2.6 installation to 1.4.1 on Ubuntu 13.04.

Installed via Ubuntu PPA, http://wiki.nginx.org/Install#Ubuntu_PPA. This all worked great.

Manually, I can reload/start/stop the service using

sudo service nginx <command>

After server reboot, nginx isn't coming up on its own.

In my previous manual installation I had a hand-written Upstart script in /etc/init that worked fine. The PPA installation has setup a /etc/init.d/ script, so I'd like to stick with the PPA methods rather than hacking something in.

It may be that because I'm not deeply familiar with the service interface that there's something I'm missing.

I've also checked /var/log/nginx/access.log and error.log and see nothing recorded during the startup process.

What should I do to fix this?

Geuis
  • 41,122
  • 56
  • 157
  • 219

7 Answers7

34

sudo update-rc.d nginx enable worked for me.

webjay
  • 5,358
  • 9
  • 45
  • 62
19

I don't have much experience with Ubuntu - but have you tried running chkconfig?

You can try:

# sudo update-rc.d nginx defaults

Or:

# sudo apt-get install chkconfig

# sudo chkconfig nginx on
Tony
  • 3,605
  • 14
  • 52
  • 84
badazzhindu
  • 913
  • 7
  • 21
  • Seems it may be working. Rebooting now. ~$ sudo update-rc.d nginx defaults Adding system startup for /etc/init.d/nginx ... /etc/rc0.d/K20nginx -> ../init.d/nginx /etc/rc1.d/K20nginx -> ../init.d/nginx /etc/rc6.d/K20nginx -> ../init.d/nginx /etc/rc2.d/S20nginx -> ../init.d/nginx /etc/rc3.d/S20nginx -> ../init.d/nginx /etc/rc4.d/S20nginx -> ../init.d/nginx /etc/rc5.d/S20nginx -> ../init.d/nginx – Geuis Jun 25 '13 at 00:55
  • 5
    Additional note, chkconfig isn't installable via apt-get. – Geuis Jun 25 '13 at 00:57
  • Yup, worked. chkconfig isn't available in Ubuntu 13.04 it seems. – Geuis Jun 25 '13 at 00:58
  • 5
    You can check the nginx configuration using: `sudo nginx -t` This would normally highlight any issues. Have you got two versions of nginx installed at the same time? – outrunthewolf Jun 25 '13 at 13:40
6

For Ubuntu:

First check the status of it to make sure it's not erroring out

systemctl status nginx.service

It will either show an error or show it's not started

If it's not started then it needs to be enabled

sudo /etc/init.d/systemctl enable nginx
sudo reboot
David
  • 3,488
  • 2
  • 21
  • 21
2

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.

Community
  • 1
  • 1
Visionscaper
  • 3,979
  • 1
  • 23
  • 26
  • I'm getting the same problem, however I don't see where the script you posted is located (on your box). In my case I use ubuntu 15.10 and I suspect the problem with failed startup happens because my express app starts up after nginx (I use pm2). For some reason if I boot in gui mode everything is ok, but when booted in headless mode nginx fails to start. – Pavel P Mar 22 '16 at 22:45
  • Hi @Pavel, please see the "Ubuntu Upstart" script [1]. There it is stated "Save this file as /etc/init/nginx.conf", so that is the location of the script. Could it be that in GUI mode the network interface is already up when NGinX starts, and in headless mode not? [1] https://www.nginx.com/resources/wiki/start/topics/examples/ubuntuupstart/# – Visionscaper Mar 27 '16 at 21:03
  • After some googling and testing, I saw in nginx logs why it wasn't starting, and it appears that it was failing because it wasn't able to connect to my node.js app that was declared as localhost:3333 (nginx is reverse proxy). it appears that because of timing apparently `localhost` might not resolve on startup (wtf!?). In my case I had to replace it with 127.0.0.1 and it fixed the problem. – Pavel P Mar 28 '16 at 00:05
0

In addition to Visionscaper answer, nginx installed from standard trusty repos uses sysvinit service file not upstart one. Thus instead of nginx service for upstart edit rc-sysinit upstart service. It's a good idea to use upstart overrides for this:

# echo "start on (filesystem and net-device-up IFACE!=lo) or failsafe-boot" > /etc/init/rc-sysinit.override
reddot
  • 764
  • 7
  • 15
0

For me it was because i didnt do a sudo apt-get update --fix-missing before installing sudo apt-get install nginx

Architecture: armv7l

OS: raspLITE

0

you must stop apache2 if you use it

service apache2 stop

then start ths nginx service again

service nginx start
Youssri Abo Elseod
  • 671
  • 1
  • 9
  • 23