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.
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
$ 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.
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>
$ docker restart nginx
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