Centos7通過reposync同步國內yum源-搭建局域網內網本地Yum源


在生產環境中無法連接外網的情況,在安裝某些軟件時通常又需要先安裝大量的基礎依賴包,通常的做法是使用系統鏡像配置一個本地yum源,使用yum安裝依賴,但是鏡像通常較大,如果只是為了安裝一個兩個的包配置一個鏡像yum源費時費力,我們可以事先在自己的可以連接外網測試環境主機使用reposync的方式同步國內的yum源下載緩存rpm包到本地機,使用createrepo命令制作一個本地內網yum倉庫。

測試主機操作系統:centos7.4 x64,2c4G,100G系統盤,1T數據盤。分區lvm格式,數據盤單獨一個lv,掛載在 /data目錄。

reposync命令是一個python腳本。包含在yum-utils包中。因此,我們如果要使用reposync命令的時候,需要安裝yum-utils包。

使用以下命令:yum install yum-utils

reposync -r 倉庫名(一般為base) -p 目標目錄

沒有的小伙伴門自行安裝yum-utils

1.安裝apache和repository的管理工具yum-utils
yum install httpd yum-utils createrepo -y
systemctl enable httpd
systemctl start httpd

2.創建文件保存目錄
mkdir -p /data/website/centos/6/
mkdir -p /data/website/centos/7/
mkdir -p /data/website/epel/

3.修改/etc/yum.repos.d/CentOS-Base.repo

## centos7 [base7] name=CentOS-7-os-cmiot.local baseurl=http://mirrors.163.com/centos/7/os/x86_64/ gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7 #released updates [updates7] name=CentOS-7-updates-cmiot.local baseurl=http://mirrors.163.com/centos/7/updates/x86_64/ gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7 #additional packages that may be useful [extras7] name=CentOS-7-extras-cmiot.local baseurl=http://mirrors.163.com/centos/7/extras/x86_64/ gpgcheck=1 gpgkey=http://mirror.centos.org/centos/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 ## epel [epel6] name=CentOS-6-epel-cmiot.local baseurl=https://mirrors.aliyun.com/epel/6/x86_64/ gpgcheck=0 [epel7] name=CentOS-7-epel-cmiot.local baseurl=https://mirrors.aliyun.com/epel/7/x86_64/ gpgcheck=0 

4.使用yum repolist驗證CentOS-Base.repo文件是否生效

# yum repolist
Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile repo id repo name base6 CentOS-6-os-cmiot.local base7 CentOS-7-os-cmiot.local epel6 CentOS-6-epel-cmiot.local epel7 CentOS-7-epel-cmiot.local extras6 CentOS-6-extras-cmiot.local extras7 CentOS-7-extras-cmiot.local updates6 CentOS-6-updates-cmiot.local updates7 CentOS-7-updates-cmiot.local 

5.同步CentOS-Base.repo文件中163的centos6包文件

reposync -n --repoid=extras6 --repoid=updates6 --repoid=base6 -p /data/website/centos/6 reposync -n --repoid=epel6 -p /data/website/epel/ 

6.同步CentOS-Base.repo文件中163的centos7包文件

reposync -n --repoid=extras7 --repoid=updates7 --repoid=base7 -p /data/website/centos/7 reposync -n --repoid=epel7 -p /data/website/epel/ 

createrepo 命令用於創建yum源(軟件倉庫),即為存放於本地特定位置的眾多rpm包建立索引,描述各包所需依賴信息,並形成元數據。
參數選項說明:

-u  --baseurl <url> 指定Base URL的地址 -o --outputdir <url> 指定元數據的輸出位置 -x --excludes <packages> 指定在形成元數據時需要排除的包 -i --pkglist <filename> 指定一個文件,該文件內的包信息將被包含在即將生成的元數據中,格式為每個包信息獨占一行,不含通配符、正則,以及范圍表達式。 -n --includepkg 通過命令行指定要納入本地庫中的包信息,需要提供URL或本地路徑。 -q --quiet 安靜模式執行操作,不輸出任何信息。 -g --groupfile <groupfile> 指定本地軟件倉庫的組划分,范例如下: createrepo -g comps.xml /path/to/rpms 注意:組文件需要和rpm包放置於同一路徑下。 -v --verbose 輸出詳細信息。 -c --cachedir <path> 指定一個目錄,用作存放軟件倉庫中軟件包的校驗和信息。 當createrepo在未發生明顯改變的相同倉庫文件上持續多次運行時,指定cachedir會明顯提高其性能。 --update 如果元數據已經存在,且軟件倉庫中只有部分軟件發生了改變或增減, 則可用update參數直接對原有元數據進行升級,效率比重新分析rpm包依賴並生成新的元數據要高很多。 -p --pretty 以整潔的格式輸出xml文件。 -d --database 該選項指定使用SQLite來存儲生成的元數據,默認項。 

7.創建索引
格式:createrepo -po 源目錄 索引元數據的輸出位置目錄

createrepo -po /data/website/centos/6/base6/ /data/website/centos/6/base6/ createrepo -po /data/website/centos/6/updates6/ /data/website/centos/6/updates6/ createrepo -po /data/website/centos/6/extras6/ /data/website/centos/6/extras6/ createrepo -po /data/website/epel/epel6/ /data/website/epel/epel6/ createrepo -po /data/website/centos/7/base7/ /data/website/centos/7/base7/ createrepo -po /data/website/centos/7/updates7 /data/website/centos/7/updates7 createrepo -po /data/website/centos/7/extras7 /data/website/centos/7/extras7 createrepo -po /data/website/epel/epel7 /data/website/epel/epel7 

8.創建組文件的索引
說明:yum groupinstall的時候會用到這個索引。
wget http://mirrors.163.com/centos/6/base6/repodata/43d8fd068164b0f042845474d6a22262798b9f0d1f49ad1bf9f95b953089777d-c6-x86_64-comps.xml -P /tmp
wget http://mirrors.163.com/centos/7/base7/repodata/38b60f66d52704cffb8696750b2b6552438c1ace283bc2cf22408b0ba0e4cbfa-c7-x86_64-comps.xml -P /tmp
createrepo -g /tmp/43d*-comps.xml /data/website/centos/6/base6/
createrepo -g /tmp/38b*-comps.xml /data/website/centos/7/base7/

9.下載KEY文件
用戶下載安裝這個RPM包時,引入RH官方的這個RPM GPG公鑰,用來驗證RPM包是不是RH官方簽名的
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/

10.設置apache 根目錄設置為/data/website
修改/etc/httpd/conf/httpd.conf
注釋#DocumentRoot "/var/www/html"這一行,在其下添加

#DocumentRoot "/var/www/html" 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> 

11.刪除welcome配置,達到訪問服務器地址直接現實站點根目錄
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
12.開啟httpd服務
systemctl restart httpd
systemctl enable httpd
瀏覽器就可以通過訪問IP來訪問/data/website目錄

 

 

13.定時reposync同步任務

用戶端/etc/yum.repos.d/CentOS-Base.repo文件修改
下面以centos6為例:

[base] name=CentOS-$releasever - Base - cmiot.local baseurl=http://172.20.11.249/centos/$releasever/base6 gpgcheck=1 gpgkey=http://172.20.11.249/centos/RPM-GPG-KEY-CentOS-6 #released updates [updates] name=CentOS-$releasever - Updates - cmiot.local baseurl=http://172.20.11.249/centos/$releasever/updates6 gpgcheck=1 gpgkey=http://172.20.11.249/centos/RPM-GPG-KEY-CentOS-6 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras - cmiot.local baseurl=http://172.20.11.249/centos/$releasever/extras6 gpgcheck=1 gpgkey=http://172.20.11.249/centos/RPM-GPG-KEY-CentOS-6 [epel] name=CentOS-$releasever - Extras - cmiot.local baseurl=http://172.20.11.249/epel/epel6 gpgcheck=0 

設置每天定時更新腳本 update.sh

#!/bin/bash echo 開始同步ubuntu apt-mirror echo ubuntu同步結束 echo 開始同步centos6 reposync -n --repoid=extras6 --repoid=updates6 --repoid=base6 -p /data/website/centos/6 reposync -n --repoid=epel6 -p /data/website/epel/ echo centos6同步結束 echo 開始同步centos7 reposync -n --repoid=extras7 --repoid=updates7 --repoid=base7 -p /data/website/centos/7 reposync -n --repoid=epel7 -p /data/website/epel/ echo centos7同步結束 #更新元數據 createrepo --update /data/website/centos/6/base6/ createrepo --update /data/website/centos/6/updates6/ createrepo --update /data/website/centos/6/extras6/ createrepo --update /data/website/epel/epel6/ createrepo --update /data/website/centos/7/base7/ createrepo --update /data/website/centos/7/updates7/ createrepo --update /data/website/centos/7/extras7/ createrepo --update /data/website/epel/epel7/ 

14. crontab -e 添加定時任務

0 6 * * * /data/update.sh 

    crontab -l 查看定時任務
0 6 * * * /data/update.sh

 
15.附加 配置NGINX網站代理
 
15.1安裝nginx   yum -y install nginx

  安裝完以后以下是Nginx的默認路徑:

  (1) Nginx配置路徑:/etc/nginx/   (2) PID目錄:/var/run/nginx.pid   (3) 錯誤日志:/var/log/nginx/error.log   (4) 訪問日志:/var/log/nginx/access.log   (5) 默認站點目錄:/usr/share/nginx/html

  事實上,只需知道Nginx配置路徑,其他路徑均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查詢到。

15.2修改配置/etc/nginx/nginx.conf  配置修改 root目錄路徑,添加location配置信息
   vim /etc/nginx/nginx.conf 

 

 

38 server {
39   listen 80 default_server;
40   listen [::]:80 default_server;
41   server_name localhost;
42   root  /data/sdb2/yum-local/;
43
44 # Load configuration files for the default server block.
45 include /etc/nginx/default.d/*.conf;
46
47 location / {
48 }
49 location /centos {
50
51    autoindex on;
52    autoindex_exact_size off;
53    autoindex_localtime on;
54
55 }
56
57 error_page 404 /404.html;
58 location = /40x.html {
59 }

15.3 啟動Nginx並設置開機自動運行

(開啟服務后需要稍等片刻才能加入開機自啟動) systemctl start nginx.service
systemctl stop nginx.service
systemctl enable nginx.service
   systemctl disable nginx.service

15.4 打開防火牆相應端口或關閉防火牆

systemctl stop firewalld.service
   systemctl disable firewalld.service

 

15.5 關閉selinux

vim /etc/selinux/config    將  SELINUX=disabled

15.6 打開網站測試如下圖

 

 

15.7  打開網頁403 forbidden 錯誤處理方法 

一、由於啟動用戶和nginx工作用戶不一致所致

  1.1查看nginx的啟動用戶,發現是nobody,而為是用root啟動的

     命令:ps aux | grep "nginx: worker process" | awk'{print $1}'

  1.2將nginx.config的user改為和啟動用戶一致,

    命令:vi conf/nginx.conf   修改頁面頂部 user nginx;   為 user root;    

 

二、缺少index.html或者index.php文件,就是配置文件中index index.html index.htm這行中的指定的文件。

  1.    server {
  2.      listen       80;
  3.      server_name  localhost;
  4.      index  index.php index.html;
  5.      root  /data/sdb2/yum-local;
  6.    }

    如果在/data/www/下面沒有index.php,index.html的時候,直接文件,會報403 forbidden。

三、權限問題,如果nginx沒有web目錄的操作權限,也會出現403錯誤。

解決辦法:修改web目錄的讀寫權限,或者是把nginx的啟動用戶改成目錄的所屬用戶,重啟Nginx即可解決

  1.    chmod -R 777 /data
  2.    chmod -R 777 /data/sdb2/yum-local

四、SELinux設置為開啟狀態(enabled)的原因。

  4.1、查看當前selinux的狀態。

  1.    /usr/sbin/sestatus

  4.2、將SELINUX=enforcing 修改為 SELINUX=disabled 狀態。

  1.    vi /etc/selinux/config
  2.
  3.    #SELINUX=enforcing
  4.    SELINUX=disabled

   4.3、重啟生效。reboot。

五、未輸入完整路徑導致的原因。
   如果設置根目錄為  /data/sdb2/yum-local  下級子目錄為 centos
  瀏覽器地址欄輸入 http://192.168.123.222/centos/  即可



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM