Step5—Nginx安装
# 使用 Nginx 搭建本地 YUM 仓库 🌐📦
在 Linux 服务器环境中,我们可以使用 Nginx 作为本地 YUM 仓库的 Web 服务器,提供 RPM 软件包的访问。相比于传统的 httpd
(Apache),Nginx 更轻量、更高效,并且默认支持目录索引,适合作为 静态文件服务器,提供 RPM 包下载。
# 1. Nginx 作为 YUM 仓库的优势 ✅
Nginx 作为 Web 服务器提供 YUM 仓库文件访问,相比 httpd(Apache)具备以下优势:
- 更轻量级 🚀:适用于高性能场景,占用资源更少。
- 默认支持目录索引 📂:无需额外配置,允许直接访问 RPM 文件。
- 更好的静态文件处理能力 🖥️:适用于 RPM 仓库或其他静态资源托管。
# 2. Nginx 安装 📥
本地 YUM 仓库服务器需要安装 Nginx,作为 Web 服务器提供 RPM 文件的访问。
# 2.1 安装 Nginx
yum install -y epel-release
yum install -y nginx
1
2
2
# 2.2 验证 Nginx 是否安装成功
nginx -v
1
如果输出类似 nginx version: nginx/1.xx.x
,说明安装成功。
# 3. 配置 Nginx 作为 YUM 仓库 🛠️
# 3.1 创建 YUM 资源目录
mkdir -p /data/modules
chmod -R 755 /data/modules
chown -R nginx:nginx /data/modules
1
2
3
2
3
此目录将作为 YUM 仓库的根目录,存放 RPM 包。
# 3.2 配置 Nginx
编辑配置文件:
vim /etc/nginx/conf.d/yum_repo.conf
1
添加以下内容:
server {
listen 80;
server_name _;
location / {
root /data/modules;
index index.html index.htm;
autoindex on; # 启用目录索引,允许浏览 RPM 文件
autoindex_exact_size off;
autoindex_localtime on;
}
error_page 404 /404.html;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
我的文件均放在/data/modules/ 下所有使用上述配置。下面将展示我的本地仓库路径。
# 3.3 禁用默认 Nginx 配置
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
1
# 3.4 配置 SELinux(如启用)
如果 SELinux 处于 Enforcing 模式,需要执行:
chcon -Rt httpd_sys_content_t /data/modules
1
# 4. 启动并测试 Nginx 🚀
# 4.1 启动 Nginx
systemctl restart nginx
systemctl enable nginx
1
2
2
# 4.2 测试 Nginx 是否正常运行
systemctl status nginx
1
如果状态显示 active (running),则表示 Nginx 运行正常。
# 4.3 测试 Nginx 是否提供 YUM 资源
curl -I http://localhost/
1
如果返回 HTTP/1.1 200 OK
,说明 YUM 资源目录已正确配置。