软件包管理

软件包管理是Linux系统的核心功能之一,它负责软件的安装、升级、卸载和依赖管理。不同的Linux发行版使用不同的包管理系统,本章将详细介绍主流的包管理工具。


一、软件包管理概述

1.1 软件包概念

1.1.1 什么是软件包

软件包:包含程序文件、配置文件、元数据的归档文件。

软件包组成

  • 可执行文件
  • 配置文件
  • 文档文件
  • 依赖信息
  • 安装脚本

东巴文理解:软件包就像一个打包好的应用,包含了运行所需的所有文件。

1.1.2 软件包类型

Linux软件包格式

格式 发行版 说明
.deb Debian/Ubuntu Debian包格式
.rpm Red Hat/CentOS/Fedora RPM包格式
.snap 通用 Snap包格式
.flatpak 通用 Flatpak包格式
.AppImage 通用 AppImage格式
源码包 通用 需要编译安装

1.1.3 软件仓库

软件仓库:存储软件包的服务器,提供软件的下载和更新。

仓库类型

  • 官方仓库:发行版官方维护
  • 第三方仓库:社区或厂商维护
  • 本地仓库:本地网络或光盘

东巴文观点:软件仓库是Linux软件生态的核心,确保软件的安全和更新。

1.2 包管理器

包管理器:管理软件包的工具,负责安装、卸载、更新等操作。

主流包管理器

包管理器 发行版 底层工具 高层工具
APT Debian/Ubuntu dpkg apt、apt-get
DNF Fedora/RHEL 8+ rpm dnf
YUM CentOS/RHEL 7 rpm yum
Zypper openSUSE rpm zypper
Pacman Arch Linux - pacman

东巴文理解

  • 底层工具:直接操作软件包文件
  • 高层工具:自动解决依赖关系

二、APT包管理器

2.1 APT基础

2.1.1 APT简介

APT:Advanced Package Tool,Debian及其衍生发行版的包管理器。

APT特点

  • 自动解决依赖关系
  • 支持多个软件源
  • 提供安全的软件更新
  • 支持软件包验证

APT命令

  • apt:新版统一命令(推荐)
  • apt-get:传统命令
  • apt-cache:查询命令

2.1.2 软件源配置

软件源配置文件/etc/apt/sources.list

配置文件格式

deb http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse

配置项说明

字段 说明
deb 二进制包
deb-src 源码包
URL 软件源地址
发行版代号 focal、jammy等
组件 main、restricted、universe、multiverse

组件说明

组件 说明
main 官方支持的自由软件
restricted 官方支持的非自由软件
universe 社区维护的自由软件
multiverse 非自由软件

东巴文提示:国内用户建议使用国内镜像源,如阿里云、清华、中科大等。

2.1.3 更新软件源

# 更新软件源索引
sudo apt update

# 输出示例:
# Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
# Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
# Get:3 http://archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
# Fetched 228 kB in 2s (114 kB/s)
# Reading package lists... Done

# 升级所有软件
sudo apt upgrade

# 完整升级(可能删除旧包)
sudo apt full-upgrade

# 升级指定软件
sudo apt install --only-upgrade nginx

2.2 软件安装

2.2.1 安装软件

# 安装软件
sudo apt install nginx

# 输出示例:
# Reading package lists... Done
# Building dependency tree
# Reading state information... Done
# The following additional packages will be installed:
#   nginx-common nginx-core
# Suggested packages:
#   fcgiwrap nginx-doc
# The following NEW packages will be installed:
#   nginx nginx-common nginx-core
# 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
# Need to get 450 kB of archives.
# After this operation, 1,536 kB of additional disk space will be used.
# Do you want to continue? [Y/n] y

# 安装多个软件
sudo apt install nginx mysql-server php

# 安装指定版本
sudo apt install nginx=1.18.0-0ubuntu1

# 安装但不升级
sudo apt install nginx --no-upgrade

# 重新安装
sudo apt install --reinstall nginx

# 安装开发包
sudo apt install python3-dev

2.2.2 安装本地deb包

# 使用apt安装本地deb包(推荐)
sudo apt install ./package.deb

# 使用dpkg安装
sudo dpkg -i package.deb

# 修复依赖
sudo apt install -f

东巴文最佳实践:使用apt install ./package.deb可以自动解决依赖关系。

2.2.3 下载软件包

# 下载软件包(不安装)
sudo apt download nginx

# 输出示例:
# Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 nginx amd64 1.18.0-0ubuntu1 [3,524 B]
# Fetched 3,524 B in 0s (10.2 kB/s)

# 下载源码包
sudo apt source nginx

# 下载软件包及其依赖
sudo apt-get download $(apt-cache depends nginx | grep Depends | cut -d: -f2)

2.3 软件卸载

2.3.1 卸载软件

# 卸载软件(保留配置文件)
sudo apt remove nginx

# 卸载软件(删除配置文件)
sudo apt purge nginx

# 卸载软件及其依赖
sudo apt autoremove nginx

# 清理不再需要的依赖
sudo apt autoremove

# 清理下载的软件包缓存
sudo apt clean

# 清理过期的软件包缓存
sudo apt autoclean

东巴文理解

  • remove:卸载软件,保留配置文件
  • purge:卸载软件,删除配置文件
  • autoremove:自动清理不再需要的依赖

2.3.2 完全卸载

# 完全卸载nginx
sudo apt purge nginx nginx-common nginx-core

# 清理配置文件
rm -rf /etc/nginx

# 清理残留文件
sudo apt autoremove --purge

2.4 软件查询

2.4.1 查询软件包

# 搜索软件包
apt search nginx

# 输出示例:
# Sorting... Done
# Full Text Search... Done
# nginx/focal-updates,focal-security 1.18.0-0ubuntu1 amd64
#   small, powerful, scalable web/proxy server
#
# nginx-common/focal-updates,focal-security 1.18.0-0ubuntu1 all
#   small, powerful, scalable web/proxy server - common files

# 搜索已安装的软件
apt list --installed | grep nginx

# 查看软件包信息
apt show nginx

# 输出示例:
# Package: nginx
# Version: 1.18.0-0ubuntu1
# Priority: optional
# Section: web
# Origin: Ubuntu
# Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
# Original-Maintainer: Debian Nginx Maintainers <pkg-nginx-maintainers@lists.alioth.debian.org>
# Bugs: https://bugs.launchpad.net/ubuntu/+filebug
# Installed-Size: 44.0 kB
# Depends: nginx-core (<< 1.18.0-0ubuntu1.1~) | nginx-full (<< 1.18.0-0ubuntu1.1~) | nginx-light (<< 1.18.0-0ubuntu1.1~) | nginx-extras (<< 1.18.0-0ubuntu1.1~), nginx-core (>= 1.18.0-0ubuntu1) | nginx-full (>= 1.18.0-0ubuntu1) | nginx-light (>= 1.18.0-0ubuntu1) | nginx-extras (>= 1.18.0-0ubuntu1)
# Download-Size: 3,524 B
# APT-Manual-Installed: yes
# APT-Sources: http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
# Description: small, powerful, scalable web/proxy server
#  Nginx (engine x) is a HTTP server and reverse proxy server.

# 查看软件包依赖
apt depends nginx

# 查看软件包文件列表
apt list --installed

# 查看可升级的软件
apt list --upgradable

2.4.2 查询软件包文件

# 查看软件包安装的文件
dpkg -L nginx

# 输出示例:
# /.
# /etc
# /etc/nginx
# /etc/nginx/nginx.conf
# /etc/nginx/sites-available
# /etc/nginx/sites-enabled
# /usr
# /usr/sbin
# /usr/sbin/nginx

# 查看文件属于哪个软件包
dpkg -S /etc/nginx/nginx.conf

# 输出示例:
# nginx-common: /etc/nginx/nginx.conf

2.5 dpkg命令

2.5.1 dpkg基础

dpkg:Debian包管理器的底层工具。

# 安装deb包
sudo dpkg -i package.deb

# 卸载软件包
sudo dpkg -r package_name

# 完全卸载(包括配置文件)
sudo dpkg -P package_name

# 查看已安装的软件包
dpkg -l

# 输出示例:
# Desired=Unknown/Install/Remove/Purge/Hold
# | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
# |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
# ||/ Name           Version      Architecture Description
# +++-==============-============-============-=================================
# ii  nginx          1.18.0-0ubun amd64        small, powerful, scalable web/pr

# 查看软件包信息
dpkg -s nginx

# 查看deb包信息
dpkg -I package.deb

# 解压deb包
dpkg -x package.deb /tmp/package

2.5.2 dpkg状态

软件包状态

状态 说明
ii 已安装
rc 已删除但配置文件存在
un 未安装
in 已安装但未配置
hn 半安装

东巴文技巧:使用dpkg -l | grep ^rc可以找到已删除但配置文件存在的软件包。

2.6 APT缓存管理

2.6.1 查询缓存

# 查看软件包统计信息
apt-cache stats

# 输出示例:
# Total package names: 65,251
# Total package structures: 65,251
# Normal packages: 65,251
# Pure virtual packages: 1,234
# Single virtual packages: 5,678
# Mixed virtual packages: 890
# Missing: 12,345
# Total distinct versions: 78,901
# Total dependencies: 456,789

# 查看软件包依赖关系
apt-cache depends nginx

# 查看反向依赖
apt-cache rdepends nginx

# 查看软件包策略
apt-cache policy nginx

# 输出示例:
# nginx:
#   Installed: 1.18.0-0ubuntu1
#   Candidate: 1.18.0-0ubuntu1
#   Version table:
#  *** 1.18.0-0ubuntu1 500
#         500 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
#         100 /var/lib/dpkg/status
#      1.17.10-0ubuntu1 500
#         500 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages

2.6.2 清理缓存

# 查看缓存大小
du -sh /var/cache/apt/archives

# 清理所有缓存
sudo apt clean

# 清理过期缓存
sudo apt autoclean

# 查看缓存文件
ls /var/cache/apt/archives/

三、DNF/YUM包管理器

3.1 DNF基础

3.1.1 DNF简介

DNF:Dandified YUM,Fedora和RHEL 8+的默认包管理器。

DNF特点

  • 自动解决依赖关系
  • 支持模块化软件仓库
  • 更快的性能
  • 更好的内存使用

东巴文理解:DNF是YUM的下一代版本,性能更好,功能更强。

3.1.2 软件源配置

软件源配置目录/etc/yum.repos.d/

配置文件格式

[baseos]
name=CentOS Stream $releasever - BaseOS
baseurl=http://mirror.centos.org/centos/$stream/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[appstream]
name=CentOS Stream $releasever - AppStream
baseurl=http://mirror.centos.org/centos/$stream/AppStream/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

配置项说明

配置项 说明
[repo_id] 仓库ID
name 仓库名称
baseurl 软件源URL
enabled 是否启用(1启用,0禁用)
gpgcheck 是否验证GPG签名
gpgkey GPG密钥路径

东巴文提示:国内用户建议使用国内镜像源,如阿里云、清华、华为等。

3.1.3 更新软件源

# 更新软件源缓存
sudo dnf makecache

# 更新所有软件
sudo dnf update

# 输出示例:
# CentOS Stream 9 - BaseOS                        12 MB/s | 7.4 MB     00:00
# CentOS Stream 9 - AppStream                     15 MB/s |  17 MB     00:01
# CentOS Stream 9 - Extras packages               10 MB/s |  15 kB     00:00
# Dependencies resolved.
# Nothing to do.
# Complete!

# 升级指定软件
sudo dnf update nginx

# 安全更新
sudo dnf update --security

3.2 软件安装

3.2.1 安装软件

# 安装软件
sudo dnf install nginx

# 输出示例:
# Last metadata expiration check: 0:01:23 ago on Mon 01 Jan 2024 10:00:00 AM CST.
# Dependencies resolved.
# ================================================================================
#  Package         Architecture   Version               Repository       Size
# ================================================================================
# Installing:
#  nginx           x86_64         1:1.20.1-10.el9       appstream       393 k
# Installing dependencies:
#  nginx-filesystem noarch        1:1.20.1-10.el9       appstream       8.5 k
#
# Transaction Summary
# ================================================================================
# Install  2 Packages
#
# Total download size: 402 k
# Installed size: 1.2 M
# Is this ok [y/N]: y

# 安装多个软件
sudo dnf install nginx mysql-server php

# 安装指定版本
sudo dnf install nginx-1.20.1

# 重新安装
sudo dnf reinstall nginx

# 安装本地rpm包
sudo dnf install ./package.rpm

# 安装开发包
sudo dnf install python3-devel

3.2.2 安装软件组

# 查看软件组
dnf group list

# 输出示例:
# Available Environment Groups:
#    Server with GUI
#    Server
#    Minimal Install
#    Workstation
#    Custom Operating System
# Installed Groups:
#    Development Tools
# Available Groups:
#    Container Management
#    .NET Development
#    Network Servers

# 安装软件组
sudo dnf group install "Development Tools"

# 删除软件组
sudo dnf group remove "Development Tools"

# 查看软件组信息
dnf group info "Development Tools"

3.3 软件卸载

3.3.1 卸载软件

# 卸载软件
sudo dnf remove nginx

# 卸载软件及其依赖
sudo dnf autoremove

# 清理缓存
sudo dnf clean all

# 清理元数据
sudo dnf clean metadata

# 清理软件包缓存
sudo dnf clean packages

3.4 软件查询

3.4.1 查询软件包

# 搜索软件包
dnf search nginx

# 输出示例:
# Last metadata expiration check: 0:05:12 ago on Mon 01 Jan 2024 10:00:00 AM CST.
# ==================== Name Exactly Matched: nginx ====================
# nginx.x86_64 : A high performance web server and reverse proxy server
# ==================== Name & Summary Matched: nginx ====================
# nginx-all-modules.noarch : Meta-package that installs all available Nginx modules
# nginx-filesystem.noarch : The basic directory layout for the Nginx server

# 查看软件包信息
dnf info nginx

# 输出示例:
# Last metadata expiration check: 0:05:30 ago on Mon 01 Jan 2024 10:00:00 AM CST.
# Available Packages
# Name         : nginx
# Epoch        : 1
# Version      : 1.20.1
# Release      : 10.el9
# Architecture : x86_64
# Size         : 393 k
# Source       : nginx-1.20.1-10.el9.src.rpm
# Repository   : appstream
# Summary      : A high performance web server and reverse proxy server
# URL          : https://nginx.org/
# License      : BSD
# Description  : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
#               : IMAP protocols, with a strong focus on high concurrency, performance and low
#               : memory usage.

# 查看已安装的软件
dnf list installed

# 查看可安装的软件
dnf list available

# 查看可更新的软件
dnf list updates

# 查看软件包依赖
dnf repoquery --requires nginx

# 查看软件包文件列表
dnf repoquery -l nginx

3.4.2 查询软件包文件

# 查看文件属于哪个软件包
rpm -qf /etc/nginx/nginx.conf

# 输出示例:
# nginx-1.20.1-10.el9.x86_64

# 查看软件包安装的文件
rpm -ql nginx

# 输出示例:
# /etc/logrotate.d/nginx
# /etc/nginx
# /etc/nginx/nginx.conf
# /usr/lib/systemd/system/nginx.service
# /usr/sbin/nginx

3.5 RPM命令

3.5.1 RPM基础

RPM:RPM Package Manager,Red Hat系发行版的底层包管理工具。

# 安装rpm包
sudo rpm -ivh package.rpm

# 输出示例:
# Verifying...                          ################################# [100%]
# Preparing...                          ################################# [100%]
#    1:package                          ################################# [100%]

# 升级软件包
sudo rpm -Uvh package.rpm

# 卸载软件包
sudo rpm -e package_name

# 查看已安装的软件包
rpm -qa

# 查看软件包信息
rpm -qi nginx

# 查看软件包文件列表
rpm -ql nginx

# 查看文件属于哪个软件包
rpm -qf /etc/nginx/nginx.conf

# 验证软件包
rpm -V nginx

# 查看rpm包信息
rpm -qip package.rpm

# 查看rpm包文件列表
rpm -qlp package.rpm

RPM选项说明

选项 说明
-i 安装
-U 升级
-e 卸载
-q 查询
-v 显示详细信息
-h 显示进度条
-a 所有软件包
-l 文件列表
-f 查询文件所属包

3.6 YUM命令

3.6.1 YUM基础

YUM:Yellowdog Updater Modified,RHEL 7及CentOS 7的默认包管理器。

# 安装软件
sudo yum install nginx

# 卸载软件
sudo yum remove nginx

# 更新软件
sudo yum update

# 搜索软件
yum search nginx

# 查看软件信息
yum info nginx

# 查看已安装的软件
yum list installed

# 清理缓存
sudo yum clean all

# 生成缓存
sudo yum makecache

东巴文提示:YUM命令与DNF命令基本相同,建议在新系统中使用DNF。


四、Snap包管理

4.1 Snap基础

4.1.1 Snap简介

Snap:Ubuntu开发的通用包格式,支持跨发行版安装。

Snap特点

  • 跨发行版支持
  • 自动更新
  • 沙箱隔离
  • 依赖包含在包内

东巴文理解:Snap类似于容器技术,软件运行在隔离环境中。

4.1.2 Snap安装

# 安装snapd(Ubuntu已预装)
sudo apt install snapd

# 启动snapd服务
sudo systemctl enable --now snapd

# 查看snap版本
snap --version

# 输出示例:
# snap    2.58+20.04.1
# snapd   2.58+20.04.1
# series  16
# ubuntu  20.04
# kernel  5.4.0-91-generic

4.2 Snap使用

4.2.1 安装软件

# 搜索软件
snap find nginx

# 输出示例:
# Name        Version  Publisher  Notes  Summary
# nginx       1.21.6   getnginx   -      nginx web server

# 安装软件
sudo snap install nginx

# 输出示例:
# nginx 1.21.6 from GetNginx installed

# 安装指定通道
sudo snap install nginx --channel=stable
sudo snap install nginx --channel=edge
sudo snap install nginx --channel=beta

# 安装经典模式(不使用沙箱)
sudo snap install code --classic

Snap通道

通道 说明
stable 稳定版(默认)
candidate 候选版
beta 测试版
edge 开发版

4.2.2 管理软件

# 查看已安装的软件
snap list

# 输出示例:
# Name    Version   Rev    Tracking       Publisher   Notes
# core18  20211215  2344   latest/stable  canonical✓  base
# nginx   1.21.6    2018   latest/stable  getnginx✓   -

# 查看软件信息
snap info nginx

# 输出示例:
# name:      nginx
# summary:   nginx web server
# publisher: GetNginx
# store-url: https://snapcraft.io/nginx
# contact:   https://nginx.org/
# license:   BSD-2-Clause
# description: |
#   nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server.
# commands:
#   nginx.nginx
#   nginx.nice
# services:
#   nginx: service, enabled, active

# 更新软件
sudo snap refresh nginx

# 更新所有软件
sudo snap refresh

# 切换通道
sudo snap refresh nginx --channel=edge

# 回退软件版本
sudo snap revert nginx

# 卸载软件
sudo snap remove nginx

# 禁用软件
sudo snap disable nginx

# 启用软件
sudo snap enable nginx

4.2.3 Snap服务管理

# 查看服务状态
snap services nginx

# 输出示例:
# Service  Startup  Current  Notes
# nginx    enabled  active   -

# 启动服务
sudo snap start nginx

# 停止服务
sudo snap stop nginx

# 重启服务
sudo snap restart nginx

# 查看日志
snap logs nginx

# 实时查看日志
snap logs nginx -f

五、Flatpak包管理

5.1 Flatpak基础

5.1.1 Flatpak简介

Flatpak:Linux桌面应用的通用包格式。

Flatpak特点

  • 跨发行版支持
  • 沙箱隔离
  • 运行时环境独立
  • 支持多种桌面环境

东巴文理解:Flatpak主要用于桌面应用,提供更好的安全隔离。

5.1.2 Flatpak安装

# 安装flatpak(Ubuntu)
sudo apt install flatpak

# 安装GNOME软件插件
sudo apt install gnome-software-plugin-flatpak

# 添加Flathub仓库
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

# 查看仓库
flatpak remotes

# 输出示例:
# Name    Options
# flathub system

5.2 Flatpak使用

5.2.1 安装软件

# 搜索软件
flatpak search firefox

# 输出示例:
# Name        Description                      Application ID
# Firefox     Fast, Private & Safe Web Browser org.mozilla.firefox

# 安装软件
flatpak install flathub org.mozilla.firefox

# 输出示例:
# Looking for matches…
# Required runtime for org.mozilla.firefox.Locale/x86_64/stable (runtime/org.freedesktop.Platform.Locale/x86_64/21.08) found in remote flathub
# Do you want to install it? [Y/n]: y

# 安装指定版本
flatpak install flathub org.mozilla.firefox//stable

# 从文件安装
flatpak install --from firefox.flatpakref

5.2.2 管理软件

# 查看已安装的软件
flatpak list

# 输出示例:
# Name       Application ID              Version Branch Installation
# Firefox    org.mozilla.firefox         95.0    stable system
# GTK Themes org.gtk.Gtk3theme.Adwaita-dark      3.24   system

# 查看软件信息
flatpak info org.mozilla.firefox

# 输出示例:
# Firefox - Fast, Private & Safe Web Browser
#
#           ID: org.mozilla.firefox
#          Ref: app/org.mozilla.firefox/x86_64/stable
#         Arch: x86_64
#       Branch: stable
#      Version: 95.0
#      License: MPL-2.0
#       Origin: flathub
#   Collection: org.flathub.Stable
# Installation: system
#      Runtime: org.freedesktop.Platform/x86_64/21.08

# 更新软件
flatpak update org.mozilla.firefox

# 更新所有软件
flatpak update

# 卸载软件
flatpak uninstall org.mozilla.firefox

# 卸载未使用的运行时
flatpak uninstall --unused

5.2.3 Flatpak权限管理

# 查看权限
flatpak permissions

# 输出示例:
# Table    Object      App               Permissions
# network  firefox     org.mozilla.firefox network

# 重置权限
flatpak permission-reset org.mozilla.firefox

# 覆盖权限
flatpak override --filesystem=home org.mozilla.firefox

# 查看覆盖
flatpak override --show org.mozilla.firefox

六、源码编译安装

6.1 编译安装概述

6.1.1 为什么编译安装

编译安装的优点

  • 获得最新版本
  • 自定义编译选项
  • 优化性能
  • 学习软件原理

编译安装的缺点

  • 编译时间长
  • 依赖管理复杂
  • 卸载困难
  • 安全更新需要手动

东巴文建议:优先使用包管理器安装,仅在必要时使用源码编译。

6.1.2 编译安装步骤

标准编译安装流程

  1. 下载源码包
  2. 解压源码包
  3. 配置编译选项
  4. 编译
  5. 安装

6.2 编译安装实践

6.2.1 安装编译工具

# Debian/Ubuntu
sudo apt install build-essential

# CentOS/RHEL
sudo dnf group install "Development Tools"

# 查看gcc版本
gcc --version

# 输出示例:
# gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

6.2.2 编译安装Nginx

下载源码

# 下载nginx源码
wget http://nginx.org/download/nginx-1.21.6.tar.gz

# 解压源码
tar -zxvf nginx-1.21.6.tar.gz

# 进入源码目录
cd nginx-1.21.6

配置编译选项

# 安装依赖
sudo apt install libpcre3-dev zlib1g-dev libssl-dev

# 查看配置选项
./configure --help

# 配置
./configure --prefix=/usr/local/nginx \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module \
  --with-http_gzip_static_module

# 输出示例:
# Configuration summary
#   + using system PCRE library
#   + using system OpenSSL library
#   + using system zlib library
#
#   nginx path prefix: "/usr/local/nginx"
#   nginx binary file: "/usr/local/nginx/sbin/nginx"
#   nginx modules path: "/usr/local/nginx/modules"
#   nginx configuration prefix: "/usr/local/nginx/conf"
#   nginx configuration file: "/usr/local/nginx/conf/nginx.conf"

编译和安装

# 编译
make

# 输出示例:
# make -f objs/Makefile
# make[1]: Entering directory '/tmp/nginx-1.21.6'
# cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
#   -o objs/src/core/nginx.o \
#   src/core/nginx.c

# 安装
sudo make install

# 输出示例:
# make -f objs/Makefile install
# make[1]: Entering directory '/tmp/nginx-1.21.6'
# test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
# test -d '/usr/local/nginx/sbin' || mkdir -p '/usr/local/nginx/sbin'

验证安装

# 查看nginx版本
/usr/local/nginx/sbin/nginx -v

# 输出示例:
# nginx version: nginx/1.21.6

# 启动nginx
sudo /usr/local/nginx/sbin/nginx

# 测试配置
sudo /usr/local/nginx/sbin/nginx -t

# 输出示例:
# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

6.2.3 创建systemd服务

创建服务文件

sudo vim /etc/systemd/system/nginx.service

服务文件内容

[Unit]
Description=Nginx HTTP Server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启用服务

# 重载systemd配置
sudo systemctl daemon-reload

# 启用并启动nginx
sudo systemctl enable --now nginx

# 查看状态
systemctl status nginx

6.3 卸载编译安装的软件

6.3.1 使用make uninstall

# 卸载(如果支持)
sudo make uninstall

东巴文提示:并非所有软件都支持make uninstall

6.3.2 手动卸载

# 查看安装的文件
cat install_manifest.txt

# 手动删除文件
sudo xargs rm < install_manifest.txt

# 删除安装目录
sudo rm -rf /usr/local/nginx

七、本章小结

7.1 核心要点

✅ 理解软件包管理的概念 ✅ 掌握APT和DNF/YUM的使用 ✅ 学会使用Snap和Flatpak ✅ 了解源码编译安装流程

7.2 验证清单

完成本章学习后,请确认您能够:

  • 使用apt/dnf安装、卸载、更新软件
  • 配置软件源
  • 查询软件包信息
  • 使用Snap和Flatpak安装应用
  • 编译安装软件

东巴文(db-w.cn) - 让Linux学习更简单