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

4624570-646d9c6d46c1de5f.png
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值