资讯动态

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

发布时间:2026/10/16 5:34:48 来源:尧图企业网站定制
系统是最小化安装。文章使用国内阿里云。空间一共大概需要120G左右提前准备好空间否则到后面下载包时会停止还需要手工的磁盘lvm设置。yum仓库地址为192.168.6.36搭建nginxnginx-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 nginxcd 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 \ --usernginx --groupnginx \ 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 * myrepolscurl -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.conftouch /usr/local/nginx/conf/nginx.conf vim /usr/local/nginx/conf/nginx.confnginx服务器端配置文件# 主配置块 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/xmlrss text/javascript; # 添加HTTP头部以增加安全性 add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection 1; modeblock; add_header X-Frame-Options DENY; # 设置缓存控制头 add_header Cache-Control public, max-age86400; # 日志定制 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客户端配置文件priorityX: 设置仓库的优先级数值越小优先级越高。errorlog/var/log/yum.log: 将错误信息记录到指定的日志文件中。timeout60: 设置连接超时时间为60秒。retry3: 设置最大重试次数为3次。retrydelay10: 设置两次重试之间的延迟时间为10秒。clean_requirements_on_removeTrue: 在移除包时清理不再需要的依赖。clean_metadataTrue: 定期清理元数据缓存。module_hotfixesTrue: 允许安装模块热修复更新。vim /etc/yum.repos.d/Centos-myrepo.repo[base] nameCentOS-$releasever - Base - mirror.template.com baseurlhttp://192.168.6.36/base/ path/ enabled1 gpgcheck0 priority10 # 设置优先级 [updates] nameCentOS-$releasever - Updates - mirror.template.com baseurlhttp://192.168.6.36/updates/ path/ enabled1 gpgcheck0 priority10 # 设置优先级 [extras] nameCentOS-$releasever - Extras - mirrors.template.com baseurlhttp://192.168.6.36/extras/ path/ enabled1 gpgcheck0 priority10 # 设置优先级 [epel] nameCentOS-$releasever - epel - mirrors.template.com baseurlhttp://192.168.6.36/epel/ failovermethodpriority enabled1 gpgcheck0 priority10 # 设置优先级 # 错误报告配置 errorlog/var/log/yum.log # 超时和重试配置 timeout60 retry3 retrydelay10 # 清理缓存配置 clean_requirements_on_removeTrue clean_metadataTrue # 清理元数据缓存 # 模块支持如果使用模块化软件包 module_hotfixesTrue定时更新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 Sources 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] nameCentOS-$releasever - Base - .template.com baseurlhttp://192.168.6.90/appstream/ path/ enabled1 gpgcheck0 priority10 # 设置优先级 [baseos] nameCentOS-$releasever - Updates - .template.com baseurlhttp://192.168.6.90/baseos/ path/ enabled1 gpgcheck0 priority10 # 设置优先级 [epel] nameCentOS-$releasever - Extras - s.template.com baseurlhttp://192.168.6.90/epel/ path/ enabled1 gpgcheck0 priority10 # 设置优先级 [epel-cisco-openh264] nameCentOS-$releasever - epel - s.template.com baseurlhttp://192.168.6.90/epel-cisco-openh264/ failovermethodpriority enabled1 gpgcheck0 priority10 # 设置优先级 [epel-next] nameCentOS-$releasever - epel - s.template.com baseurlhttp://192.168.6.90/epel-next/ failovermethodpriority enabled1 gpgcheck0 priority10 # 设置优先级 [extras-common] nameCentOS-$releasever - epel - s.template.com baseurlhttp://192.168.6.90/extras-common/ failovermethodpriority enabled1 gpgcheck0 priority10 # 设置优先级 # 错误报告配置 errorlog/var/log/yum.log # 超时和重试配置 timeout60 retry3 retrydelay10 # 清理缓存配置 clean_requirements_on_removeTrue clean_metadataTrue # 清理元数据缓存 # 模块支持如果使用模块化软件包 module_hotfixesTrue

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑