apache安全优化
————————代表对什么样的内容启用gzip压缩
————————代表压缩级别
————————代表启用deflate模块对本站点的输出进行gzip压缩
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:
[root@localhost ~]# cd /abc/LAMP-C7/
[root@localhost LAMP-C7]# ls
apr-1.6.2.tar.gz httpd-2.4.29.tar.bz2 php-5.6.11.tar.bz2
apr-util-1.6.0.tar.gz LAMP-php5.6.txt
Discuz_X2.5_SC_UTF8.zip mysql-5.6.26.tar.gz
[root@localhost LAMP-C7]# tar xzvf apr-1.6.2.tar.gz -C /opt
[root@localhost LAMP-C7]# tar xzvf apr-util-1.6.0.tar.gz -C /opt
[root@localhost LAMP-C7]# tar xjvf httpd-2.4.29.tar.bz2 -C /opt
[root@localhost LAMP-C7]# cd /opt
[root@localhost opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
[root@localhost opt]# yum install gcc gcc-c++ perl pcre pcre-devel expat-devel make zlib-devel -y
已安装:
expat-devel.x86_64 0:2.1.0-10.el7_3 gcc.x86_64 0:4.8.5-39.el7
gcc-c++.x86_64 0:4.8.5-39.el7 pcre-devel.x86_64 0:8.32-17.el7
zlib-devel.x86_64 0:1.2.7-18.el7
作为依赖被安装:
cpp.x86_64 0:4.8.5-39.el7
glibc-devel.x86_64 0:2.17-292.el7
glibc-headers.x86_64 0:2.17-292.el7
kernel-headers.x86_64 0:3.10.0-1062.9.1.el7
libmpc.x86_64 0:1.0.1-3.el7
libstdc++-devel.x86_64 0:4.8.5-39.el7
更新完毕:
make.x86_64 1:3.82-24.el7 perl.x86_64 4:5.16.3-294.el7_6
作为依赖被升级:
glibc.x86_64 0:2.17-292.el7 glibc-common.x86_64 0:2.17-292.el7
libgcc.x86_64 0:4.8.5-39.el7 libgomp.x86_64 0:4.8.5-39.el7
libstdc++.x86_64 0:4.8.5-39.el7 perl-libs.x86_64 4:5.16.3-294.el7_6
zlib.x86_64 0:1.2.7-18.el7
完毕!
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@localhost opt]# cd httpd-2.4.29/
[root@localhost httpd-2.4.29]#
[root@localhost httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-expires --enable-cgi --enable-deflate
configure: summary of build options:
--
Server Version: 2.4.29
Install prefix: /usr/local/httpd
C compiler: gcc -std=gnu99
CFLAGS: -g -O2 -pthread
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E
[root@localhost httpd-2.4.29]# make && make install
mkdir /usr/local/httpd/man
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: 离开目录“/opt/httpd-2.4.29”
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@localhost httpd-2.4.29]# grep -v "#" /usr/local/httpd/bin/apachectl > /etc/init.d/httpd
[root@localhost httpd-2.4.29]# ls -l /etc/init.d/httpd
-rw-r--r--. 1 root root 987 12月 17 16:26 /etc/init.d/httpd
[root@localhost httpd-2.4.29]# chmod +x /etc/init.d/httpd
[root@localhost httpd-2.4.29]# ls -l /etc/init.d/httpd
-rwxr-xr-x. 1 root root 987 12月 17 16:26 /etc/init.d/httpd
[root@localhost httpd-2.4.29]# chkconfig --add httpd
[root@localhost httpd-2.4.29]# chkconfig --list httpd
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
httpd 0:关 1:关 2:关 3:开 4:关 5:开 6:关
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@localhost httpd-2.4.29]# vim /etc/init.d/httpd
//开头增加
#!/bin/sh
# chkconfig: 35 85 21
# description: Apache is a World Wide Web server
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf
51 Listen 192.168.247.149:80
52 #Listen 80
101 LoadModule filter_module modules/mod_filter.so
105 LoadModule deflate_module modules/mod_deflate.so
112 LoadModule headers_module modules/mod_headers.so
198 ServerName www.kgc.com:80
//在末尾加入deflate压缩模块详细参数
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/j
avascript text/jpg text/png
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
[root@localhost httpd-2.4.29]# systemctl stop firewalld.service
[root@localhost httpd-2.4.29]# setenforce 0
[root@localhost httpd-2.4.29]# apachectl -t
Syntax OK
[root@localhost httpd-2.4.29]# apachectl -t -D DUMP_MODULES | grep "deflate"
deflate_module (shared)
[root@localhost httpd-2.4.29]# systemctl start httpd
[root@localhost httpd-2.4.29]# netstat -natp | grep "httpd"
tcp 0 0 192.168.247.149:80 0.0.0.0:* LISTEN 36256/httpd
[root@localhost httpd-2.4.29]# cp /abc/bzhw.png /usr/local/httpd/htdocs/
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
bzhw.png index.html
[root@localhost htdocs]# vim index.html
It works!
在客户机内安装fidder,查看是否存在压缩的参数
备注:此时修改网页的参数,并没有重新开启服务,依旧可以读取到照片
启用mod_expires模块,并设置http协议下任意格式的文档均60秒后过期
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:
[root@localhost ~]#
[root@localhost ~]# cd /abchttpd2.4.2版本/
[root@localhost httpd2.4.2版本]#
[root@localhost httpd2.4.2版本]# ls
apr-1.4.6.tar.gz apr-util-1.4.1.tar.gz httpd-2.4.2.tar.gz
[root@localhost httpd2.4.2版本]# tar zxvf httpd-2.4.2.tar.gz -C /opt
[root@localhost httpd2.4.2版本]# tar zxvf apr-1.4.6.tar.gz -C /opt
[root@localhost httpd2.4.2版本]# tar zxvf apr-util-1.4.1.tar.gz -C /opt
[root@localhost httpd2.4.2版本]# cd /opt
[root@localhost opt]# ls
apr-1.4.6 apr-util-1.4.1 httpd-2.4.2 rh
[root@localhost opt]# mv apr-1.4.6/ httpd-2.4.2/srclib/apr
[root@localhost opt]# mv apr-util-1.4.1/ httpd-2.4.2/srclib/apr-util
[root@localhost opt]# cd httpd-2.4.2/
[root@localhost httpd-2.4.2]# ls
ABOUT_APACHE BuildBin.dsp httpd.dsp Makefile.in ROADMAP
acinclude.m4 buildconf httpd.spec Makefile.win server
Apache-apr2.dsw CHANGES include modules srclib
Apache.dsw config.layout INSTALL NOTICE support
apache_probes.d configure InstallBin.dsp NWGNUmakefile test
ap.d configure.in LAYOUT os VERSIONING
build docs libhttpd.dsp README
BuildAll.dsp emacs-style LICENSE README.platforms
[root@localhost httpd-2.4.2]# ./configure --prefix=/usr/local/httpd --enable-deflate --enable-expires --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
configure: error: in `/opt/httpd-2.4.2/srclib/apr':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
configure failed for srclib/apr
[root@localhost httpd-2.4.2]# yum install gcc gcc-c++ zlib-devel pcre pcre-devel make perl expat-devel -y
[root@localhost httpd-2.4.2]# ./configure --prefix=/usr/local/httpd --enable-deflate --enable-expires --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
config.status: creating include/ap_config_auto.h
config.status: executing default commands
[root@localhost httpd-2.4.2]# make
make[4]: 离开目录“/opt/httpd-2.4.2/modules/mappers”
make[3]: 离开目录“/opt/httpd-2.4.2/modules/mappers”
make[2]: 离开目录“/opt/httpd-2.4.2/modules”
make[2]: 进入目录“/opt/httpd-2.4.2/support”
make[2]: 离开目录“/opt/httpd-2.4.2/support”
make[1]: 离开目录“/opt/httpd-2.4.2”
[root@localhost httpd-2.4.2]# make install
mkdir /usr/local/httpd/man
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: 离开目录“/opt/httpd-2.4.2”
[root@localhost ~]# grep -v "#" /usr/local/httpd/bin/apachectl > /etc/init.d/httpd
[root@localhost ~]# vim /etc/init.d/httpd
//开头增加
#!/bin/sh
# chkconfig: 35 85 21
# description: Apache is a World Wide Web server
[root@localhost ~]# ln -s /usr/local/httpd/bin/* /usr/local/bin
[root@localhost ~]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@localhost ~]# vim /etc/httpd.conf
51 Listen 192.168.247.149:80
52 #Listen 80
188 ServerName www.kgc.com:80
108 LoadModule expires_module modules/mod_expires.so
//开启这个功能
489
490 ExpiresActive On
491 ExpiresDefault "access plus 50 seconds"
492
//末尾添加以上四行
[root@localhost ~]# apachectl -t
Syntax OK
//验证语法格式是否有误
[root@localhost ~]# apachectl -t -D DUMP_MODULES | grep "expires"
//查看模块
expires_module (shared)
[root@localhost ~]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
index.html
[root@localhost htdocs]# vim index.html
It works!
[root@localhost htdocs]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:
[root@localhost htdocs]# cd -
/abc
[root@localhost abc]# ls
bzhw.png
[root@localhost abc]# cp bzhw.png /usr/local/httpd/htdocs/
[root@localhost abc]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
bzhw.png index.html
[root@localhost htdocs]# systemctl stop firewalld.service
[root@localhost htdocs]# setenforce 0
[root@localhost htdocs]# apachectl start
//启动服务
备注:若是想要使网页识别中文,需要输入< meta charset=“utf-8”>标签
查看抓包工具,得到验证
[root@localhost htdocs]# vim /etc/httpd.conf
ExpiresActive On
ExpiresDefault "access plus 30 seconds"
[root@localhost htdocs]# apachectl restart
备注:
1.记得清除历史纪录
2.修改配置文件,需要重启服务
3.不支持systemctl 开启服务,可以使用service 开启服务
4.缓存的是静态数据,动态数据无法缓存
apache的版本信息,透露了一定的漏洞信息,从而给网站带来安全隐患
所以生产环境中要配置apache隐藏版本信息
[root@localhost httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf
490 Include conf/extra/httpd-default.conf
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/conf/
[root@localhost conf]# ls
extra httpd.conf magic mime.types original
[root@localhost conf]# vim extra/httpd-default.conf
55 ServerTokens Prod
65 ServerSignature Off
备注:systemctl restart httpd失败,需要先stop ,然后再start
或者apache restart 可以执行
防盗链就是防止别人的网站代码里面盗用服务器的图片、文件、视频等相关资源
如果别人盗用网站的这些静态资源,明显的是会增大服务器的带宽压力
所以作为网站的维护人员,要杜绝我们服务器的静态资源被其他网站盗用
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:
[root@localhost ~]# cd /abc/LAMP-C7/
[root@localhost LAMP-C7]# ls
apr-1.6.2.tar.gz httpd-2.4.29.tar.bz2 php-5.6.11.tar.bz2
apr-util-1.6.0.tar.gz LAMP-php5.6.txt
Discuz_X2.5_SC_UTF8.zip mysql-5.6.26.tar.gz
[root@localhost LAMP-C7]# tar zxvf apr-1.6.2.tar.gz -C /opt
[root@localhost LAMP-C7]# tar zxvf apr-util-1.6.0.tar.gz -C /opt
[root@localhost LAMP-C7]# tar xjvf httpd-2.4.29.tar.bz2 -C /opt
[root@localhost LAMP-C7]# cd /opt
[root@localhost opt]# ls
apr-1.6.2 apr-util-1.6.0 httpd-2.4.29 rh
[root@localhost opt]# mv apr-1.6.2/ httpd-2.4.29/srclib/apr
[root@localhost opt]# mv apr-util-1.6.0/ httpd-2.4.29/srclib/apr-util
[root@localhost opt]# ls
httpd-2.4.29 rh
[root@localhost opt]# yum install gcc gcc-c++ perl pcre pcre-devel expat-devel zlib-devel make -y
已安装:
expat-devel.x86_64 0:2.1.0-10.el7_3 gcc.x86_64 0:4.8.5-39.el7
gcc-c++.x86_64 0:4.8.5-39.el7 pcre-devel.x86_64 0:8.32-17.el7
zlib-devel.x86_64 0:1.2.7-18.el7
作为依赖被安装:
cpp.x86_64 0:4.8.5-39.el7
glibc-devel.x86_64 0:2.17-292.el7
glibc-headers.x86_64 0:2.17-292.el7
kernel-headers.x86_64 0:3.10.0-1062.9.1.el7
libmpc.x86_64 0:1.0.1-3.el7
libstdc++-devel.x86_64 0:4.8.5-39.el7
更新完毕:
make.x86_64 1:3.82-24.el7 perl.x86_64 4:5.16.3-294.el7_6
作为依赖被升级:
glibc.x86_64 0:2.17-292.el7 glibc-common.x86_64 0:2.17-292.el7
libgcc.x86_64 0:4.8.5-39.el7 libgomp.x86_64 0:4.8.5-39.el7
libstdc++.x86_64 0:4.8.5-39.el7 perl-libs.x86_64 4:5.16.3-294.el7_6
zlib.x86_64 0:1.2.7-18.el7
完毕!
[root@localhost httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-deflate --enable-expires --enable-mods-shared=most --with-mpm=worker
configure: summary of build options:
Server Version: 2.4.29
Install prefix: /usr/local/httpd
C compiler: gcc -std=gnu99
CFLAGS: -g -O2 -pthread
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E
或者这种版本
[root@localhost httpd-2.4.29]# ./configure --prefix=/usr/local/httpd --enable-so --enable-deflate --enable-rewrite --enable-cgi --enable-charset-lite
configure: summary of build options:
Server Version: 2.4.29
Install prefix: /usr/local/httpd
C compiler: gcc -std=gnu99
CFLAGS: -g -O2 -pthread
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E
[root@localhost httpd-2.4.29]# make && make install
mkdir /usr/local/httpd/man
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: 离开目录“/opt/httpd-2.4.29”
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/conf/httpd.conf /etc/httpd.conf
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@localhost httpd-2.4.29]# ln -s /usr/local/httpd/bin/apachectl /etc/init.d/httpd
[root@localhost httpd-2.4.29]#
[root@localhost httpd-2.4.29]# ls -l /etc/init.d/httpd
lrwxrwxrwx. 1 root root 30 12月 17 18:22 /etc/init.d/httpd -> /usr/local/httpd/bin/apachectl
localhost httpd-2.4.29]# vim /etc/init.d/httpd
1 #!/bin/sh
2 # chkconfig: 35 85 21
3 # description: Apache is a World Wide Web server
[root@localhost httpd-2.4.29]# chkconfig --add httpd
[root@localhost httpd-2.4.29]# chkconfig --list httpd
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
httpd 0:关 1:关 2:关 3:开 4:关 5:开 6:关
[root@localhost httpd-2.4.29]# vim /etc/httpd.conf
51 Listen 192.168.247.149:80
52 #Listen 80
198 ServerName www.kgc.com:80
[root@localhost httpd-2.4.29]# cp /abc/bzhw.png /usr/local/httpd/htdocs/
[root@localhost httpd-2.4.29]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
bzhw.png index.html
[root@localhost htdocs]# vim index.html
It works!
[root@localhost htdocs]# systemctl stop firewalld.service
[root@localhost htdocs]# setenforce 0
[root@localhost htdocs]#
[root@localhost htdocs]# systemctl start httpd
Warning: httpd.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@localhost htdocs]# systemctl daemon-reload
[root@localhost htdocs]# systemctl start httpd
[root@localhost htdocs]#
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]#
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.247.160:80
#Listen 80
Name www.accp.com:80
[root@localhost ~]# cd /var/www/
[root@localhost www]# ls
cgi-bin html
[root@localhost www]# cd html/
[root@localhost html]# ls
[root@localhost html]# vim index.html
[root@localhost ~]# systemctl start httpd.service
在服务端配置dns服务,以便盗机和客户端解析
否则在盗链主机处不能输入域名
配置步骤就不详述了,之前的博客有介绍
[root@localhost htdocs]# yum install bind -y
[root@localhost htdocs]# vim /etc/named.conf
[root@localhost htdocs]# vim /etc/named.rfc1912.zones
[root@localhost htdocs]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost kgc.com.zone
[root@localhost named]# vim kgc.com.zone
[root@localhost named]# systemctl start named
备注:防火墙记得关
盗机配置:
[root@localhost html]# echo "nameserver 192.168.247.149" > /etc/resolv.conf
[root@localhost html]# nslookup www.kgc.com
Server: 192.168.247.149
Address: 192.168.247.149#53
Name: www.kgc.com
Address: 192.168.247.149
[root@localhost html]# vim index.html
It is daoji
[root@localhost html]# systemctl restart httpd
再次查看,发现图已被盗
[root@localhost named]# vim /etc/httpd.conf
156 LoadModule rewrite_module modules/mod_rewrite.so
在htdocs目录参数配置中加入配置
[root@localhost named]# vim /etc/httpd.conf
223
248 Require all granted
249 RewriteEngine On
250 RewriteCond %{HTTP_REFERER} !^http://kgc.com/.*$ [NC]
251 RewriteCond %{HTTP_REFERER} !^http://kgc.com$ [NC]
252 RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/.*$ [NC]
253 RewriteCond %{HTTP_REFERER} !^http://www.kgc.com/$ [NC]
254 RewriteRule .*\.(gif|jpg|swf|png)$ http://www.kgc.com/error.png
255
在/usr/local/httpd/htdocs/中,添加error.png文件
[root@localhost named]# cp /abc/error.png /usr/local/httpd/htdocs/
[root@localhost named]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# ls
bzhw.png error.png index.html
[root@localhost htdocs]#
[root@localhost htdocs]# apachectl -t
Syntax OK
[root@localhost htdocs]# apachectl -t -D DUMP_MODULES | grep "rewrite"
rewrite_module (shared)
[root@localhost bin]# apachectl stop
[root@localhost bin]# apachectl start
可以发现盗机无法再访问到我方的相关图片