centos7.2搭建yun源和apt源服务器

搭建类似于网易源的服务器,方面内网服务器安装和更新软件包。

结论

一台centos7.2的服务器,可以同时提供yum源服务和apt源服务。都是使用apache提供web服务。同步其他yum源服务器使用reposync,同步其他apt源服务器使用apt-mirror.

使用软件和用途

Apache 用于提供web服务。
遇到问题2个:更改/目录,默认列出目录内容。第一个问题修改httpd.conf,第二个问题除了修改httpd.conf,还需移除welcome.conf。

apt-mirror 用于同步镜像其他apt服务器,使用rpm包安装。配置文件apt-mirror.conf需要修改同步文件保存位置、同步软件包位数amd64以及同步源url。

reposync 通过使用yum的方式同步yum仓库。参数—repoid指定.repo中[]的内容。-p指定同步保存位置。-n同步较新的。

createrepo用于生产rpm包的依赖数据信息。
createrpeo -pod /fo/os/x86_64 /fo/os/x86_64/ 表示对os软件包生成数据包依赖信息并放在那个目录。createrepo -g *comg.xml /fo/os/x86_64/ 用于生产yum grouplist信息。xml文件在网易镜像下载保存到任意位置即可。

安装规划

操作系统:centos7.2 x64,2c4G,100G系统盘,500G数据盘。分区lvm格式,数据盘单独一个lv,挂载在 /data目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
tree -L 3 /data
./
|--website
|--centos
| |--6
| |--7
| |--RPM-GPG-KEY-CentOS-6
| |--RPM-GPG-KEY-CentOS-7
|--CentOS-Base.repo
|--epel
| |--6
| |--7
|--sources.list
|--ubuntu
|--dists
|--pool

apache 根目录设置为/data/website

实施过程

设置wget代理和yum代理

由于部署的源服务器无法直接访问外网,通过代理服务器访问。

1
2
3
4
vi /etc/yum.conf
#添加内容 proxy=http://172.x.x.x:3128
vi ~/.wgetrc
#添加内容 http-proxy=172.x.x.x.:3128

创建目录

mkdir -p /data/website/{centos,epel}

安装apt-mirror并同步镜像

1
2
3
cd /tmp
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/Packages/a/apt-mirror-0.5.1-1.git420c3ec.el6.noarch.rpm
yum install apt-mirror*

vi /etc/apt-mirror.list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
set base_path      /data/apt-source
# set mirror_path $base_path/mirror
# set skel_path $base_path/skel
# set var_path $base_path/var
# defultarch不配置会自动检测,centos上会检测失败为i386,所以指定amd64.
set defaultarch amd64
# set nthreads 20

deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse

clean http://mirrors.163.com/ubuntu

同步163的ubuntu1404镜像,会启动20个线程,同步完成大概164G。

1
apt-mirror

同步完成后将下载的文件移动到/data/website/在原位置创建链接文件。
原因是apache不能显示链接目录,目前还没解决。这种方案也可行,定时同步也简单。

1
2
mv /data/apt-source/mirror/mirrors.163.com/ubuntu /data/website/
ln -s /data/website/ubuntu/ /data/apt-source/mirror/mirrors.163.com/

同步yum包并生成软件仓库

安装软件包

1
yum install yum-utils

配置本机的repo,需要配置centos6和centos7的,配置文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

## centos6
[base6]
name=CentOS-6-os-cmiot.local
baseurl=http://mirrors.163.com/centos/6/os/x86_64/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#released updates
[updates6]
name=CentOS-6-updates-cmiot.local
baseurl=http://mirrors.163.com/centos/6/updates/x86_64/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras6]
name=CentOS-6-extras-cmiot.local
baseurl=http://mirrors.163.com/centos/6/extras/x86_64/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

[epel6]
name=CentOS-6-epel-cmiot.local
baseurl=https://mirrors.aliyun.com/epel/6/x86_64/
gpgcheck=0

使用yum repolist查看是否生效,然后使用reposync同步
repolist 类似如下

1
2
3
4
5
6
7
8
9
10
11
yum repolist
repo id repo name status
base/7/x86_64 CentOS-7 - Base 9,591
base6 CentOS-6-os-cmiot.local 6,706
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,136
epel6 CentOS-6-epel-cmiot.local 12,444
extras/7/x86_64 CentOS-7 - Extras 326
extras6 CentOS-6-extras-cmiot.local 46
updates/7/x86_64 CentOS-7 - Updates 1,540
updates6 CentOS-6-updates-cmiot.local 834
repolist: 43,623

1
2
3
4
5
6
#同步centos7
reposync -n --repoid=extras --repoid=updates --repoid=base -p /data/website/centos/7
reposync -n --repoid=epel -p /data/website/epel/7/
#同步centos6
reposync -n --repoid=extras6 --repoid=updates6 --repoid=base6 -p /data/website/centos/6
reposync -n --repoid=epel6 -p /data/website/epel/6/

生成仓库数据文件

1
2
3
4
5
6
7
8
createrepo  /data/website/centos/7/os/x86_64 /data/website/centos/7/os/x86_64
wget http://mirrors.163.com/centos/7/os/x86_64/repodata/38b60f66d52704cffb8696750b2b6552438c1ace283bc2cf22408b0ba0e4cbfa-c7-x86_64-comps.xml -P /tmp
createrepo -g /tmp/38b\*-comps.xml /data/website/centos7/os/x86_64

createrepo /data/website/centos/7/updates/x86_64 /data/website/centos/7/updates/x86_64
createrepo /data/website/centos/7/extras/x86_64 /data/website/centos/7/extras/x86_64
createrepo /data/website/epel/7/x86_64 /data/website/epel/7/x86_64
#6类似,下载的comps.xml到6的位置下载。

下载key文件

1
2
wget http://mirrors.163.com/centos/7/os/x86_64/RPM-GPG-KEY-CentOS-7 -P /data/website/centos/
wget http://mirrors.163.com/centos/6/os/x86_64/RPM-GPG-KEY-CentOS-6 -P /data/website/centos/

设置apache

httpd.conf文件内容类似如下

1
2
3
4
5
6
7
8
9
10
DocumentRoot "/data/website"
<Directory "/data/website">
Options Indexes FollowSymLinks
Options Indexes
AllowOverride All
Order allow,deny
IndexOptions Charset=UTF-8
Allow from all
Require all granted
</Directory>

删除welcome配置,达到访问服务器地址直接现实站点根目录

1
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak

每天定时自动同步

auto-update.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#同步ubuntu
apt-mirror
#同步centos7
reposync -n --repoid=extras --repoid=updates --repoid=base -p /data/website/centos/7
reposync -n --repoid=epel -p /data/website/epel/7/
#同步centos6
reposync -n --repoid=extras6 --repoid=updates6 --repoid=base6 -p /data/website/centos/6
reposync -n --repoid=epel6 -p /data/website/epel/6/
createrepo --update /data/website/centos/7/os/x86_64/
createrepo --update /data/website/centos/7/updates/x86_64/
createrepo --update /data/website/centos/7/extras/x86_64/
createrepo --update /data/website/centos/6/os/x86_64/
createrepo --update /data/website/centos/6/updates/x86_64/
createrepo --update /data/website/centos/6/extras/x86_64/
createrepo --update /data/website/epel/7/x86_64/
createrepo --update /data/website/epel/7/x86_64/

用户端yum源文件和apt文件

centos6、7文件”CentOS-Base.repo”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[base]
name=CentOS-$releasever - Base - cmiot.local
baseurl=http://server-ip/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://server-ip/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates - cmiot.local
baseurl=http://server-ip/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http:/server-ip/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - cmiot.local
baseurl=http://server-ip/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://server-ip/centos/RPM-GPG-KEY-CentOS-7

[epel]
name=CentOS-$releasever - Extras - cmiot.local
baseurl=http://server-ip/epel/$releasever/$basearch/
gpgcheck=0

ubuntu1404x64 sources.list

1
2
3
4
5
6
deb [arch=amd64] http://ip/ubuntu/ trusty main restricted universe multiverse
deb [arch=amd64] http://ip/ubuntu/ trusty-security main restricted universe multiverse
deb [arch=amd64] http://ip/ubuntu/ trusty-updates main restricted universe multiverse
deb-src [arch=amd64] http://ip/ubuntu/ trusty main restricted universe multiverse
deb-src [arch=amd64] http://ip/ubuntu/ trusty-security main restricted universe multiverse
deb-src [arch=amd64] http://ip/ubuntu/ trusty-updates main restricted universe multiverse

参考链接

http://haibing.org/?p=213
https://centos.pkgs.org/6/epel-x86_64/apt-mirror-0.5.1-1.git420c3ec.el6.noarch.rpm.html
http://mirrors.163.com
http://www.linuxidc.com/Linux/2014-08/105415.htm