环境介绍

操作系统 IP地址 主机名 备注
Redhat7.4 192.168.10.21 yum-server  
Redhat7.4 192.168.10.22 jump01  

环境准备

1. 查看系统版本

[root@yum-server ~]# cat /etc/redhat-release 

Red Hat Enterprise Linux Server release 7.4 (Maipo)

2. 关闭防火墙

[root@yum-server ~]# systemctl stop ebtables firewalld

[root@yum-server ~]# systemctl disable ebtables firewalld

3. 关闭selinux

[root@yum-server ~]# sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinux 

[root@yum-server ~]# setenforce 0

操作步骤

1. 备份系统自带的yum源

[root@yum-server ~]# tar -zcvf Centos-bak.tar.gz /etc/yum.repos.d/*

2. 修改yum源为阿里云yum源

[root@yum-server ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@yum-server ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

或者

[root@yum-server ~]# curl -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@yum-server ~]# curl -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo  

3. 检测阿里云仓库源是否正常

[root@yum-server ~]# yum repolist
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                         repo name                                                               status
base/$releasever/x86_64                         CentOS-$releasever - Base - mirrors.aliyun.com                          10,019
epel/x86_64                                     Extra Packages for Enterprise Linux 7 - x86_64                          13,341
extras/$releasever/x86_64                       CentOS-$releasever - Extras - mirrors.aliyun.com                           435
updates/$releasever/x86_64                      CentOS-$releasever - Updates - mirrors.aliyun.com                        2,500
repolist: 26,295
 

4. 安装相关需要软件

[root@yum-server ~]# yum install -y wget make cmake gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel createrepo yum-utils

yum-utils:reposync同步工具

createrepo:编辑yum库工具

plugin-priorities:控制yum源更新优先级工具,这个工具可以用来控制进行yum源检索的先后顺序,建议可以用在client端。

注:由于很多人喜欢最小化安装,上边软件是一些常用环境。

 

5. 创建本地目录

[root@yum-server ~]# mkdir /mirrors/

6. 同步到本地目录

[root@yum-server ~]# reposync -r base -p /mirrors/

注:不用担心没有创建相关目录,系统自动创建相关目录,并下载,时间较长请耐心等待。

可以用 repo -r --repoid=repoid指定要查询的repo id,可以指定多个(# reposync -r base -p /mirror #这里同步base目录到本地)

 

7. 创建索引

createrepo语法:

格式:createrepo -po  源目录  索引元数据的输出位置目录

[root@yum-server ~]# createrepo -po /mirrors/base/  /mirrors/base/

[root@yum-server ~]# createrepo -po /mirrors/epel/  /mirrors/epel/

8. 更新源数据

[root@yum-server ~]# createrepo --update /mirrors/base

[root@yum-server ~]# createrepo --update /mirrors/epel

[root@yum-server ~]# createrepo --update /mirrors/extras

[root@yum-server ~]# createrepo --update /mirrors/updates

9. 创建定时任务脚本

[root@yum-server ~]# vim /mirrors/centos_yum_update.sh

#!/bin/bash

echo 'Updating Aliyum Source'

DATETIME=`date +%F_%T`

exec > /var/log/aliyumrepo_$DATETIME.log

     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: $DATETIME aliyum_yum update successful"

  else

      echo "ERROR: $DATETIME aliyum_yum update failed"

fi

10. 给脚本添加执行权限

[root@yum-server ~]# chmod a+x /mirrors/centos_yum_update.sh

11. 将脚本加入到定时任务中

[root@yum-server ~]# crontab -e

# Updating Aliyum Source

00 22 * * 6  /bin/bash /mirrors/centos_yum_update.sh

解释:每月第一个周六的22点更新阿里云yum源

12. 安装nginx,提供

[root@yum-server ~]# yum -y install nginx

13. 配置nginx的服务,让其家目录为:/mirrors

[root@yum-server ~]# vim /etc/nginx/nginx.conf 

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {

    worker_connections 1024;

}

http {

    include /etc/nginx/mime.types;

    default_type application/octet-stream;

    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;

    keepalive_timeout 65;

    gzip on;

    include /etc/nginx/conf.d/*.conf;

}

server {

    listen 80;

    server_name localhost;

    root /mirrors;

    index index.html index.htm;

    #charset koi8-r;

    #access_log /var/log/nginx/yum.access.log main;

    location / {

        autoindex on;

        autoindex_exact_size off;

        autoindex_localtime on;

        charset utf-8,gbk;

    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html

    #

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

        root /usr/share/nginx/html;

    }

}

14. 启动nginx服务

[root@yum-server ~]# systemctl restart nginx

[root@yum-server ~]# systemctl enable nginx

客户端测试

1. 修改客户端yum源,并指向本地搭建的yum源主机

[root@jump01 ~]# vim /etc/yum.repos.d/centos_local.repo 

[base]

name=base

baseurl=http://192.168.10.21/base/

failovermethod=priority

enabled=1

gpgcheck=0

[extras]

name=extras

baseurl=http://192.168.10.21/extras/

failovermethod=priority

enabled=1

gpgcheck=0


[updates]

name=updates

baseurl=http://192.168.10.21/updates/

failovermethod=priority

enabled=1

gpgcheck=0


[epel]

name=epel

baseurl=http://192.168.10.21/epel/

failovermethod=priority

enabled=1

gpgcheck=0


2. 生成yum缓存

[root@jump01 ~]# yum makecache 

3. 安装memcached测试

[root@jump01 ~]# yum -y install memcached