【Nginx】portが占領されてサーバーが起動できないときの対処法

##ログの確認
Nginxで500がでたので、`/var/log/nginx/error.log``を確かめてみたら以下のエラーが発生。

```
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2019/04/12 18:05:08 [emerg] 13069#0: still could not bind()
```

`80`番ポートへのバインドに失敗しているらしい。
そしてアドレスはすでに使われていると。いろんなプロセスが同じポートを利用している？と仮説をたて、調べてみた。

```
sudo lsof -i:80
```

```
nginx   12395  root    8u  IPv4 544463      0t0  TCP *:http (LISTEN)
nginx   12396 nginx    8u  IPv4 544463      0t0  TCP *:http (LISTEN)
nginx   12397 nginx    8u  IPv4 544463      0t0  TCP *:http (LISTEN)
nginx   12398 nginx    8u  IPv4 544463      0t0  TCP *:http (LISTEN)
nginx   12399 nginx    8u  IPv4 544463      0t0  TCP *:http (LISTEN)
```

nginxはちゃんと走っていました。

一方、nginxのステータスを確認。

```
sudo service nginx status
```

````
nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 金 2019-04-12 18:05:10 JST; 8min ago
  Process: 13069 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 13067 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 13064 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 2388 (code=killed, signal=KILL)

 4月 12 18:05:08 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 4月 12 18:05:08 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 4月 12 18:05:09 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 4月 12 18:05:09 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13069]: nginx: [emerg] still could not bind()
 4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: nginx.service: control process exited, code=exited status=1
 4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
 4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: Unit nginx.service entered failed state.
 4月 12 18:05:10 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: nginx.service failed.
Failed to start The nginx HTTP and reverse proxy server
```


やはり`sudo lsof -i:80`で打ったときにでるプロセスが80番ポートを占領しててバインドに失敗してるんだなと予想。


 ##解決方法: とりあえずプロセスをkill


```
sudo kill 12395 12396 12397 12398 12399
```

そしてnginxを起動！

```
sudo service nginx start

sudo service nginx status
```

```
Redirecting to /bin/systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 金 2019-04-12 18:19:48 JST; 2min 11s ago
  Process: 13243 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 13241 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 13238 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 13245 (nginx)
   CGroup: /system.slice/nginx.service
           ├─13245 nginx: master process /usr/sbin/nginx
           ├─13246 nginx: worker process
           ├─13247 nginx: worker process
           ├─13249 nginx: worker process
           └─13250 nginx: worker process

 4月 12 18:19:48 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: Starting The nginx HTTP and reverse proxy server...
 4月 12 18:19:48 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13241]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
 4月 12 18:19:48 ip-172-31-19-254.ap-northeast-1.compute.internal nginx[13241]: nginx: configuration file /etc/nginx/nginx.conf test is successful
 4月 12 18:19:48 ip-172-31-19-254.ap-northeast-1.compute.internal systemd[1]: Started The nginx HTTP and reverse proxy server.
```

これでエラーが消えてプロセスが走るようになりました！

参考: https://easyramble.com/nginx-emerg-bind-failed.html