Linux 搭建私有yum源仓库

发布于:2024-04-18 ⋅ 阅读:(177) ⋅ 点赞:(0)

一、环境准备

IP 系统版本 作用
192.168.140.155 CentOS 7.9.2009 yum源仓库
192.168.140.153 CentOS 7.9.2009 测试

        准备两台服务器,一台作为yum源仓库,另一台作为测试使用。

二、搭建yum源服务器

(无法连接外网的情况,需要去官网下载镜像,拷贝到服务器)

1、下载CentOS 7.9.2009的完整版镜像

centos镜像_centos下载地址_centos安装教程-阿里巴巴开源镜像站阿里巴巴开源镜像站为您提供免费的centos下载地址及centos安装教程,centos镜像简介:CentOS,是基于Red Hat Linux提供的可自由使用源代码的企业级Linux发行版本,是RHEL(Red Hat Enterprise Linux)源代码再编译的产物,是一个免费的企业级计算平台;CentOS 定期发行及更新版本,以便支持新的硬件,从而建立一个安全、低维护、稳定、高预测性、高重复性的Linux环境。阿里巴巴开源镜像站,持续同步CentOS、CentOC-altarch等最新版镜像文件,为用户提供极致的下载体验。阿里巴巴开源镜像站icon-default.png?t=N7T8https://developer.aliyun.com/mirror/centos?spm=a2c6h.13651102.0.0.3e221b11XL26Tr注:下载everything版本

2、将下载的镜像上传到yum源仓库服务器

3、挂载镜像
# 1、查看磁盘空间是否足够(可用空间必须大于镜像大小)
[root@localhost final]# df -h

# 2、创建目录,用于挂载镜像
[root@localhost final]# mkdir -p /mnt/centos7

# 3、挂载镜像
# -t iso9660 挂载的文件系统类型为iso9660
# -o loop 使用回环设备的方式挂载文件,允许将文件当作块设备处理。因为iso是一个文件,不是物理设备
[root@localhost final]# mount -t iso9660 -o loop /home/final/CentOS-7-x86_64-Everything-2009.iso /mnt/centos7/

# 4、设置开机自启动挂载
[root@localhost final]# vi /etc/fstab
# 添加下面行
/home/final/CentOS-7-x86_64-Everything-2009.iso /mnt/centos7                       iso9660     defaults,loop,ro        0 0
4、本地yum配置
# 1、进入yum配置文件目录
[root@localhost final]# cd /etc/yum.repo.d/

# 2、创建备份目录
[root@localhost final]# mkdir repo_bak

# 3、备份
[root@localhost final]# mv *.repo repo_bak/

# 4、新建yum文件
[root@localhost final]# vi localyum.repo
[localyum]
name=localyum
baseurl=file:///mnt/centos7        # 镜像挂载路径
gpgcheck=0
enabled=1

# 5、清除原有缓存,生成新缓存
[root@localhost final]# yum clean all
[root@localhost final]# yum makecache
# 查看当前使用的镜像列表
[root@localhost final]# yum repolist
5、安装工具
# 1、安装同步工具
[root@localhost final]# yum -y install yum-utils

# 2、安装repo制作工具
[root@localhost final]# yum -y install createrepo
6、制作base仓库和other仓库

other仓库用来存放base仓库中缺少的rpm包,可以通过其它方式获取

# 1、创建base仓库目录
[root@localhost final]# mkdir -p /mnt/repo/centos/7/base/

# 2、复制源文件到base目录下
[root@localhost final]# cp -rf /mnt/centos7/* /mnt/repo/centos/7/base/

# 3、创建repo数据
[root@localhost final]# createrepo /mnt/repo/centos/7/base/

# 4、创建other仓库目录
[root@localhost final]# mkdir -p /mnt/repo/centos/7/other/
7、搭建web服务器(这里选择Nginx)
7.1 下载nginx二进制安装包

http://nginx.org/download/nginx-1.18.0.tar.gzicon-default.png?t=N7T8http://nginx.org/download/nginx-1.18.0.tar.gz

7.2 上传安装包到服务器
7.3 编译安装
# 1、解压压缩包
[root@localhost final]# tar -zxvf nginx-1.18.0.tar.gz

# 2、进入解压目录
[root@localhost final]# cd nginx-1.18.0

# 3、使用默认配置
[root@localhost final]# ./configure

# 4、编译安装
[root@localhost final]# make && make install

# 5、查看
[root@localhost final]# whereis nginx
nginx: /usr/local/nginx
7.4 配置启动脚本
[root@localhost final]# vi /etc/init.d/nginx

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  https://lnmp.org

NGINX_BIN='/usr/local/nginx/sbin/nginx'
CONFIG='/usr/local/nginx/conf/nginx.conf'

case "$1" in
    start)
        echo -n "Starting nginx... "

        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            echo "nginx (pid $PID) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIG

        if [ "$?" != 0 ]; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping nginx... "

        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" = "" ]; then
            echo "nginx is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            $0 force-quit
        else
            echo " done"
        fi
        ;;

    status)
        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            echo "nginx (pid $PID) is running..."
        else
            echo "nginx is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "Terminating nginx... "

        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" = "" ]; then
            echo "nginx is is stopped."
            exit 1
        fi

        kill $PID

        if [ "$?" != 0 ]; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload nginx... "

        PID=$(ps -ef | grep "$NGINX_BIN" | grep -v grep | awk '{print $2}')
        if [ "$PID" != "" ]; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "nginx is not running, can't reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test nginx configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;

esac

修改文件权限

# 1、修改权限
chmod +x /etc/init.d/nginx

# 2、启动
/etc/init.d/nginx start

# 3、查看运行状态
/etc/init.d/nginx status

# 4、设置开机自启动
chkconfig nginx on
7.5 访问测试

在测试服务器使用curl命令测试nginx服务

[root@localhost nginx]# curl http://192.168.140.155
8、修改Nginx配置
# 修改location模块
​[root@localhost nginx]# vi /usr/local/nginx/conf/nginx.conf
        location / {
#            root   html;
            root   /mnt/repo;
            index  index.html index.htm;
            autoindex on;        自动列出目录文件,允许下载目录下文件
        }

9、重新启动Nginx
[root@localhost final]# /etc/init.d/nginx restart
10、访问nginx服务

三、测试服务器 私有YUM源配置

1、在测试服务器配置yum文件
# 1、进入yum配置文件目录
[root@localhost final]# cd /etc/yum.repos.d/

# 2、创建备份目录
[root@localhost yum.repos.d]# mkdir repo_bak

# 3、备份
[root@localhost yum.repos.d]# mv *.repo repo_bak/

# 4、创建yum配置文件
[root@localhost yum.repos.d]# vi local.repo
# 添加如下内容
[base]
name=localbase
baseurl=http://192.168.140.155/centos/7/base
enables=1
gpgcheck=0

[other]
name=localother
baseurl=http://192.168.140.155/centos/7/other
enables=1
gpgcheck=0

# 5、清除原有缓存,生成新缓存
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
# 查看当前使用的镜像列表
[root@localhost yum.repos.d]# yum repolist
2、测试

在开源镜象网站下载nginx,放入other库中,在测试库安装,测试是否能安装成功(因为CentOS7中Base库默认没有Nginx,所以通过Nginx做测试)

# 在YUM源仓库所在服务器执行
# 1、下载
wget https://mirrors.aliyun.com/centos/7.9.2009/infra/x86_64/infra-common/Packages/n/nginx-mod-mail-1.12.2-2.el7.x86_64.rpm

# 2、存放到other目录下
mv nginx-1.12.2-2.el7.x86_64.rpm /mnt/repo/centos/7/other

# 3、更新repo数据
createrepo /mnt/repo/centos/7/other
# 在测试服务器执行
# 1、清理缓存
yum clean all

# 2、生成新缓存
yum makecache

# 3、查看是否存在上传的nginx
yum list | grep nginx
nginx.x86_64                          1:1.12.2-2.el7                   base

# 3、安装nginx
yum -y install nginx

如果报以下错误,是因为缺少依赖,需要去官网下载指定的rpm包,跟据上面步骤放到other目录下

Error: Package: 1:nginx-1.12.2-2.el7.x86_64 (other)

四、rpm包获取方式

1、红帽官网

Red Hat Customer Portal - Access to 24x7 support and knowledgeAccess Red Hat's knowledge, guidance, and support through your subscription.icon-default.png?t=N7T8https://access.redhat.com

2、开源镜像网站

阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区阿里巴巴开源镜像站,免费提供Linux镜像下载服务,拥有Ubuntu、CentOS、Deepin、MongoDB、Apache、Maven、Composer等多种开源软件镜像源,此外还提供域名解析DNS、网络授时NTP等服务,致力于为互联网用户提供全面,高效和稳定的基础服务。icon-default.png?t=N7T8https://developer.aliyun.com/mirror

3、第三方下载网站

Rpmfind mirroricon-default.png?t=N7T8https://rpmfind.net


网站公告

今日签到

点亮在社区的每一天
去签到