Help us understand the problem. What is going on with this article?

他のプロセスがポートを占有してnginxを再起動できない

More than 1 year has passed since last update.

現象

nginxを起動しようとすると、Address already in useと言われ再起動できない。

$ sudo /usr/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

環境

$ nginx -v
nginx version: nginx/1.12.2

原因

nginxが異常終了する等して古いhttpdプロセスが残っていることが原因。

対策

lsofコマンドで古いプロセスを表示し、killすればよい。

$ sudo lsof -i | grep nginx
nginx    3254     root    6u  IPv4  20710      0t0  TCP *:http (LISTEN)
nginx    3254     root    7u  IPv6  20711      0t0  TCP *:http (LISTEN)
nginx    3255    nginx    6u  IPv4  20710      0t0  TCP *:http (LISTEN)
nginx    3255    nginx    7u  IPv6  20711      0t0  TCP *:http (LISTEN)
nginx    7013     root    6u  IPv4 227085      0t0  TCP *:vcom-tunnel (LISTEN)
nginx    7014    nginx    6u  IPv4 227085      0t0  TCP *:vcom-tunnel (LISTEN)

3254,3255,7013,7014のidをもつプロセスが残っていることが分かる。

$ sudo kill -9 3254
$ sudo kill -9 3255
$ sudo kill -9 7013
$ sudo kill -9 7014

これで起動するようになる。

Yu-s
技術的エアプレイヤー
https://semigeek-dev.hatenablog.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away