Apache优化:隐藏版本信息,权限分离提高安全性,禁止日志遍历,日志切割

Apache优化

在实验环境中,我们不会去关注apache的优化,但是如果要用于生产环境中,必须要对apahe进行优化后,才可以上线进行服务,不然会出现很多问题:如安全性,不方便查看日志等等

实验环境

一台centos7主机IP192.168.1.11 纯净就可以,配置好yum源,如有需要软件包,我后面会提供百度云

一.查看Apache的版本信息

1)查看默认自己的版本信息

[root@apache ~]# yum -y install httpd
[root@apache ~]# yum -y install curl
[root@apache ~]# curl -I 192.168.1.11
在这里插入图片描述

2)隐藏apache版本信息

只有源码安装中的编译过程,才可以修改apache版本信息,我们先来源代码安装apahe

1.上传并解压httpd软件包

链接:https://pan.baidu.com/s/1WI4th9jijqpCn5H7wMiWtQ
提取码:whvt

[root@apache ~]# ls
在这里插入图片描述
[root@apache ~]# tar -zxvf httpd-2.4.38.tar.gz

2.修改ap_release.h参数以隐藏版本信息

[root@apache ~]# cd httpd-2.4.38/
[root@apache httpd-2.4.38]# vim include/ap_release.h
找到40~47行,改为我如下,也可以随意写,然后保存退出
在这里插入图片描述

3.源码安装上apache服务

1)先把刚刚yum安装的httpd服务删除了
[root@apache httpd-2.4.38]# yum -y remove httpd

2)安装编译环境
[root@apache httpd-2.4.38]# yum -y install gcc gcc-c++ apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre openssl*

3)编译并安装
[root@apache httpd-2.4.38]# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-ssl --enable-mpms-shared=all

这里解释一下参数
–enable-so :启动动态加载模块,也就是apache可以动态加载模块而不需要从新编译
–enable-rewrite:支持网站地址重写,也就是网页跳转,后面会有演示
–enable-charset-lite:让apche支持多种语言编码
–enable-cgi:让apahce可以支持cgi脚本
–enable-ssl:支持ssl加密,也就是https方式
–enable-mpms-shared=all:安装apache所有运行默认模块,主要用于模式切换,现在不必深究

[root@apache httpd-2.4.38]# make && make install

4)改一下配置文件,避免报错
因为是实验环境,ServerName改为自己主机名即可,真实环境这里写本服务的上的任意一个域名就行,其实没什么作用
[root@apache ~]# vim /usr/local/httpd/conf/httpd.conf
我这里主机名是apache,你们写你们的主机名即可,然后保存退出即可
在这里插入图片描述
5)设置一下启动脚本并让systemctl可以管理apache
[root@apache ~]# cp /usr/local/httpd/bin/apachectl /etc/init.d/apache
[root@apache ~]# vim /etc/init.d/apache
在第一行后添加两行内容,如下#号也要写上,注意格式要一致,空格都要一致,不然会报错

#chkconfig: 2345 11 88
# despriction:httpd apache server

在这里插入图片描述
这里解释一下意思
2345:是在哪些级别开机自启,3是字符,5是图形界面,至于2,4都是其他的,这样写就可以了
11:是开机自动启动的顺序,它开机自启也有顺序,因为10是网卡服务,所以意思就是开机的时候在网卡后面启动
88:这个就是关机的时候的关闭顺序了

despriction:是表述信息,可以随意
[root@apache ~]# chkconfig --add apache

6)启动服务
[root@apache ~]# systemctl start apache
[root@apache ~]# netstat -anpt | grep 80
tcp6 0 0 :::80 :::* LISTEN 47955/httpd

7)添加环境变量path,以便可以随时使用apache的命令
[root@apache ~]#ln -s /usr/local/httpd/bin/* /usr/local/bin/

8)访问页面
下面这是本机的IP
http://192.168.1.11/
在这里插入图片描述

3)测试隐藏版本信息效果

[root@apache ~]# curl -I 192.168.1.11
那个JAVA就是我们编译前改的配置文件的第42行,后面的数字就是那时自定义的版本号
在这里插入图片描述

4)彻底隐藏版本号

[root@apache ~]# vim /usr/local/httpd/conf/httpd.conf
这是apache的主配置文件,找到497行,取到前面的注释,意思是include调用另一个配置文件,修改后保存退出就可以了
在这里插入图片描述
[root@apache ~]# vim /usr/local/httpd/conf/extra/httpd-default.conf
55 与 65行更改为我如下
在这里插入图片描述
[root@apache ~]# systemctl restart apache
Job for apache.service failed because the control process exited with error code. See "systemctl status apache.service" and "journalctl -xe" for details. #这里报错了,没事,再次重启就可以了
[root@apache ~]# systemctl restart apache

5)测试最终结果

[root@apache ~]# curl -I 192.168.1.11
在这里插入图片描述

二.设置apache权限分离提供安全性

1)查看并修改apache默认用户
1.查看apache默认用户

[root@apache ~]# ps -aux | grep httpd
可以看到第蓝色圈上的root是启动用户作用不大,就是谁启动的apache服务就是谁,中间绿色的daemon才是apache的程序用户,用来响应客户端请求的用户,最后一是本条命令,不用管
在这里插入图片描述
[root@apache ~]# id daemon
uid=2(daemon) gid=2(daemon) 组=2(daemon)
[root@apache ~]# cat /etc/passwd | grep daemon
daemon:x:2:2:daemon:/sbin:/sbin/nologin

虽然daemon没有系统登陆的权限,但是好多服务默认都是daemon用户,如黑客获取了daemon用户权限有可能会破坏其他服务,所以我们要给apache创建一个单独用户

2.修改apache默认用户为apache

[root@apache ~]# cat /etc/passwd | grep apache
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

apache用户在apache服务安装后已经自动创建了
[root@apache ~]# vim /usr/local/httpd/conf/httpd.conf
更改174,与175行把原来的daemon改为apache,保存退出
在这里插入图片描述
[root@apache ~]# systemctl restart apache
Job for apache.service failed because the control process exited with error code. See “systemctl status apache.service” and “journalctl -xe” for details.

[root@apache ~]# systemctl restart apache
[root@apache ~]# ps -aux | grep httpd
再次查看,运行用户已经改过来了
在这里插入图片描述

2)权限分离

在实验环境中一般使用root用户,或单一用户给予关于其服务的全部权限,可是这样在实际生产完全性非常低,一旦有入侵者攻破这一用户,那整个网站服务和其中的数据,也就任其所为了

1.先设置一些重要目录的属主属组,之后再进行细分

1)将整个apache目录属主属组设为apache
[root@apache ~]# chown -R apache:apache /usr/local/httpd/
-R的意思就这/usr/local/htppd,包括httpd目录本身,整个下面的所有文件目录属组属主都为apache用户

2)再单独给于daemon用户,让daemon用户拥有对应权限
[root@apache ~]# chown -R daemon:daemon /usr/local/httpd/htdocs/
这里设置的把上面的顶掉了,apache也只算其他用户
[root@apache ~]# ll /usr/local/httpd/htdocs/
总用量 4 #这里可以发现apache对其也只有读权限,一定程度分离了权限
-rw-r--r-- 1 daemon daemon 45 6月 12 2007 index.html

2.网页与目录文件快速赋权

在网站的站点目录,也就是/usr/local/httpd/htdocs,会存在目录或文件,我们需要给予对应的权限

1.设置此目录下所有文件的权限为644
这里解释一下644 :6就是属主读写(4+2) , 4属组读(4), 4其他用户读(4)

[root@apache ~]# find /usr/local/httpd/htdocs -type f -exec chmod 644 {} ;
-type后面的f就是文件的意思,-exec 是要执行的命令,{} 是将find的结果 执行前面的chmod命令

[root@apache ~]# ll /usr/local/httpd/htdocs/index.html
-rw-r--r-- 1 daemon daemon 45 6月 12 2007 /usr/local/httpd/htdocs/index.html

2.设置此目录下所以目录的权限为755
7属主读写执行,5属组读执行,5其他人读执行

先再htdocs目录下,创建一个目录,方便看效果
[root@apache ~]# mkdir /usr/local/httpd/htdocs/directory
[root@apache ~]# find /usr/local/httpd/htdocs -type d -exec chmod 755 {} ;
-d是目录的意思
[root@apache ~]# ll -d /usr/local/httpd/htdocs/directory/
drwxr-sr-x 2 root daemon 6 2月 26 02:57 /usr/local/httpd/htdocs/directory/
#这个属组的s暂且不管,总之现在属组也有x执行权限,通过getfacl命令查看

[root@apache ~]# getfacl /usr/local/httpd/htdocs/directory/
getfacl: Removing leading '/' from absolute path names
# file: usr/local/httpd/htdocs/directory/
# owner: root
# group: daemon
# flags: -s-
user::rwx
group::r-x #这行就是属组的权限
other::r-x

3.日志文件单独授权为root用户,提高安全

如果apache用户被入侵,此方法会保护日志文件不被黑客篡改
[root@apache ~]# chown -R root:root /usr/local/httpd/logs/
[root@apache ~]# ll -d /usr/local/httpd/logs/access_log
-rw-r--r-- 1 root root 211 2月 26 01:26 /usr/local/httpd/logs/access_log

其他用户也仅有读取权限

4.重启服务,保证权限配置正确,用户依然可以正常访问

[root@apache ~]# systemctl restart apache
访问测试,仍可正常访问
在这里插入图片描述

三.禁止日志遍历

apache默认开启目录浏览功能,意思就是/usr/local/httpd/htdocs中如果不存在index.html文件,就显示这个目录下的目录结构
如果index.html文件为空不会显示目录结构
如果存在其他html文件,但不是index.html也会显示目录结构

1.在htdocs目录下创建测试目录与文件

[root@apache ~]# echo "123123" > /usr/local/httpd/htdocs/123.html
[root@apache ~]# mkdir /usr/local/httpd/htdocs/{dir1,dir2}
[root@apache ~]# ls /usr/local/httpd/htdocs/
123.html dir1 dir2 directory index.html

2.移走index.html

[root@apache ~]# mv /usr/local/httpd/htdocs/index.html /opt/

3.测试,可以看到目录结构

http://192.168.1.11/
在这里插入图片描述
这样对应普通没有上传需求的网站,非常不安全

4.修改配置文件,取消此功能

[root@apache ~]# vim /usr/local/httpd/conf/httpd.conf
修改243行,改为我下图,报错退出
在这里插入图片描述
[root@apache ~]# systemctl restart apache
Job for apache.service failed because the control process exited with error code. See “systemctl status apache.service” and “journalctl -xe” for details.

[root@apache ~]# systemctl restart apache

5.测试

再次访问,已不会显示目录结构,而是报错
在这里插入图片描述

四.日志切割

默认不进行日志切割,日志都是在一个文件中,很不方便查看如果不进行日志切割无法进行定期清理日志,造成资源浪费

1)使用apache系统自动切割工具rotatelogs
1.先修改主配值文件

首先是错误日志
[root@apache ~]# vim /usr/local/httpd/conf/httpd.conf
找的281行改为我如下这样,如果你想直接复制粘贴我后面有打好的,然后不要退出还有参数要改
在这里插入图片描述

ErrorLog "|/usr/local/httpd/bin/rotatelogs -l logs/error_%Y%m%d.log 86400"

这里解释一下其中的参数
|:是调用命令
-l:是命令参数
%Y%m%d:是时间格式,表示当前系统的日期
86400:单位为秒,意思是当前一个错误日志生成后,过86400秒(24小时后),在生成下一个错误日志文件

2.更改正确日志位置

继续找到310行,改为我如下,这是正确日志位置,然后保存退出
在这里插入图片描述

CustomLog "|/usr/local/httpd/bin/rotatelogs -l logs/access_%Y%m%d.log 86400" combined

[root@apache ~]# systemctl restart apache
重启服务的时候一定要看到下面的信息后再重启一遍,然后才可看到效果,如看不到下面信息就重启多次即可
Job for apache.service failed because the control process exited with error code. See “systemctl status apache.service” and “journalctl -xe” for details.

[root@apache ~]# systemctl restart apache

4.测试rotatelogs工具效果

1.先测试错误日志
[root@apache ~]# ls /usr/local/httpd/logs/
可以看到,已经有错误日志,而且是当前的日期,另几个日志是没有开启日志分隔时的日志
access_log error_20200226.log error_log httpd.pid

[root@apache ~]# date
2020年 02月 26日 星期三 12:18:38 CST

2.测试正确日志
这里是因为测试日志遍历,现在移动回来
[root@apache ~]# mv /opt/index.html /usr/local/httpd/htdocs/

客户访问机网页httpd://192.168.1.11时多次刷新
回到服务器中,发现已经多出acces日志而且是以时间格式显示
[root@apache ~]# ls /usr/local/httpd/logs/
access_20200226.log access_log error_20200226.log error_log httpd.pid

2)使用cronolog进行日志切换

apache自带的切割工具容易造成数据丢失,因此我们一般使用第三方切割工具cronolog

1.上传cronolog工具并安装

链接:https://pan.baidu.com/s/1547EuH46lSQa-5sbf-IL-g
提取码:q6ij

回到root目录下,并上传软件包
在这里插入图片描述
[root@apache ~]# tar -zxvf cronolog-1.6.2.tar.gz
[root@apache cronolog-1.6.2]# ./configure && make && make install

2.查找安装到了哪里,/sbin下的那个是命令

[root@apache cronolog-1.6.2]# find / -name cronolog
/root/cronolog-1.6.2/src/cronolog
/usr/local/sbin/cronolog

3.这次使用虚拟主机做

1.先打开虚拟主机功能
[root@apache ~]# vim /usr/local/httpd/conf/httpd.conf
去掉这行的#号
在这里插入图片描述
[root@apache ~]# systemctl restart apache
Job for apache.service failed because the control process exited with error code. See “systemctl status apache.service” and “journalctl -xe” for details.

[root@apache ~]# systemctl restart apache

2.添加一个I临时P(重启网卡会失效),做基于IP的虚拟主机
[root@apache ~]# ifconfig ens33:1 192.168.1.200
[root@apache ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::953c:64db:834f:b238  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ea:b3:4d  txqueuelen 1000  (Ethernet)
        RX packets 30927  bytes 11775679 (11.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17799  bytes 2781661 (2.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.200  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:ea:b3:4d  txqueuelen 1000  (Ethernet)

3.修改虚拟主机配置文件,并配置日志切割
[root@apache ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
里面有两个例子,把而个例子,整个都删掉,第一个改为我如下,我后面有打好的,可以直接复制
在这里插入图片描述

<VirtualHost 192.168.1.200:80>
    ServerAdmin ajbn@ajbn.com
    DocumentRoot "/virtual/htdocs"
    ServerName apache
    ErrorLog "|/usr/local/sbin/cronolog /virtual/logs/www.ajbn.com_error_%Y%m%d.log"
    CustomLog "|/usr/local/sbin/cronolog /virtual/logs/www.ajbn.com-access_%Y%m%d.log" combined
    <Directory />
    Require all granted
    </Directory>
</VirtualHost>

4.创建配置文件中写的目录与默认首页
[root@apache ~]# mkdir -p /virtual/{logs,htdocs}
[root@apache ~]# echo "<h1>Log File</h1>" > /virtual/htdocs/index.html
[root@apache ~]# systemctl restart apache
Job for apache.service failed because the control process exited with error code. See “systemctl status apache.service” and “journalctl -xe” for details.

[root@apache ~]# systemctl restart apache

5.测试效果

http://192.168.1.200/
先访问虚拟主机,并刷新多次
在这里插入图片描述
在回来查看,日志,因为没有出错,这里只有正确日志
[root@apache ~]# ls /virtual/logs/
我这后来改过日期,所以和上面对不上,文件后面的日期就是当前日期,可使用date命令查看
www.ajbn.com-access_20201210.log

然后我解释一下那个没有轮转时间,意思就是只要系统日期变更了,就从新记录一份,也就是,分割一个日志文件出来,然后文件名称是当前日期,这里不是秒数的概念了而是日期

可以试一下
[root@apache ~]# date -s "2020-12-11"
这条命令修改系统时间
2020年 12月 11日 星期五 00:00:00 CST

这时候,访问一次虚拟主机192.168.1.200
再回来看日志,已经多了一个
[root@apache logs]# ls
www.ajbn.com-access_20201210.log www.ajbn.com-access_20201211.log

然后上面没有说一个问题,现在运行apache的用户是apache,正常说我们新创建的文件/virtual和下面的子文件夹,apache用户肯定没有写入权限,日志也就无法写入,

但是这里可以写入,这是因为是root用户打开了apache服务,他就是主进程用户,而apache服务的日志文件是由主进程用户负责并写入的

!!实验完毕

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值