Nginx hangs using "service nginx start"

Solution 1:

I'm having the same issue with Ubuntu 16.04, systemd and NginX 1.10.1 from source.

I was using the default nginx.service file: https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

The issue was the nginx.pid location. To fix it, I:

  1. Fired up Nginx without the service

    sudo nginx start
    
  2. Updated the locate db

    sudo updatedb
    
  3. Found the location of the pid file

    locate "nginx.pid"
    
  4. Updated the nginx.service file to the location where I found it

    PIDFile=/usr/local/nginx/logs/nginx.pid
    

    (No idea why it was stored in my logs dir...)

  5. Then run daemon-reload to reload the nginx.service file

    systemctl daemon-reload
    

Afterwards, systemctl start nginx works like a charm. Hope this helps.

Solution 2:

It hangs because of this error:

PID file /var/run/nginx.pid not readable (yet?) after start

Newer Linux distros comes with systemd. If you use a service bundled with your distro you'll get it already configured for systemd.

Since you are compiling nginx from the sources and you are using a SysV init file (/etc/init.d/nginx), systemd will use a generator to parse it (systemd-sysv-generator).

In your SysV script, you define the pid file and starts the process with:

NGINX_PID=/var/run/nginx.pid
...
/sbin/startproc -p $NGINX_PID $NGINX_BIN

If I'm not wrong you are using a SUSE Linux init script on Ubuntu (because of the startproc command), that startproc command only reads the pid file (specified by the -p parameter), it doesn't create it, thus systemd can't find a pid file and it hangs.

In your case, the solution is either to create the pid file in your SysV init script (on the /var/run/nginx.pid location), use an Ubuntu SysV init script, or a systemd one.

This may also happen (not what's happening to you) when you have a correct SysV init script which creates a pid file, but it's different from the one commented at the top of the file. The systemd generator reads the comments, for example this one:

# pidfile: /var/run/nginxd.pid

And uses the pidfile defined there.

More information:

  • nginx init scripts for SysV init, systemd, etc.
  • SysV generator processing the pidfile comment.
  • How systemd reads SysV init scripts.
  • SysV generator man page.
  • startproc source code (SUSE FTP site).