Centos7系统搭建本地离线yum云仓库、设置同步-nginx优化项、启用压缩传输、开启目录查看、设置本地时间、设置字符集、防盗链、性能优化、文件类型检测、默认MIME类型-映射、客户端配置。

20 篇文章 0 订阅
10 篇文章 0 订阅

系统是最小化安装。文章使用国内阿里云。

空间一共大概需要120G左右!提前准备好空间否则到后面下载包时会停止,还需要手工的磁盘lvm设置。

yum仓库地址为:192.168.6.36 

搭建nginx

nginx-1.27.0.tar.gz官网下载链接

 部署安装之前安装nginx所需要的依赖环境
yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel wget
创建nginx运行用户和组
groupadd nginx
useradd -M -s /sbin/nologin -g nginx nginx

在/usr/local/src/目录下创建nginx目录下载nginx-1.27.0源代码包

cd /usr/local/src/&& mkdir nginx&&cd nginx&& wget https://nginx.org/download/nginx-1.27.0.tar.gz

解压nginx-1.27.0.

tar xf nginx-1.27.0.tar.gz;cd nginx-1.27.0
建议隐藏nginx版本信息
sed -i "13s/1.27.0/jingyu飞鸟/" /usr/local/src/nginx/nginx-1.27.0/src/core/nginx.h

sed -i "14s/nginx\//CSDN-/" /usr/local/src/nginx/nginx-1.27.0/src/core/nginx.h

sed -i "49s/nginx/CSDN-jingyu飞鸟/" /usr/local/src/nginx/nginx-1.27.0/src/http/ngx_http_header_filter_module.c

sed -i "36s/nginx/CSDN-jingyu飞鸟/" /usr/local/src/nginx/nginx-1.27.0/src/http/ngx_http_special_response.c

配置编译nginx

./configure --prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--user=nginx --group=nginx \
&& make && make install

添加环境变量

sed -i '$aexport PATH=$PATH:/usr/local/nginx/sbin/' /etc/profile
source /etc/profile
nginx

搭建配置yum仓库

修改yum源仓库为阿里源

cd /etc/yum.repos.d/ && mkdir myrepo && mv * myrepo&&ls
curl -O https://mirrors.aliyun.com/repo/epel-7.repo;curl -O https://mirrors.aliyun.com/repo/Centos-7.repo && yum makecache all

由于是最小化安装的系统没有开发工具、编译器、构建工具、base(基础工具),安装系统环境。

yum -y groupinstall "development tools" "base"

安装yum所需要相关工具软件

yum -y install createrepo yum-utils pcre-devel zlib-devel openssl openssl-devel make cmake gcc gcc-c++

创建源数据目录

mkdir /mirror

同步本地目录(如果没有创建系统也会自动创建目录,并下载。注意时间较长!!!)

reposync -p /mirror

更新新rpm的包

reposync -np /mirror

创建目录索引

createrepo -po /mirror/base/ /mirror/base/
createrepo -po /mirror/extras/ /mirror/extras/
createrepo -po /mirror/updates/ /mirror/updates/
createrepo -po /mirror/epel/ /mirror/epel/

更新源数据

createrepo --update /mirror/base
createrepo --update /mirror/extras
createrepo --update /mirror/updates
createrepo --update /mirror/epel

修改nginx 配置文件

删除原有的nginx.conf配置文件

rm -rf /usr/local/nginx/conf/nginx.conf
touch /usr/local/nginx/conf/nginx.conf&& vim /usr/local/nginx/conf/nginx.conf

nginx服务器端配置文件

# 主配置块
worker_processes  1;  #指定Nginx的工作进程数量。这里设置为1,意味着只有一个工作进程处理请求。
events {
    worker_connections  1024; #每个工作进程可以同时处理的最大连接数。这里设置为1024个并发连接。
}

http {
    # 包含MIME类型映射
    include       mime.types;
    # 设置默认MIME类型
    default_type  application/octet-stream; #当无法从mime.types中找到匹配项时,默认使用application/octet-stream MIME类型。
    # 开启高效文件传输, 启用高效文件传输方式(sendfile),减少数据拷贝次数。
    sendfile        on;
    # 设置客户端连接超时时间
    keepalive_timeout  65; #客户端空闲连接保持时间,单位是秒。这里设置为65秒。
    # 压缩输出
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    # 添加HTTP头部以增加安全性
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Frame-Options DENY;
    # 设置缓存控制头
    add_header Cache-Control "public, max-age=86400";
    # 日志定制
    access_log /var/log/nginx/yum.log combined;
    # SSL/TLS配置(如果需要HTTPS)
    # listen 443 ssl;
    # ssl_certificate /etc/nginx/ssl/cert.pem;
    # ssl_certificate_key /etc/nginx/ssl/key.pem;
    # ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    # ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
    # ssl_prefer_server_ciphers on;
    # 定义服务器
    server {
        listen       80;
        server_name  localhost;
        # 设置服务器根目录
        root         /mirror;
        # 设置缓存过期时间
        location ~* \.(js|css|jpg|jpeg|png|gif|ico)$ {
            expires 30d;
        }
        # 设置文件类型检测
        types_hash_max_size 2048;
        # 定义根路径处理方式
        location / {
            # 开启目录索引功能,#开启目录索引功能,允许用户浏览目录中的文件列表。
            autoindex on;
            # 设置文件大小显示方式
            autoindex_exact_size off; #控制文件大小的显示方式,off表示显示易读的格式,如KB、MB等。
            # 设置文件时间显示为服务器本地时间,文件的时间戳显示为服务器本地时间。
            autoindex_localtime on;
            # 设置字符集
            charset utf-8,gbk; #指定支持的字符集,以便正确显示中文文件名。
            # 设置默认索引文件
            index index.html;
        }
        # 设置错误页面
        error_page   404 /404.html;
        error_page   500 502 503 504  /50x.html;
        # 定义错误页面位置
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        location = /404.html {
            root   /usr/share/nginx/html;
        }
        # 防盗链
        if ($invalid_referer) {
            return 403;
        }
        # 性能优化
        tcp_nopush on;
        directio 1M;
    }
}

nginx客户端配置文件

  • priority=X: 设置仓库的优先级,数值越小优先级越高。
  • errorlog=/var/log/yum.log: 将错误信息记录到指定的日志文件中。
  • timeout=60: 设置连接超时时间为60秒。
  • retry=3: 设置最大重试次数为3次。
  • retrydelay=10: 设置两次重试之间的延迟时间为10秒。
  • clean_requirements_on_remove=True: 在移除包时清理不再需要的依赖。
  • clean_metadata=True: 定期清理元数据缓存。
  • module_hotfixes=True: 允许安装模块热修复更新。
vim /etc/yum.repos.d/Centos-myrepo.repo
[base]
name=CentOS-$releasever - Base - mirror.template.com
baseurl=http://192.168.6.36/base/
path=/
enabled=1
gpgcheck=0
priority=10  # 设置优先级

[updates]
name=CentOS-$releasever - Updates - mirror.template.com
baseurl=http://192.168.6.36/updates/
path=/
enabled=1
gpgcheck=0
priority=10  # 设置优先级

[extras]
name=CentOS-$releasever - Extras - mirrors.template.com
baseurl=http://192.168.6.36/extras/
path=/
enabled=1
gpgcheck=0
priority=10  # 设置优先级

[epel]
name=CentOS-$releasever - epel - mirrors.template.com
baseurl=http://192.168.6.36/epel/
failovermethod=priority
enabled=1
gpgcheck=0
priority=10  # 设置优先级

# 错误报告配置
errorlog=/var/log/yum.log

# 超时和重试配置
timeout=60
retry=3
retrydelay=10

# 清理缓存配置
clean_requirements_on_remove=True
clean_metadata=True  # 清理元数据缓存

# 模块支持(如果使用模块化软件包)
module_hotfixes=True

定时更新yum仓库数据库计划脚步

vim /etc/cron.d/update-yum-source-script.sh
#!/bin/bash

# 定义日志文件名
DATETIME=$(date +%F_%T)
LOG_FILE="/var/log/aliyumrepo_$DATETIME.log"

# 开始同步仓库
echo "Update the database of Alibaba Source" > "$LOG_FILE"
echo "Update the step execution time of Alibaba Source's database"$LOG_FILE"

# 执行reposync命令
reposync -np /mirror

# 检查reposync命令的退出状态
if [ $? -eq 0 ]; then
    # 更新仓库的元数据
    echo "Generating repository metadata..." >> "$LOG_FILE"
    createrepo --update /mirror/base
    createrepo --update /mirror/extras
    createrepo --update /mirror/updates
    createrepo --update /mirror/epel

    # 检查createrepo命令的退出状态
    if [ $? -eq 0 ]; then
        echo "SUCCESS: $DATETIME aliyum_yum update successful" >> "$LOG_FILE"
        echo "End time: $(date)" >> "$LOG_FILE"
    else
        echo "ERROR: $DATETIME createrepo failed" >> "$LOG_FILE"
        echo "End time: $(date)" >> "$LOG_FILE"
    fi
else
    echo "ERROR: $DATETIME reposync failed" >> "$LOG_FILE"
    echo "End time: $(date)" >> "$LOG_FILE"
fi

# 输出日志文件的位置
echo "Log file created at: $LOG_FILE"

客户端repo文件配置 

[appstream]
name=CentOS-$releasever - Base - .template.com
baseurl=http://192.168.6.90/appstream/
path=/
enabled=1
gpgcheck=0
priority=10  # 设置优先级
 
[baseos]
name=CentOS-$releasever - Updates - .template.com
baseurl=http://192.168.6.90/baseos/
path=/
enabled=1
gpgcheck=0
priority=10  # 设置优先级
 
[epel]
name=CentOS-$releasever - Extras - s.template.com
baseurl=http://192.168.6.90/epel/
path=/
enabled=1
gpgcheck=0
priority=10  # 设置优先级
 
[epel-cisco-openh264]
name=CentOS-$releasever - epel - s.template.com
baseurl=http://192.168.6.90/epel-cisco-openh264/
failovermethod=priority
enabled=1
gpgcheck=0
priority=10  # 设置优先级
 
[epel-next]
name=CentOS-$releasever - epel - s.template.com
baseurl=http://192.168.6.90/epel-next/
failovermethod=priority
enabled=1
gpgcheck=0
priority=10  # 设置优先级

[extras-common]
name=CentOS-$releasever - epel - s.template.com
baseurl=http://192.168.6.90/extras-common/
failovermethod=priority
enabled=1
gpgcheck=0
priority=10  # 设置优先级

# 错误报告配置
errorlog=/var/log/yum.log
 
# 超时和重试配置
timeout=60
retry=3
retrydelay=10
 
# 清理缓存配置
clean_requirements_on_remove=True
clean_metadata=True  # 清理元数据缓存
 
# 模块支持(如果使用模块化软件包)
module_hotfixes=True
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jingyu飞鸟

醒来觉得甚是爱你。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值