Score:0

nginx: unrecognized service after upgrading Nginx on Ubuntu 18.04

eg flag

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.

Score:1
ru flag

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.)

DevOpsSauce avatar
eg flag
Thank you. Looks like I will have to make a code commit to change the syntax of that check. Simple fix. :)
DevOpsSauce avatar
eg flag
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.
ru flag
@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.
DevOpsSauce avatar
eg flag
Thank you very much for the explanation.
ru flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.