How to restart Nginx in Ubuntu or other linux servers [closed]

Posted on Apr 8, 2022

Question

I used this sh file to install Nginx. When I modify the nginx.conf and try to reload or restart Nginx it didn't restart. I used below command.

sudo systemctl restart nginx

gave me

sudo: unable to resolve host localhost.localdomain sudo: systemctl: command not found

and this one

sudo service nginx restart

sudo: unable to resolve host localhost.localdomain nginx: unrecognized service

and this one

sudo /etc/init.d/nginx restart

sudo: unable to resolve host localhost.localdomain sudo: /etc/init.d/nginx: command not found

Answer

The nginx web server can be restarted using any one of the following command line syntax. Use systemctl on systemd based version such as Ubuntu Linux 16.04LTS and above:

sudo systemctl restart nginx

OR

sudo service nginx restart

OR (older Ubuntu Linux version):

sudo /etc/init.d/nginx restart

The same commands can be used to start / stop / restart the nginx server on a Red Hat 7 versions:

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

OR

sudo service nginx start
sudo service nginx stop
sudo service nginx restart

OR

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

To view status of your nginx server, use any one of the following command:

sudo service nginx status

OR

sudo /etc/init.d/nginx status

OR for Red Hat 7, CentOS 7 and higher

sudo systemctl status nginx