0

I removed the default Nginx package from my server, and compiled it from source here. There is a script on the server that checks Nginx, and reports any problem:

if ($result->num_rows > 0) {
    exec('sudo service nginx configtest 2>&1', $output, $returnCode);
    if ($returnCode === 0) {
        passthru('sudo service nginx restart');
    } else {
        $subject = 'Nginx config test failed on ' .gethostname();
        $message = implode('<br>', $output);
        Mail::sendEmail('it_staff@mydomain.com', $subject, $message);
    }
}

When running service nginx configtest I get:

nginx: unrecognized service

However, running nginx -t returns:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Am I missing a configuration somewhere that I'm unaware of? Nginx is working, but it's saying it's an unrecognized service.

I created a systemd unit file with the following:

[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

I also enabled and started Nginx:

sudo systemctl enable nginx.service
sudo systemctl start nginx.service

Nginx is running, and I can hit web sites using this server. What could I be missing? Thanks.

0
1

The old service call is there for 'legacy' programs, but nginx is a SystemD unit, and not a legacy SysVInit/Upstart service.

service nginx configtest is (generally considered) 'deprecated' in favor of straight sudo nginx -t commands to test the configuration nowadays, and you should always operate with sudo nginx -t as your go-to for configuration testing. Rely only on service / systemctl for stopping, starting, and reloading the service. Do not rely on it for the 'configtest' parameters anymore.

(In the nginx IRC chat on Freenode now LiberaChat, we always pushed for people to use sudo nginx -t for their config tests, and sudo nginx -T to dump their configurations in a full readable form for debugging, so this is the 'standard' I go by since the nginx forums also use this notation more than the service calls.)

5
  • Thank you. Looks like I will have to make a code commit to change the syntax of that check. Simple fix. :) Jun 10 at 14:11
  • Question: Where can I find in the docs that configtest no longer works? I cannot seem to find it anywhere, even when trying to search through the Nginx docs themselves. Sep 18 at 17:01
  • 1
    @DevOpsSauce it's just general knowledge in the nginx world. It's not documented in the nginx documentation. But overall, service nginx ... is a deprecated SysVInit / Upstart mechanism that doesn't translate directly into the SystemD world of nginx. It's usually suggested that, for NGINX, running sudo nginx -t is the way to run a config test, because it's init system independent entirely, which means it works on all the distributions and init systems still out there.
    – Thomas Ward
    Sep 18 at 18:51
  • Thank you very much for the explanation. Sep 20 at 13:20
  • You're welcome, I've added some clarification on this as well to my answer as to why it's generally considered 'deprecated' in favor of the init-system-agnostic way to trigger the same tests.
    – Thomas Ward
    Sep 20 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.