centos 查找nginx_CentOS 通过 yum 安装 Nginx 最新版

查看自带的 yum 源信息

从阿里云购买一台服务器,选择公共镜像 CentOS,登录系统后运行以下命令:

ls /etc/yum.repos.d/

会发现其自带的一些 yum 源配置文件:

CentOS-AppStream.repo CentOS-CR.repo CentOS-Extras.repo CentOS-PowerTools.repo

CentOS-Base.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Sources.repo

CentOS-centosplus.repo CentOS-epel.repo CentOS-Media.repo CentOS-Vault.repo

运行以下命令,查看 CentOS-AppStream.repo 的文件内容:

cat CentOS-AppStream.repo

会发现此 yum 源其实是阿里云自家的:

[AppStream]

name=CentOS-$releasever - AppStream

baseurl=http://mirrors.cloud.aliyuncs.com/$contentdir/$releasever/AppStream/$basearch/os/

gpgcheck=1

enabled=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

接着运行以下命令,查看 yum 上的 Nginx 版本:

yum list nginx

会发现只有一个 1.14 版本的 Nginx:

Available Packages

nginx.x86_64 1:1.14.1-9.module_el8.0.0+184+e34fea82 AppStream

而在 Nginx 官网 查看后会发现最新 Stable 版本已经是 1.18.0,所以我们需要添加 Nginx 官方维护的 yum 源才能使用最新版。

设置 Nginx 官方 yum 源

运行以下命令,安装 yum-utils,此插件可让我们自主选择 yum 源:

yum install yum-utils -y

运行以下命令,新增 Nginx 官方 yum 源的仓库文件:

vi /etc/yum.repos.d/nginx.repo

文件内容如下:

[nginx-stable]

name=nginx stable repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

module_hotfixes=true

[nginx-mainline]

name=nginx mainline repo

baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/

gpgcheck=0

enabled=0

module_hotfixes=true

其中 nginx-stable 即稳定版,nginx-mainline 即最新版,生产环境毫无疑问是选择稳定版,而最新版则适合一些真正的勇士。

运行以下命令,切换 yum 源为 Nginx 稳定版本的 yum 源:

yum-config-manager --enable nginx-stable

安装 Nginx

首先运行以下命令,可查看 yum 上的 Nginx 版本:

yum info nginx

会发现输入以下信息,Nginx 版本为 1.18:

Available Packages

Name : nginx

Epoch : 1

Version : 1.18.0

Release : 2.el8.ngx

Architecture : x86_64

Size : 799 k

Source : nginx-1.18.0-2.el8.ngx.src.rpm

Repository : nginx-stable

Summary : High performance web server

URL : http://nginx.org/

License : 2-clause BSD-like license

Description : nginx [engine x] is an HTTP and reverse proxy server, as well as

: a mail proxy server.

如果已安装旧版本的 Nginx,需要先备份 /etc/nginx/nginx.conf,再执行以下命令卸载:

yum remove nginx

接着运行以下命令,安装 Nginx:

yum install nginx -y

最后运行以下命令,可查看当前 Nginx 的版本:

nginx -v

会发现 1.18.0 版本的 Nginx 已安装完成:

nginx version: nginx/1.18.0

以下是操作 Nginx 的常用命令:

# 查看状态

systemctl status nginx

# 启动

systemctl start nginx

# 停止

systemctl stop nginx

# 重启

systemctl restart nginx

# 平滑重启

systemctl reload nginx

# 查看是否已开机启动

systemctl is-enabled nginx

# 设置开机后自动启动

systemctl enable nginx

# 关闭开机后自动启动

systemctl disable nginx

解决启动报错问题

执行以下命令启动 Nginx 并查看运行状态

systemctl start nginx

systemctl status nginx

会发现报错 No such file or directory:

systemd[1]: Starting The nginx HTTP and reverse proxy server...

systemd[1]: nginx.service: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory

systemd[1]: Started The nginx HTTP and reverse proxy server.

或者报错 Invalid argument:

systemd[1]: Starting The nginx HTTP and reverse proxy server...

systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument

systemd[1]: Started The nginx HTTP and reverse proxy server.

因为 systemd 在 Nginx 启动未完成时去读取 nginx.pid,而 Nginx 启动完成后才会自动生成 nginx.pid,所以 systemd 必然会读取不到 nginx.pid 而直接报错。

执行以下命令,让 systemd 读取 nginx.pid 时等待 0.1 秒即可解决以上报错:

mkdir -p /etc/systemd/system/nginx.service.d

printf "[Service]\nExecStartPost=/bin/sleep 0.1" > /etc/systemd/system/nginx.service.d/override.conf

systemctl daemon-reload

systemctl restart nginx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值