Rocky Linux 9.1 安装 Nginx

作者: 温新

分类: 【Linux】

阅读: 2936

时间: 2023-03-09 05:25:41

hi,我是温新,一名 PHPer

我们从存储库安装的 Nginx 并非是最新稳定版本的,那么,本篇文章的目的是安装最新稳定版 Nginx。

第一步:更新系统中所有的软件包

# 更新软件包
sudo dnf upgrade --refresh -y

第二步:删除以前的 Nginx

若没有安装过 nginx,这一步可以省略。

1)备份 nginx 配置文件

# 备份配置文件
sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old

2)停止 nignx

# 停止 nginx
sudo systemctl stop nginx

3)删除 nginx

# 该步骤需要操作
sudo dnf autoremove nginx*

第三步:导入 nginx 库

1)方式一:从官方网站导入

sudo tee /etc/yum.repos.d/nginx-mainline.repo<<EOF

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/9/x86_64/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

EOF

2)方式二:从 Rocky Linux EL9 中导入

sudo tee /etc/yum.repos.d/nginx-stable.repo<<EOF

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/9/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

EOF

从这两种方式中选择一种即可。

第四步:安装 nginx

1)安装 dnf-utils

sudo dnf -y install dnf-utils

2)配置主线版本(可以省略)

# 该版本是最新版本(非稳定),可以不执行该命令
sudo yum-config-manager --enable nginx-mainline

3)安装最新稳定版本 nginx

#
sudo dnf -y install nginx

4)删除主线版本(可以省略)

# 可省略,安装了 3),可以使用它进行删除
sudo yum-config-manager --disable nginx-mainline

主要看 1)3) 步即可。

第五步:配置防火墙

# 允许 HTTP
sudo firewall-cmd --permanent --zone=public --add-service=http
    
# 允许 HTTPS
sudo firewall-cmd --permanent --zone=public --add-service=https
    
# 重载防火墙
sudo firewall-cmd --reload

如此,nginx 已经安装完成

第六步:nginx 相关操作

# 启动 nginx
systemctl start nginx.service
# 停止 nginx
systemctl stop nginx.service
# 平滑启动
systemctl reload nginx.service

第七步:访问 nginx

出现如下信息。

HTTP Server Test Page
This page is used to test the proper operation of an HTTP server after it has been installed on a Rocky Linux system. If you can read this page, it means that the software is working correctly.

输出的信息不同于编译编译 nginx。

请登录后再评论