快轉到主要內容
  1. Posts/

徹底解決 Nginx 安裝與卸載問題

Linux Nginx Nginx Ubuntu Server Web Server
目錄

前言
#

一開始,我以為安裝和卸載任何套件的方法都很簡單,只需使用以下命令:

sudo apt-get install <package>
sudo apt-get remove <package>

然而,當我遇到 Nginx 時,才發現這種方式並不完全適用,特別是在處理某些依賴套件和配置檔案時。

1. 安裝 Nginx
#

首先,使用 Ubuntu 的套件管理工具安裝 Nginx:

sudo apt-get install nginx

2. 卸載 Nginx
#

要卸載 Nginx,可以使用 remove 命令:

sudo apt-get remove nginx

接著,使用 autoremove 移除不再需要的依賴套件:

sudo apt-get autoremove

若要完全刪除 Nginx 的配置文件,則可使用以下命令:

sudo rm -rf /etc/nginx

3. 重新安裝 Nginx
#

在卸載後,執行以下命令更新套件資料庫並重新安裝 Nginx:

sudo apt-get update
sudo apt-get install nginx

這時可能會報錯,顯示如下信息:

dpkg: error processing package nginx (--configure):
 dependency problems - leaving unconfigured
Processing triggers for libc-bin (2.23-0ubuntu9) ...
Errors were encountered while processing:
 nginx-core
 nginx

解決方案
#

經過查詢,發現可以使用 –purge remove 完全移除 Nginx 及其相關的依賴套件:

sudo apt-get --purge remove nginx

接著,重新使用 autoremove 清理不需要的套件:

sudo apt-get autoremove

接下來,查看是否還有與 Nginx 相關的套件:

dpkg --get-selections | grep nginx

如果仍有與 Nginx 相關的套件,使用 –purge remove 命令將它們完全移除:

sudo apt-get --purge remove <package_name>

重新安裝 Nginx
#

完成上述步驟後,重新安裝 Nginx:

sudo apt-get install nginx

檢查安裝版本:

nginx -v

最後,檢查 Nginx 配置文件的語法:

sudo nginx -t

如果顯示如下內容,表示配置文件語法正確:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

這樣就成功清理並重新安裝了 Nginx。

Ernie
作者
Ernie

相關文章

使用 Supervisor 管理 Linux 服務
Linux Python Supervisor Flask Gunicorn Linux Python
學習如何在Linux環境下使用Supervisor管理Flask+Gunicorn應用程式。
有效清理Ubuntu系統的安裝套件
Ubuntu Linux Ubuntu 套件管理 系統清理 磁碟空間
學習如何使用apt-get clean, apt-get autoclean, apt-get remove, 和 apt-get autoremove 命令清理和移除Ubuntu系統中不需要的套件,釋放磁碟空間。
重新安裝Nginx會遇到的問題
Linux Linux Nginx
sudo apt-get remove清不乾淨?