在 CentOS 7 部署 vue 项目时,添加网站配置后重启出现如下错误:

[root@iZbp12wuri www]# service nginx restart
Redirecting to /bin/systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

检查 nginx 配置文件也没有错误

[root@iZbp12wuri www]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

按照错误信息中的提示执行 systemctl status nginx.service 详情错误信息如下:

[root@iZbp12wuri www]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2020-09-02 16:07:58 CST; 25s ago
  Process: 24192 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 24189 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 24187 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 26912 (code=exited, status=0/SUCCESS)

Sep 02 16:07:56 iZbp12wuri nginx[24192]: nginx: [emerg] bind() to 0.0.0.0:8080 failed (9...se)
(9...se)
Sep 02 16:07:58 iZbp12wuri  nginx[24192]: nginx: [emerg] still could not bind()
Sep 02 16:07:58 iZbp12wuri systemd[1]: nginx.service: control process exited, code=exite...s=1
Sep 02 16:07:58 iZbp12wuri systemd[1]: Failed to start The nginx HTTP and reverse proxy ...er.
Sep 02 16:07:58 iZbp12wuri systemd[1]: Unit nginx.service entered failed state.
Sep 02 16:07:58 iZbp12wuri systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

关键错误信息 nginx: [emerg] bind() to 0.0.0.0:8080 failed 可以猜测应该是端口被占用了。

查看 8080 端口是否被占用,发现 8080 端口被 node 进行占用。

[root@iZbp12wuri www]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name          
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      31111/node               
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1355/sshd           

查看进程的详情信息

[root@iZbp12wuri www]# ps -ef | grep node
root     24590 22541  0 16:12 pts/3    00:00:00 grep --color=auto node
root     31111 31092  0 11:48 pts/0    00:00:22 node /opt/www/node_modules/.bin/vue-cli-service serve

由于之前执行 'npm run dev' 进 xshell 非正常退出,导致 vue serve 一直在运行。nginx 使用的端口与 vue 开发时使用的端口一样。

结束占用的进程,重新启动 nginx ,问题解决。

kill -9 31111
service nginx restart

nginx 出现如下关键错误信息,基本上是 nginx 运行的端口号被占用了。
nginx: [emerg] bind() to 0.0.0.0:8080 failed
nginx: [emerg] still could not bind()
Failed to start The nginx HTTP and reverse proxy ...er.

标签: CentOS, Nginx, Vue