Shell脚本一键进行Apache服务的优化
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,使用Shell脚本一键进行Apache服务的优化(包含日志分割、日志文件分析系统、静态资源压缩、设置网页缓存时间、防盗链、系统版本号隐藏)本次实验使用软件包合集(均为开源软件):链接:https://
千家信息网最后更新 2026年11月01日Shell脚本一键进行Apache服务的优化
使用Shell脚本一键进行Apache服务的优化
(包含日志分割、日志文件分析系统、静态资源压缩、设置网页缓存时间、防盗链、系统版本号隐藏)
本次实验使用软件包合集(均为开源软件):链接:https://pan.baidu.com/s/1Byuu3PWgCap85lqfqrA6eA 提取码:x806 请添加链接描述
安装步骤:手工编译安装apache→安装dns服务→进行日志分割→安装日志文件分析系统→对静态资源进行压缩→设施网页缓存时间→防盗链设置→版本号隐藏
#!/bin/bashyum install bind expect -y#安装基础DNS及expect免交互read -p "提供硬盘的IP:" ipsmbclient -L //$ip/ <<-eofeofread -p "请输入想挂载的文件夹:" testmount.cifs -o username="******",password="******" //$ip/$test /mnt#账户密码请自定义df -hT | grep -o "cifs" &> /dev/nullif [ $? -eq 0 ];then echo "挂载成功,开始安装"else echo "失败,请重来" exit 1fi#远程挂载yum -y install \gcc \gcc-c++ \pcre \pcre-devel \zlib-devel \expat-devel#安装环境包cd /mnttar xjf httpd-2.4.29.tar.bz2 -C /opttar zxf apr-1.6.2.tar.gz -C /opt/tar zxf apr-util-1.6.0.tar.gz -C /opt/rpm -ivh cronolog-1.6.2-14.el7.x86_64.rpmcd /optmv apr-1.6.2/ httpd-2.4.29/srclib/aprmv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-utilcd httpd-2.4.29/./configure \--prefix=/usr/local/httpd \--enable-so \--enable-deflate \--enable-expires \--enable-rewrite \--enable-charest-lite \--enable-cgi make && make install#编译安装quanju="/etc/named.conf"quyu="/etc/named.rfc1912.zones"zhu=`ifconfig ens33 | awk 'NR==2{print $2}'`sed -i -e "13s/127.0.0.1/any/" -e '21s/localhost/any/' $quanjused -i "18azone \"zhy.com\" IN {\n type master\;\n file \"zhy.zone\"\;\n}\;\n" $quyucp -p /var/named/named.localhost /var/named/zhy.zonesed -i -e '$d' /var/named/zhy.zoneecho "www IN A $zhu" >> /var/named/zhy.zone#DNS配置http="/usr/local/httpd/conf/httpd.conf"sed -i -e '/Listen 80/s/^/#/' -e "/#Listen 12.34.56.78:80/s/#Listen 12.34.56.78:80/Listen $zhu:80/" $httpsed -i '/#ServerName www.example.com/s/#ServerName www.example.com/ServerName www.zhy.com/' $httpcd /usr/local/httpd/bin/./apachectl startsed -i '/ErrorLog \"logs\/error_log\"/s/ErrorLog \"logs\/error_log\"/ErrorLog \"\| \/usr\/sbin\/cronolog logs\/www.zhy.com.error_\%Y\%m\%d.log\"/' $httpsed -i '/CustomLog \"logs\/access_log\" common/s/CustomLog \"logs\/access_log\" common/CustomLog \"\| \/usr\/sbin\/cronolog logs\/www.zhy.com.access_\%Y\%m\%d.log\" combined/' $http#HTTP服务改监听、改域名ln -s /usr/local/httpd/bin/* /etc/init.d/ service apachectl stopservice apachectl start#日志分割cd /mnttar zxf awstats-7.6.tar.gzmv awstats-7.6 /usr/local/awstatscd /usr/local/awstats/tools/expect <<-eof spawn ./awstats_configure.pl expect ">" {send "/usr/local/httpd/conf/httpd.conf\r"} expect "y/N" {send "y\r"} expect ">" {send "www.zhy.com\r"} expect ">" {send "\r"} expect "continue..." {send "\r"} expect "finish..." {send "\r"}eoftime=`date -d 'today' '+%Y%m%d'`ls /usr/local/httpd/logs/ | grep "$time"if [ $? -eq 0 ];then sed -i "50s/\/var\/log\/httpd\/mylog.log/\/usr\/local\/httpd\/logs\/www.zhy.com.access_$time.log/" /etc/awstats/awstats.www.zhy.com.conffised -i "/LogFormat=1/s/1/4/" /etc/awstats/awstats.www.zhy.com.confmkdir /var/lib/awstatssed -i "/#LoadModule cgid_module modules\/mod_cgid.so/aLoadModule cgid_module modules\/mod_cgid.so" $httpsed -i -e '/Order allow,deny/s/^/#/' -e '/Allow from all/s/^/#/' $httpsed -i '/Allow from all/a Require all granted' $httpecho "* * 1 * * ls /usr/local/httpd/logs/ | grep $time &> /dev/null && sed -i \"50s/\/var\/log\/httpd\/mylog.log/\/usr\/local\/httpd\/logs\/www.zhy.com.access_$time.log/\" root /etc/awstats/awstats.www.zhy.com.conf" >> /etc/crontabecho "*/5 * * * * root /usr/local/awstats/tools/awstats_updateall.pl now" >> /etc/crontab/bin/systemctl reload crond.servicesystemctl enable crond#创建计划性任务touch /usr/local/httpd/htdocs/houtai.htmlecho "" >> /usr/local/httpd/htdocs/houtai.htmlecho " " >> /usr/local/httpd/htdocs/houtai.htmlecho " " >> /usr/local/httpd/htdocs/houtai.htmlecho " " >> /usr/local/httpd/htdocs/houtai.htmlecho " " >> /usr/local/httpd/htdocs/houtai.htmlecho "" >> /usr/local/httpd/htdocs/houtai.htmlservice apachectl stopservice apachectl startsystemctl stop firewalld.service setenforce 0systemctl start named#日志文件分割系统cd /mntsed -i '/LoadModule deflate_ module modules\/mod_ deflate.so/s/#/ /' $httpecho "" >> $httpecho " AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript" >> $httpecho " DeflateCompressionLevel 9" >> $httpecho " SetOutputFilter DEFLATE" >> $httpecho " " >> $http#静态资源压缩sed -i '/LoadModule expires_module/s/#/ /' $httpecho "" >> $httpecho " ExpiresActive On" >> $httpecho " ExpiresDefault \"access plus 30 seconds\"" >> $httpecho " " >> $http#网页缓存时间sed -i '/LoadModule rewrite_module/s/#/ /' $httpsed -i '248a RewriteEngine On\n RewriteCond %{HTTP_REFERER} !^http://zhy.com/.*$ [NC]\n RewriteCond %{HTTP_REFERER} !^http://zhy.com$ [NC]\n RewriteCond %{HTTP_REFERER} !^http://www.zhy.com/.*$ [NC]\n RewriteCond %{HTTP_REFERER} !^http://www.zhy.com/$ [NC]\n RewriteRule .*\\.(gif|jpg|swf)$ https://cache.yisu.com/upload/information/20200309/28/16809.jpg' $http#防盗链,防盗图片请放到默认站点htdocs中sed -i '/httpd-default.conf/s/#/ /' $httpdefault="/usr/local/httpd/conf/extra/httpd-default.conf"sed -i '55s/Full/Prod/' $default#隐藏版本号service apachectl stopservice apachectl startecho "本机IP为:$zhu"
日志
系统
防盗
服务
文件
时间
版本
缓存
网页
资源
静态
软件
链接
分析
编译
脚本
一键
成功
任务
图片
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技社
互联网金融 科技金融
苏州互联网科技贷款公司
丰台科技园 互联网
齐家互联网科技有限公司
互联网科技美容
科技互联网创业计划书
科技赋能互联网健康
中国科技互联网公司收入
央行互联网金融科技公司
昊天互联网科技有限公司
岳阳华臻互联网科技有限公司
哈尔滨屹滨互联网科技有限公司
亳州好赞互联网科技有限公司
互联网科技和法律行业
华中科技大学互联网的牛人
山东子淼互联网科技有限公司
伍佰万互联网科技怎么赚钱
萌小萌互联网科技河南有限公司
云鲸互联网络科技有限公司
深圳齐家互联网科技
掌上梦互联网科技
人人互联网科技是培训
欧洲十大科技互联网公司
南通创西澳互联网科技有限公司
互联网与数字科技关系
吉林省码上互联网科技电话
互联网时代高科技布局
三亚科技创新互联网基地
科技互联网专业务虚会材料