配置阿里源
安装下载工具
首先需要安装wget下载工具
[root@localhost ~]# yum -y install wget
Loaded plugins: fastestmirror
Determining fastest mirrors
centos7 | 3.6 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-15.el7_4.1 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================================================================================
Installing:
wget x86_64 1.14-15.el7_4.1 centos7 547 k
Transaction Summary
======================================================================================================================================================================================================
Install 1 Package
Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : wget-1.14-15.el7_4.1.x86_64 1/1
Verifying : wget-1.14-15.el7_4.1.x86_64 1/1
Installed:
wget.x86_64 0:1.14-15.el7_4.1
Complete!
下载源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2023-10-24 23:25:26-- https://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 110.157.247.207, 110.157.247.202, 110.157.247.209, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|110.157.247.207|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’
100%[============================================================================================================================================================>] 2,523 --.-K/s in 0.002s
2023-10-24 23:25:27 (1.11 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
--2023-10-24 23:25:37-- https://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 110.157.247.209, 110.157.247.203, 110.157.247.202, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|110.157.247.209|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/epel.repo’
100%[============================================================================================================================================================>] 664 --.-K/s in 0s
2023-10-24 23:25:38 (275 MB/s) - ‘/etc/yum.repos.d/epel.repo’ saved [664/664]
安装同步工具
下面的报错不用管它,那是因为网络故障引起的
[root@localhost ~]# yum -y install yum-utils createrepo
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/7): epel/x86_64/group_gz | 99 kB 00:00:00
(2/7): base/7/x86_64/group_gz | 153 kB 00:00:00
(3/7): extras/7/x86_64/primary_db | 250 kB 00:00:00
(4/7): epel/x86_64/updateinfo | 1.0 MB 00:00:02
(5/7): epel/x86_64/primary_db | 7.0 MB 00:00:10
base/7/x86_64/primary_db FAILED 60% [============================================ ] 814 kB/s | 23 MB 00:00:19 ETA
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/6d0c3a488c282fe537794b5946b01e28c7f44db79097bb06826e1c0c88bad5ef-primary.sqlite.bz2: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; Connection refused"
Trying other mirror.
base/7/x86_64/primary_db FAILED 64% [===============================================- ] 780 kB/s | 25 MB 00:00:17 ETA
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/repodata/6d0c3a488c282fe537794b5946b01e28c7f44db79097bb06826e1c0c88bad5ef-primary.sqlite.bz2: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error"
Trying other mirror.
(6/7): base/7/x86_64/primary_db | 6.1 MB 00:00:08
(7/7): updates/7/x86_64/primary_db | 24 MB 00:00:33
Resolving Dependencies
--> Running transaction check
---> Package createrepo.noarch 0:0.9.9-28.el7 will be installed
--> Processing Dependency: python-deltarpm for package: createrepo-0.9.9-28.el7.noarch
--> Processing Dependency: libxml2-python for package: createrepo-0.9.9-28.el7.noarch
--> Processing Dependency: deltarpm for package: createrepo-0.9.9-28.el7.noarch
---> Package yum-utils.noarch 0:1.1.31-54.el7_8 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-54.el7_8.noarch
--> Running transaction check
---> Package deltarpm.x86_64 0:3.6-3.el7 will be installed
---> Package libxml2-python.x86_64 0:2.9.1-6.el7_9.6 will be installed
--> Processing Dependency: libxml2 = 2.9.1-6.el7_9.6 for package: libxml2-python-2.9.1-6.el7_9.6.x86_64
---> Package python-deltarpm.x86_64 0:3.6-3.el7 will be installed
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch
--> Running transaction check
---> Package libxml2.x86_64 0:2.9.1-6.el7_2.3 will be updated
---> Package libxml2.x86_64 0:2.9.1-6.el7_9.6 will be an update
---> Package python-chardet.noarch 0:2.2.1-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================================================================================
Installing:
createrepo noarch 0.9.9-28.el7 base 94 k
yum-utils noarch 1.1.31-54.el7_8 base 122 k
Installing for dependencies:
deltarpm x86_64 3.6-3.el7 base 82 k
libxml2-python x86_64 2.9.1-6.el7_9.6 updates 247 k
python-chardet noarch 2.2.1-3.el7 base 227 k
python-deltarpm x86_64 3.6-3.el7 base 31 k
python-kitchen noarch 1.1.1-5.el7 base 267 k
Updating for dependencies:
libxml2 x86_64 2.9.1-6.el7_9.6 updates 668 k
Transaction Summary
======================================================================================================================================================================================================
Install 2 Packages (+5 Dependent packages)
Upgrade ( 1 Dependent package)
Total download size: 1.7 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/base/packages/deltarpm-3.6-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY ] 0.0 B/s | 0 B --:--:-- ETA
Public key for deltarpm-3.6-3.el7.x86_64.rpm is not installed
(1/8): deltarpm-3.6-3.el7.x86_64.rpm | 82 kB 00:00:00
(2/8): createrepo-0.9.9-28.el7.noarch.rpm | 94 kB 00:00:00
(3/8): python-deltarpm-3.6-3.el7.x86_64.rpm | 31 kB 00:00:00
(4/8): python-chardet-2.2.1-3.el7.noarch.rpm | 227 kB 00:00:00
Public key for libxml2-2.9.1-6.el7_9.6.x86_64.rpm is not installed
(5/8): libxml2-2.9.1-6.el7_9.6.x86_64.rpm | 668 kB 00:00:01
(6/8): yum-utils-1.1.31-54.el7_8.noarch.rpm | 122 kB 00:00:00
(7/8): python-kitchen-1.1.1-5.el7.noarch.rpm | 267 kB 00:00:00
(8/8): libxml2-python-2.9.1-6.el7_9.6.x86_64.rpm | 247 kB 00:00:06
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 280 kB/s | 1.7 MB 00:00:06
Retrieving key from http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
From : http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : deltarpm-3.6-3.el7.x86_64 1/9
Installing : python-deltarpm-3.6-3.el7.x86_64 2/9
Updating : libxml2-2.9.1-6.el7_9.6.x86_64 3/9
Installing : libxml2-python-2.9.1-6.el7_9.6.x86_64 4/9
Installing : python-chardet-2.2.1-3.el7.noarch 5/9
Installing : python-kitchen-1.1.1-5.el7.noarch 6/9
Installing : yum-utils-1.1.31-54.el7_8.noarch 7/9
Installing : createrepo-0.9.9-28.el7.noarch 8/9
Cleanup : libxml2-2.9.1-6.el7_2.3.x86_64 9/9
Verifying : python-chardet-2.2.1-3.el7.noarch 1/9
Verifying : python-deltarpm-3.6-3.el7.x86_64 2/9
Verifying : libxml2-2.9.1-6.el7_9.6.x86_64 3/9
Verifying : libxml2-python-2.9.1-6.el7_9.6.x86_64 4/9
Verifying : createrepo-0.9.9-28.el7.noarch 5/9
Verifying : python-kitchen-1.1.1-5.el7.noarch 6/9
Verifying : deltarpm-3.6-3.el7.x86_64 7/9
Verifying : yum-utils-1.1.31-54.el7_8.noarch 8/9
Verifying : libxml2-2.9.1-6.el7_2.3.x86_64 9/9
Installed:
createrepo.noarch 0:0.9.9-28.el7 yum-utils.noarch 0:1.1.31-54.el7_8
Dependency Installed:
deltarpm.x86_64 0:3.6-3.el7 libxml2-python.x86_64 0:2.9.1-6.el7_9.6 python-chardet.noarch 0:2.2.1-3.el7 python-deltarpm.x86_64 0:3.6-3.el7 python-kitchen.noarch 0:1.1.1-5.el7
Dependency Updated:
libxml2.x86_64 0:2.9.1-6.el7_9.6
Complete!
同步阿里源
同步数据
创建存放阿里源的目录并同步数据。
同步阿里源时时间比较长,这个和网速有关系。
[root@localhost ~]# mkdir /mirrors
[root@localhost ~]# reposync -np /mirrors
创建索引
[root@localhost ~]# createrepo -po /mirrors/base/ /mirrors/base/
[root@localhost ~]# createrepo -po /mirrors/extras/ /mirrors/extras/
[root@localhost ~]# createrepo -po /mirrors/updates/ /mirrors/updates/
[root@localhost ~]# createrepo -po /mirrors/epel/ /mirrors/epel/
安装web服务
这里我们选择使用nginx
安装nginx
[root@localhost ~]# yum -y install nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-10.el7 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.20.1-10.el7 for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Running transaction check
---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed
---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
---> Package nginx-filesystem.noarch 1:1.20.1-10.el7 will be installed
---> Package openssl11-libs.x86_64 1:1.1.1k-5.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================================================================================
Installing:
nginx x86_64 1:1.20.1-10.el7 epel 588 k
Installing for dependencies:
centos-indexhtml noarch 7-9.el7.centos base 92 k
gperftools-libs x86_64 2.6.1-1.el7 base 272 k
nginx-filesystem noarch 1:1.20.1-10.el7 epel 24 k
openssl11-libs x86_64 1:1.1.1k-5.el7 epel 1.5 M
Transaction Summary
======================================================================================================================================================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 2.4 M
Installed size: 6.7 M
Downloading packages:
(1/5): nginx-filesystem-1.20.1-10.el7.noarch.rpm | 24 kB 00:00:00
(2/5): centos-indexhtml-7-9.el7.centos.noarch.rpm | 92 kB 00:00:00
(3/5): gperftools-libs-2.6.1-1.el7.x86_64.rpm | 272 kB 00:00:01
(4/5): nginx-1.20.1-10.el7.x86_64.rpm | 588 kB 00:00:02
(5/5): openssl11-libs-1.1.1k-5.el7.x86_64.rpm | 1.5 MB 00:00:02
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 734 kB/s | 2.4 MB 00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:openssl11-libs-1.1.1k-5.el7.x86_64 1/5
Installing : 1:nginx-filesystem-1.20.1-10.el7.noarch 2/5
Installing : centos-indexhtml-7-9.el7.centos.noarch 3/5
Installing : gperftools-libs-2.6.1-1.el7.x86_64 4/5
Installing : 1:nginx-1.20.1-10.el7.x86_64 5/5
Verifying : gperftools-libs-2.6.1-1.el7.x86_64 1/5
Verifying : centos-indexhtml-7-9.el7.centos.noarch 2/5
Verifying : 1:nginx-filesystem-1.20.1-10.el7.noarch 3/5
Verifying : 1:nginx-1.20.1-10.el7.x86_64 4/5
Verifying : 1:openssl11-libs-1.1.1k-5.el7.x86_64 5/5
Installed:
nginx.x86_64 1:1.20.1-10.el7
Dependency Installed:
centos-indexhtml.noarch 0:7-9.el7.centos gperftools-libs.x86_64 0:2.6.1-1.el7 nginx-filesystem.noarch 1:1.20.1-10.el7 openssl11-libs.x86_64 1:1.1.1k-5.el7
Complete!
修改配置文件
我们需要修改数据目录存放位置
[root@localhost ~]# cat /etc/nginx/nginx.conf
user root; #得用root用户,要不会报 open() "/data/centos/7/base" failed (13: Permission denied)
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 10240;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name mirrors.dfwlg.com;
root /mirrors; #这里是yum源存放目录
location / {
autoindex on; #打开目录浏览功能
autoindex_exact_size off; # off:以可读的方式显示文件大小
autoindex_localtime on; # on、off:是否以服务器的文件时间作为显示的时间
charset utf-8,gbk; #展示中文文件名
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
# systemctl restart nginx
写repo文件
我们提前写好repo文件存放在同步数据的目录下
[root@localhost ~]# cat /data/centos7.repo
[base]
name=base - mirror.qidi.com
baseurl=http://192.168.1.17/base/
enabled=1
gpgcheck=0
[updates]
name=updates - mirror.qidi.com
baseurl=http://192.168.1.17/updates/
enabled=1
gpgcheck=0
[extras]
name=extras - mirrors.qidi.com
baseurl=http://192.168.1.17/extras/
enabled=1
gpgcheck=0
[epel]
name=epel - mirrors.qidi.com
baseurl=http://192.168.1.17/epel/
failovermethod=priority
enabled=1
gpgcheck=0
访问
我们现在可以访问web页面进行查看,出现下图中的画面表示成功了
之后我们可以通过下面的命令,就可以访问我们的私有源了
[root@localhost ~]# curl -o /etc/yum.repos.d/centos7.repo http://192.168.1.17/centos7.repo
同步脚本
编写脚本
[root@localhost ~]# cat rsync-aliyun.sh
#!/bin/bash
DATAFILE=`date +%F_%T`
ls /var/log/aliyun &> /dev/null
if [ $? -eq 0 ];
then
ls /var/log/aliyun/aliyumrepo.log &> /dev/null
if [ $? -eq 0 ];
then
reposync -np /mirrors
if [ $? -eq 0 ];
then
createrepo --update /mirrors/base
createrepo --update /mirrors/extras
createrepo --update /mirrors/updates
createrepo --update /mirrors/epel
echo "SUCESS: $DATAFILE aliyum_yum update successful" >> /var/log/aliyun/aliyumrepo.log
echo "====================================================" >> /var/log/aliyun/aliyumrepo.log
else
echo "ERROR: $DATAFILE aliyum_yum update failed" >> /var/log/aliyun/aliyumrepo.log
echo "====================================================" >> /var/log/aliyun/aliyumrepo.log
fi
else
touch /var/log/aliyun/aliyumrepo.log
if [ $? -eq 0 ];
then
reposync -np /mirrors
if [ $? -eq 0 ];
then
createrepo --update /mirrors/base
createrepo --update /mirrors/extras
createrepo --update /mirrors/updates
createrepo --update /mirrors/epel
echo "SUCESS: $DATAFILE aliyum_yum update successful" >> /var/log/aliyun/aliyumrepo.log
echo "====================================================" >> /var/log/aliyun/aliyumrepo.log
else
echo "ERROR: $DATAFILE aliyum_yum update failed" >> /var/log/aliyun/aliyumrepo.log
echo "====================================================" >> /var/log/aliyun/aliyumrepo.log
fi
fi
fi
else
mkdir /var/log/aliyun
ls /var/log/aliyun/aliyumrepo.log &> /dev/null
if [ $? -eq 0 ];
then
reposync -np /mirrors
if [ $? -eq 0 ];
then
createrepo --update /mirrors/base
createrepo --update /mirrors/extras
createrepo --update /mirrors/updates
createrepo --update /mirrors/epel
echo "SUCESS: $DATAFILE aliyum_yum update successful" >> /var/log/aliyun/aliyumrepo.log
echo "====================================================" >> /var/log/aliyun/aliyumrepo.log
else
echo "ERROR: $DATAFILE aliyum_yum update failed" >> /var/log/aliyun/aliyumrepo.log
echo "====================================================" >> /var/log/aliyun/aliyumrepo.log
fi
else
touch /var/log/aliyun/aliyumrepo.log
if [ $? -eq 0 ];
then
reposync -np /mirrors
if [ $? -eq 0 ];
then
createrepo --update /mirrors/base
createrepo --update /mirrors/extras
createrepo --update /mirrors/updates
createrepo --update /mirrors/epel
echo "SUCESS: $DATAFILE aliyum_yum update successful" >> /var/log/aliyun/aliyumrepo.log
echo "====================================================" >> /var/log/aliyun/aliyumrepo.log
else
echo "ERROR: $DATAFILE aliyum_yum update failed" >> /var/log/aliyun/aliyumrepo.log
echo "====================================================" >> /var/log/aliyun/aliyumrepo.log
fi
fi
fi
fi
设置计划任务
计划任务定时间为每周6的1点执行这个脚本
[root@localhost ~]# chmod +x rsync-aliyun.sh
[root@localhost ~]# crontab -l
0 1 * * 6 /usr/bin/sh /root/rsync-aliyun.sh