Linux: How to Restart Nginx on Ubuntu/Centos/Docker

linux restart nginx

How often do you restart ngnix service on Linux webserver? This blog post will cover various ways to restart Nginx on a Linux system via command line and on Nginx running in a docker container. I have covered on both on a Systemd server and SysV init/Upstart system.

Open your terminal and login with root/sudo user to run below shown commands. Read also on 2 Ways to SSH into a Running Docker Container.

Restart Nginx HTTP server

CentOS 7,  Ubuntu 18.04  and Ubuntu16.04 are a systemd operating system. To restart nginx service, you'll need to use systemctl command line tool.

It is recommended to check syntax before restarting nginx service,

$ sudo nginx -t
$ sudo systemctl restart nginx

If you're restarting after modifying nginx service unit file, you need to reload systemd first

$ sudo systemctl daemon-reload

Then restart nginx

$ sudo systemctl restart nginx

If you want to reload the configuration without restarting the service, i.e maintain current sessions, use

sudo systemctl reload nginx

Restarting Nginx on Upstart/SysV init system

If you're running a system with upstart or SysV init system. e.g Ubuntu 14.04, CentOS 6, you need to manage nginx service using the service command.

$ sudo service nginx restart

You can also use an absolute path to init script, e.g

sudo /etc/init.d/nginx restart

Restarting Nginx inside docker container

For nginx running inside a Docker container, it is advisable to reload the configuration instead of restarting the service. Also, advise to read  how to use Docker without sudo on Ubuntu.

docker exec <nginx-container-name-or-id> nginx -s reload

If you must restart the nginx process, then consider restarting the container using the command:

$ docker restart <container name|id>

Example

$ docker restart nginx

Where nginx is the name of nginx container.

Restarting nginx is a simple process but you need to be careful to ensure that any change made has correct syntax to avoid failure during a restart. The command used to check for configuration syntax is nginx -t

Read Also:

Josphat Mutai 6:16 am

About Josphat Mutai

Hat Certified Engineer with more than 2 years experience managing critical Cloud Infrastructures and Services. Passionate about Technology and loves testing and reviewing gadgets and Open-source tools.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.