资讯专栏INFORMATION COLUMN

记一次重装nginx时遇到的问题

Vultr / 2429人阅读

摘要:起因之前在网上看文章提到说,通过的方式安装,可能安装不是最新版本的情况,考虑到在下第一次安装,以后肯定会有卸载重新安装新版本的需求,刚好刚开始学习,索性练习下卸载重装的过程。

起因

之前在网上看文章提到说,通过apt-get的方式安装nginx,可能安装不是最新版本的情况,考虑到在Ubuntu下第一次安装nginx,以后肯定会有卸载重新安装新版本的需求,刚好刚开始学习nginx,索性练习下卸载重装的过程。


安装

使用Ubuntu下的包管理工具apt来安装nginx

</>复制代码

  1. $ sudo apt-get install nginx
卸载

同样使用apt来卸载nginx

</>复制代码

  1. $ sudo apt-get remove nginx

sudo apt-get autoremove命令帮我们卸载不再需要的依赖包
在这里我们卸载nginx相关的依赖包

</>复制代码

  1. $ sudo apt-get autoremove

删除nginx配置文件夹

</>复制代码

  1. $ sudo rm -rf /etc/nginx
重新安装nginx

</>复制代码

  1. # 更新源
  2. $ sudo apt update
  3. $ sudo apt-get install nginx

然而控制台报错:

</>复制代码

  1. Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
  2. invoke-rc.d: initscript nginx, action "start" failed.
  3. dpkg: error processing package nginx-core (--configure):
  4. subprocess installed post-installation script returned error exit status 1
  5. dpkg: dependency problems prevent configuration of nginx:
  6. nginx depends on nginx-core (>= 1.10.3-0ubuntu0.16.04.2) | nginx-full (>= 1.10.3-0ubuntu0.16.04.2) | nginx-light (>= 1.10.3-0ubuntu0.16.04.2) | nginx-extras (>= 1.10.3-0ubuntu0.16.04.2); however:
  7. Package nginx-core is not configured yet.
  8. Package nginx-full is not installed.
  9. Package nginx-light is not installed.
  10. Package nginx-extras is not installed.
  11. nginx depends on nginx-core (<< 1.10.3-0ubuntu0.16.04.2.1~) | nginx-full (<< 1.10.3-0ubuntu0.16.04.2.1~) | nginx-light (<< 1.10.3-0ubuntu0.16.04.2.1~) | nginx-extras (<< 1.10.3-0ubuntu0.16.04.2.1~); however:
  12. Package nginx-core is not configured yet.
  13. No apport report written because the error message indicates its a followup error from a previous failure.
  14. Package nginx-full is not installed.
  15. Package nginx-light is not installed.
  16. Package nginx-extras is not installed.
  17. dpkg: error processing package nginx (--configure):
  18. dependency problems - leaving unconfigured
  19. Processing triggers for libc-bin (2.23-0ubuntu9) ...
  20. Errors were encountered while processing:
  21. nginx-core
  22. nginx
  23. E: Sub-process /usr/bin/dpkg returned an error code (1)

查看nginx.service状态

</>复制代码

  1. $ systemctl status nginx.service

</>复制代码

  1. ● nginx.service - A high performance web server and a reverse proxy server
  2. Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
  3. Active: failed (Result: exit-code) since Sat 2018-03-17 23:16:40 CST; 1min 18s ago
  4. Process: 26795 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  5. Main PID: 18259 (code=exited, status=0/SUCCESS)
  6. Mar 17 23:16:40 VM-0-5-ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
  7. Mar 17 23:16:40 VM-0-5-ubuntu nginx[26795]: nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or direMar 17 23:16:40 VM-0-5-ubuntu nginx[26795]: nginx: configuration file /etc/nginx/nginx.conf test failed
  8. Mar 17 23:16:40 VM-0-5-ubuntu systemd[1]: nginx.service: Control process exited, code=exited status=1
  9. Mar 17 23:16:40 VM-0-5-ubuntu systemd[1]: Failed to start A high performance web server and a reverse proxy server.
  10. Mar 17 23:16:40 VM-0-5-ubuntu systemd[1]: nginx.service: Unit entered failed state.
  11. Mar 17 23:16:40 VM-0-5-ubuntu systemd[1]: nginx.service: Failed with result "exit-code".

仔细看报错信息,获取到两个信息点:

Package nginx-core is not configured yet.

Package nginx-full/nginx-light/nginx-extras is not installed.

于是上网搜,网上找说可能是apache占用了80端口导致报错

查看端口占用情况

</>复制代码

  1. $ sudo netstat -nlp

</>复制代码

  1. Active Internet connections (only servers)
  2. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  3. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1287/sshd
  4. udp 0 0 0.0.0.0:68 0.0.0.0:* 891/dhclient
  5. udp 0 0 172.21.0.5:123 0.0.0.0:* 1231/ntpd
  6. udp 0 0 127.0.0.1:123 0.0.0.0:* 1231/ntpd
  7. udp 0 0 0.0.0.0:123 0.0.0.0:* 1231/ntpd
  8. udp6 0 0 :::123 :::* 1231/ntpd

发现80端口没被占用,而且systemctl status nginx.service的提示信息也没提及80端口被占用,如果提示信息中说80端口被占用,可以执行以下两步试试:

终止apache运行

</>复制代码

  1. $ sudo service apache2 stop

重新安装nginx

</>复制代码

  1. $ sudo apt-get install nginx

</>复制代码

  1. 了解详细信息请看askubuntu上的提问

上述情况与我的并不相符,于是换个思路,报错信息中提及nginx的配置文件不存在,思考为什么卸载重装nginx,却没有生成配置文件?
和第一次安装的时候不一样,然后百度上搜nginx卸载重装后配置文件没有重新生成,找到了类似的问题,给出了如下的操纵步骤:

卸载nginx不保留配置文件

</>复制代码

  1. $ sudo apt-get --purge remove nginx
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. The following packages were automatically installed and are no longer required:
  6. fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4
  7. libxslt1.1 nginx-common nginx-core
  8. Use "sudo apt autoremove" to remove them.
  9. The following packages will be REMOVED:
  10. nginx*
  11. 0 upgraded, 0 newly installed, 1 to remove and 205 not upgraded.
  12. 2 not fully installed or removed.
  13. After this operation, 37.9 kB disk space will be freed.
  14. Do you want to continue? [Y/n] y
  15. (Reading database ... 66386 files and directories currently installed.)
  16. Removing nginx (1.10.3-0ubuntu0.16.04.2) ...
  17. Setting up nginx-core (1.10.3-0ubuntu0.16.04.2) ...
  18. Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
  19. invoke-rc.d: initscript nginx, action "start" failed.
  20. dpkg: error processing package nginx-core (--configure):
  21. subprocess installed post-installation script returned error exit status 1
  22. Errors were encountered while processing:
  23. nginx-core
  24. E: Sub-process /usr/bin/dpkg returned an error code (1)

卸载自动安装且不再需要的依赖包

</>复制代码

  1. $ sudo apt-get autoremove
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. The following packages will be REMOVED:
  6. fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4
  7. libxslt1.1 nginx-common nginx-core
  8. 0 upgraded, 0 newly installed, 13 to remove and 205 not upgraded.
  9. 1 not fully installed or removed.
  10. After this operation, 9,745 kB disk space will be freed.
  11. Do you want to continue? [Y/n] y
  12. (Reading database ... 66383 files and directories currently installed.)
  13. Removing nginx-core (1.10.3-0ubuntu0.16.04.2) ...
  14. Removing libgd3:amd64 (2.1.1-4ubuntu0.16.04.8) ...
  15. Removing libfontconfig1:amd64 (2.11.94-0ubuntu1.1) ...
  16. Removing fontconfig-config (2.11.94-0ubuntu1.1) ...
  17. Removing fonts-dejavu-core (2.35-1) ...
  18. Removing libtiff5:amd64 (4.0.6-1ubuntu0.2) ...
  19. Removing libjbig0:amd64 (2.1-3.1) ...
  20. Removing libjpeg8:amd64 (8c-2ubuntu8) ...
  21. Removing libjpeg-turbo8:amd64 (1.4.2-0ubuntu3) ...
  22. Removing libvpx3:amd64 (1.5.0-2ubuntu1) ...
  23. Removing libxpm4:amd64 (1:3.5.11-1ubuntu0.16.04.1) ...
  24. Removing libxslt1.1:amd64 (1.1.28-2.1ubuntu0.1) ...
  25. Removing nginx-common (1.10.3-0ubuntu0.16.04.2) ...
  26. Processing triggers for libc-bin (2.23-0ubuntu9) ...
  27. Processing triggers for man-db (2.7.5-1) ...

筛选已安装软件包中与nginx有关的

</>复制代码

  1. $ dpkg --get-selections | grep nginx
  2. nginx-common deinstall

卸载nginx-common不保留配置文件

</>复制代码

  1. $ sudo apt-get --purge remove nginx-common
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. The following packages will be REMOVED:
  6. nginx-common*
  7. 0 upgraded, 0 newly installed, 1 to remove and 205 not upgraded.
  8. After this operation, 0 B of additional disk space will be used.
  9. Do you want to continue? [Y/n] y
  10. (Reading database ... 66243 files and directories currently installed.)
  11. Removing nginx-common (1.10.3-0ubuntu0.16.04.2) ...
  12. Purging configuration files for nginx-common (1.10.3-0ubuntu0.16.04.2) ...
  13. dpkg: warning: while removing nginx-common, directory "/var/www/html" not empty so not removed

重新安装nginx

</>复制代码

  1. $ sudo apt-get install nginx
  2. ...

查看版本号,执行nginx配置文件语法检测

</>复制代码

  1. $ nginx -v
  2. nginx version: nginx/1.10.3 (Ubuntu)
  3. $ sudo nginx -t
  4. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  5. nginx: configuration file /etc/nginx/nginx.conf test is successful

虽然nginx已经重装好了,但是一路下来还有几个困惑:

apt-get autoremove到底是按什么规则卸载软件?

为什么nginx-common没有在一开始的时候卸载?

1. apt-get autoremove到底是按什么规则卸载软件?

apt-get autoremove:删除,自动安装的,且不再需要的
(不被其他软件当作依赖的)软件包

</>复制代码

  1. 举个栗子: 通过apt-get方式安装nginx时,会通过引导自动安装所需的依赖包(nginx-core),而当我们卸载nginx后,
    那些自动安装的依赖包就成为不再需要的软件包(nginx-core),通过apt-get autoremove会自动清理它们

既然会自动删除不再需要的软件包,那么为什么nginx-common没被删除?

2. 为什么nginx-common没有在apt-get autoremove的时候卸载?

不知道细心的童鞋发现没,我们在卸载nginx的时候,
手动删除了nginx的配置文件夹(sudo rm -rf /etc/nginx)。
那这些配置文件夹和nginx-common软件包有什么关系呢?

通过查找ubuntu packages,找到了Ubuntu16.0.4下nginx-common软件包的文件清单。

按照清单上的目录手动排查,发现本机上除了手动删除的/etc/nginx目录不存在,文件清单内的其他文件都没有被删除,
猜测可能是nginx-common软件包完整性被破坏,导致autoremove的时候没有被删除,当然这只我的猜测。

问题回答后续补充:

整理完全卸载nginx命令时,发现执行如下命令,ngnix的配置文件并没有被删除

</>复制代码

  1. $ sudo apt-get remove --purge nginx

查阅ubuntu packages发现,nginx依赖nginx-core,nginx-core依赖nginx-common,且其中nginx的配置文件属于nginx-common软件包配置文件的一部分。

执行如下命令进一步验证

</>复制代码

  1. # 卸载nginx-common ,nginx配置文件不会被删除
  2. $ sudo apt-get remove nginx-common
  3. ...
  4. # 卸载nginx-common,nginx配置文件已经被删除(包括但不限于)
  5. $ sudo apt-get remove --purge nginx-common
  6. $ find /etc/nginx/
  7. find: ‘/etc/nginx/’: No such file or directory
推翻之前的猜测:

apt-get autoremove执行时,nginx-common软件包已经被选择用于卸载( deinstall ),但是实际还没有卸载。

</>复制代码

  1. $ dpkg --get-selections| grep nginx
  2. nginx-common deinstall

了解更多关于deinstall,点击这里,如果有知道为什么的同学请不吝赐教。


命令汇总

</>复制代码

  1. # 完全卸载nginx
  2. $ sudo apt-get remove --purge nginx
  3. $ sudo apt-get autoremove --purge
  4. # 更新nginx,保留配置文件
  5. # 亲测nginx.conf不会被删除和覆盖,但保险起见还是建议先备份
  6. $ sudo apt-get remove nginx
  7. $ sudo apt-get autoremove
  8. $ sudo apt update
  9. $ sudo apt-get install nginx
  10. # 安装软件包
  11. # sudo apt-get install 软件包名称`
  12. # eg:
  13. $ sudo apt-get install nginx
  14. # 卸载软件包
  15. # sudo apt-get remove 软件包名称
  16. # eg:
  17. $ sudo apt-get remove nginx
  18. # 卸载软件包且不保留配置文件
  19. # sudo apt-get remove --purge 软件包名称
  20. # eg:
  21. $ sudo apt-get remove --purge nginx
  22. # 卸载自动安装的软件包且不保留配置文件
  23. # sudo apt-get autoremove --purge
  24. # 列出本地软件包列表
  25. # dpkg --get-selections [| grep 筛选关键字]
  26. # eg:(列出本地所有软件包)
  27. $ dpkg --get-selections
  28. # eg:(列出本地与ngnix有关的软件包)
  29. $ dpkg --get-selections | grep nginx
  30. # 查看进程信息
  31. # ps -ef [| grep 筛选关键字]
  32. # eg:(列出所有进程信息)
  33. $ ps -ef
  34. # eg:(列出与nginx有关的进程信息)
  35. $ ps -ef | grep nginx
  36. # 查看端口占用信息
  37. $ sudo netstat -nlp

参考链接汇总

</>复制代码

  1. Package nginx-core not configured yet.Sub-process :/user/bin/dpkg returned an error code(1)

    nginx fail to install on Ubuntu 15.10 server

  2. Ubuntu 14.04上卸载nginx之后重新安装没有重新生成配置文件的解决方法

  3. 在 xenial 发行版中 all 硬件架构下的 nginx-common 软件包文件清单

  4. What is difference between the options “autoclean”, “autoremove” and “clean”?

  5. dpkg --get-selections shows packages marked “deinstall”

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39849.html

相关文章

  • 【踩坑记录】一次MySQL主从复制延迟

    摘要:最近开发中遇到的一个主从延迟的坑,记录并总结,避免再次犯同样的错误。运行时查询为空,执行完毕后查询时内容存在,初步怀疑是主从延迟问题。报错只是部分失败,确定是主从延迟的问题。接下来,会去学习主从复制的原理,敬请期待。 最近开发中遇到的一个MySQL主从延迟的坑,记录并总结,避免再次犯同样的错误。 情景 一个活动信息需要审批,审批之后才能生效。因为之后活动要编辑,编辑后也可能触发审批,审...

    cartoon 评论0 收藏0

发表评论

0条评论

Vultr

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<