Skip to main content
How To

Restart Nginx on Ubuntu

Here's how you can restart Ngnix safely on Ubuntu.

Sagar Sharma

If you are just getting started with Nginx, you may find difficulty in restarting the Nginx service.

And if you are using Ubuntu, the Nginx can easily be restarted using the following command:

sudo systemctl restart nginx

But if you want to learn more like how you can gracefully restart the Nginx, you can refer to the following tutorial.

How to restart the Nginx on Ubuntu

In this section, I will show you the following:

  • Start, stop, and restart the Nginx using the systemd
  • Start, stop, and restart the Nginx using the Nginx script (useful if you're using a different init system)

But before that, let's have a look at what the start, stop, and restart mean.

Using systemd, you can do various operations related to the process and the major ones are:

  • start: Starts the process.
  • stop: Stops the process.
  • restart: It will stop the process and start it again.
  • reload: It is also known as gracefully restart. Here, it will only stop the child processes, load the new config and start the child processes.

Now, let's jump to the how-to part.

How to restart Nginx using the systemctl

To restart the Nginx, all you have to do is execute the following command:

sudo systemctl restart nginx

Similarly, you can use the different systemd unit commands (start, stop, etc.).

Let me show you how.

How to start Nginx using systemctl

To start the Nginx, you'd have to use the start systemd unit command as shown:

sudo systemctl start nginx

And now, if you check the status, the service should be up and running:

systemctl status ngin
check status of nginx in Ubuntu

How to stop the Nginx using systemctl

To stop the Nginx, you'd have to use the systemctl in the following manner:

sudo systemctl stop nginx
stop the Nginx service using the systemctl command in ubuntu

And as you can see, when I checked the status, it showed me that the Nginx is not running.

How to reload the Nginx using the systemctl

💡
Service can only be reloaded if it is running!

Reloading the service is known as a gentle restart as it won't kill the entire process and will only restart the child processes after taking effect from the new config.

To reload the Nginx, all you have to do is use the following command:

sudo systemctl reload nginx

But what if you're using a different init system than systemd? Here's the solution.

How to restart the Nginx using the Nginx script

This section will be helpful for those who are running a different init system than the systemd.

To restart the service using the Nginx, all you have to do is use the following command:

sudo /etc/init.d/nginx restart
restart nginx service without systemd

How to start the Nginx using the Nginx script

To start the Nginx, use the following command:

sudo /etc/init.d/nginx start
start the nginx service without systemd

And it will start the Nginx service.

Similarly, you can use this script to check whether the service is running or not:

sudo /etc/init.d/nginx status
check the Nginx status without systemd

How to stop the Nginx using the Nginx script

To stop the service, all you have to do is append the stop after adding the path to the Nginx script followed by one space:

sudo /etc/init.d/nginx stop
stop the Nginx service without systemd

And as you can see, when I checked the status, Nginx was not running!

How to reload the Nginx using the Nginx script

To reload the Nginx, all you need to do is execute the following command:

sudo /etc/init.d/nginx reload
reload the nginx service without systemd

Want to create a secure web server using NGINX and certbot? I got you

If you want to create a secure web server from scratch using Nginx and certbot, we have a dedicated guide that covers everything from the installation to getting the certificates right:

Using Certbot With Nginx Web Server
Certbot makes the SSL certificate deployment and renewal such an effortless process. Learn how to use certbot with Ngnix.

I hope you will find this guide helpful. And if you have any queries, feel free to ask in the comments.

Sagar Sharma