What's the difference between starting nginx with command "nginx", "service start nginx" and "systemctl nginx start"?

Viewed 2377

I have noticed that when ever I start nginx with ubuntu command "nginx" and I do systemctl status nginx. It shows that systemctl is disabled. More over if I first start nginx with command systemctl start nginx and i try to start nginx with command nginx, it check the availbility of the ports and then says nginx: [emerg] still could not bind(). So i thought there must be a differene and their purpose. When I strt nginx with command nginx the only way to stop nginx is by the means of force using killlall nginx or kill -9 (process id) or by clearing the port. So I am pretty sure there is some difference in them.

2 Answers

The difference between the examples you have provied is how the processes are being started.

Running the command nginx will start the application and wait for your user action to stop it.

The systemctl or service commands are nearly the same thing and running service nginx start or systemctl start nginx will start a service in the background running the Nginx daemon.

You can also use this to do a service nginx restart or systemctl restart nginx to restart the service, or even a service nginx reload / systemctl reload nginx to reload the configuration without completely stopping the Nginx server.

The reason why you can't do both nginx and systemctl start nginx is due to the nginx configuration already listening port 80, and you can't listen on the same port on a single IP address at the same time.

You can also force the nginx service to start on boot by running systemctl enable nginx which will be why your systemctl status nginx returns 'disabled'.

Hope this makes sense.

service command is just a simple script which basically abstract choosing the underlying init system (upstart, systemmd , /etc/init.d or systemctl).

since it being a very concise script, it only supports a very limited set of operations (start | stop | reload .. ).

However, if you actually want to perform the additional operation you need to make use of the actual init system in this case systemctl

An apt example would be starting the service on boot time using systemctl sudo systemctl enable the-name-of-service which is not possible using service

Related