本文以nginx为例,介绍Linux中软件的安装与卸载方法。
一、rpm安装包介绍与管理
(一)rpm的优点
- 安装软件非常方便。
- 配套资源丰富。
- 支持软件包内容校验。
- 支持多种硬件。
(二)rpm的功能
- 安装:将软件安装到操作系统。
- 卸载:将软件移除。
- 升级:更新软件的新版本。
- 查询:查询软件包的信息。
- 验证:验证操作系统中的软件是否为RPM包中的软件。
(三)rpm的用法
查询所有RPM包的个数
1
[root@centos7-test ~]# rpm -qa | wc -l
示例输出:
1
444
查询是否安装了nginx软件
1
[root@centos7-test ~]# rpm -qa | grep nginx
下载nginx
- 下载地址:http://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/nginx-1.18.0-1.el7.x86_64.rpm
- 使用
wget
下载:1
[root@centos7-test ~]# wget http://rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/nginx-1.18.0-1.el7.x86_64.rpm
安装nginx
1
[root@centos7-test ~]# rpm -ivh nginx-1.18.0-1.el7.x86_64.rpm
- 选项说明:
-i
:安装-v
:显示详细信息-h
:显示安装进度
- 选项说明:
查询安装的nginx软件
1
[root@centos7-test ~]# rpm -qa | grep nginx
示例输出:
1
nginx-1.18.0-1.el7.x86_64
查询RPM包安装到操作系统的所有文件
1
[root@centos7-test ~]# rpm -ql nginx
示例输出:
1 2 3 4 5
/etc/nginx /etc/nginx/conf.d /etc/nginx/nginx.conf /usr/sbin/nginx ...
卸载nginx
1
[root@centos7-test ~]# rpm -e nginx
二、yum安装管理与介绍
(一)yum简介
Yum 是基于 RPM 的软件包管理工具,它提供了 RPM 的仓库,并自动维护 RPM 之间的依赖关系。Yum 可以自动下载 RPM 包及其依赖项,资源丰富,主流 Linux 厂商均支持 Yum。
(二)yum安装nginx
列出镜像源
1
[root@centos7-test ~]# yum repolist
查询RPM包
1 2
[root@centos7-test ~]# yum search nginx [root@centos7-test ~]# yum list | grep nginx
安装RPM包
1
[root@centos7-test ~]# yum -y install nginx.x86_64
卸载RPM包
1
[root@centos7-test ~]# yum remove nginx
清除残留文件和配置
1
[root@centos7-test ~]# sudo yum autoremove
清除缓存
1
[root@centos7-test ~]# sudo yum clean all
查看yum历史记录
1 2
[root@centos7-test ~]# yum history [root@centos7-test ~]# yum history info [id]
三、编译安装与卸载
(一)编译安装依赖
在编译安装nginx之前,需要安装一些必要的开发工具和库:
|
|
(二)下载与解压源码
下载nginx源码
1
[root@centos7-test ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
解压源码
1
[root@centos7-test ~]# tar -zxvf nginx-1.18.0.tar.gz
(三)编译安装步骤
进入解压后的目录
1
[root@centos7-test ~]# cd nginx-1.18.0
配置
1
[root@centos7-test nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
--prefix
:指定安装目录--with-http_ssl_module
:启用SSL模块
编译
1
[root@centos7-test nginx-1.18.0]# make
安装
1
[root@centos7-test nginx-1.18.0]# make install
(四)服务管理
添加启动脚本
- 创建
/etc/systemd/system/nginx.service
文件:1
[root@centos7-test ~]# vim /etc/systemd/system/nginx.service
- 内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target
- 内容如下:
- 创建
启动及管理服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 启动nginx服务 [root@centos7-test ~]# systemctl start nginx # 查看nginx状态 [root@centos7-test ~]# systemctl status nginx # 设置开机自启 [root@centos7-test ~]# systemctl enable nginx # 停止服务 [root@centos7-test ~]# systemctl stop nginx # 重新加载配置 [root@centos7-test ~]# systemctl reload nginx
卸载编译安装的nginx
1 2 3 4 5 6 7 8 9 10 11
# 停止服务 [root@centos7-test ~]# systemctl stop nginx # 删除服务文件 [root@centos7-test ~]# rm -f /etc/systemd/system/nginx.service # 重新加载systemd配置 [root@centos7-test ~]# systemctl daemon-reload # 删除安装目录 [root@centos7-test ~]# rm -rf /usr/local/nginx
通过以上步骤,你可以通过RPM、YUM或编译安装的方式安装和卸载nginx软件。每种方式各有优缺点,可以根据实际需求选择合适的安装与卸载方法。