---
title: "apache安全配置tips"
author: "zhwj184"
date: "2013-10-17T09:39:52Z"
tags: []
categories: ["开源软件"]
slug: "apache-tips"
draft: false
lastmod: "2013-10-17T09:39:52Z"
id: 1959765
url: "https://zhwj184.iteye.com/blog/1959765"
---

原文：[http://www.javaarch.net/jiagoushi/1105.htm](http://www.javaarch.net/jiagoushi/1105.htm "http://www.javaarch.net/jiagoushi/1105.htm")

 

apache安全配置tips

 

1.apache出错页面隐藏apache版本和操作系统信息

 

默认情况下apache的404页面会出现apache的版本和OS信息，这个会成为一个主要的安全威胁点。那么如何隐藏这些信息，打开apache的httpd.conf配置文件，里面ServerSignature这个参数默认为on，然后再通过ServerTokens Prod这个选项在每个response中只返回apache字符串，而不返回具体的版本信息

 

\# vim /etc/httpd/conf/httpd.conf

 

ServerSignature Off

ServerTokens Prod

 

2.禁用目录列表

 

默认情况下apache会列出index file的目录内容，这个会暴露一些动态脚本比如php的源码，可以通过httpd.conf增加下面选项禁用列出目录内容

 

&lt;Directory /var/www/html&gt;

Options -Indexes

&lt;/Directory&gt;

 

3.经常更新到最新版的apache，解决一些apache的漏洞

 

\# httpd -v 查看apache版本，

根据不同的linux版本更新apache

\# yum update httpd

\# apt-get update apache2

 

4.禁用不需要的模块，找到httpd.conf的loadModule行，对于不需要的用#注释掉

 

\# grep LoadModule /etc/httpd/conf/httpd.conf

 

\# have to place corresponding \`LoadModule' lines at this location so the

\# LoadModule foo\_module modules/mod\_foo.so

LoadModule auth\_basic\_module modules/mod\_auth\_basic.so

LoadModule auth\_digest\_module modules/mod\_auth\_digest.so

LoadModule authn\_file\_module modules/mod\_authn\_file.so

LoadModule authn\_alias\_module modules/mod\_authn\_alias.so

LoadModule authn\_anon\_module modules/mod\_authn\_anon.so

 

5.用单独的账号和组账号运行apache

 

apache默认用nobody和daemon账号，为了安全问题我们需要用一个受限制的账号来运行apache，我们可以先增加另外的账号

 

\# groupadd http-web

\# useradd -d /var/www/ -g http-web -s /bin/nologin http-web

 

然后在httpd.conf修改user和group

 

User http-web

Group http-web

 

6.使用allow和deny来限制对目录的访问，对于根目录

 

&lt;Directory /&gt;

  Options None

  Order deny,allow

  Deny from all

&lt;/Directory&gt;

 

7.禁用apache的符号链接，apache默认是开启符号链接的，下面关闭

 

Options -FollowSymLinks

 

打开：

 

\# Enable symbolic links

Options +FollowSymLinks

 

8.禁用server side include和CGI

 

Options -Includes

Options -ExecCGI

 

对某个特定目录禁用

 

&lt;Directory "/var/www/html/web1"&gt;

Options -Includes -ExecCGI

&lt;/Directory&gt;

 

9.限制request请求包的大小，如果太大，可能会造成DOS攻击，在文件上传服务中需要限制上传文件的大小，通过参数LimitRequestBody设置

下面设置request大小为512k

 

&lt;Directory "/var/www/myweb1/user\_uploads"&gt;

  LimitRequestBody 512000

&lt;/Directory&gt;

 

10.保护DDOS攻击，比如TimeOut ：设置request的超时时间，默认300s，MaxClients ：设置最大连接客户端默认512,，KeepAliveTimeout ：设置长连接的超时时间，默认5s，LimitRequestFields：设置请求header字段大小，默认100

 

11.开启apache日志，设置ErrorLog 和CustomLog 

 

&lt;VirtualHost \*:80&gt;

DocumentRoot /var/www/html/example.com/

ServerName www.example.com

DirectoryIndex index.htm index.html index.php

ServerAlias example.com

ErrorDocument 404 /story.php

ErrorLog /var/log/httpd/example.com\_error\_log

CustomLog /var/log/httpd/example.com\_access\_log combined

&lt;/VirtualHost&gt;

 

12.安装设置SSL

 

\# openssl genrsa -des3 -out example.com.key 1024

\# openssl req -new -key example.com.key -out exmaple.csr

\# openssl x509 -req -days 365 -in example.com.com.csr -signkey example.com.com.key -out example.com.com.crt

 

&lt;VirtualHost 172.16.25.125:443&gt;

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/example.com.crt

SSLCertificateKeyFile /etc/pki/tls/certs/example.com.key

SSLCertificateChainFile /etc/pki/tls/certs/sf\_bundle.crt

ServerAdmin ravi.saive@example.com

ServerName example.com

DocumentRoot /var/www/html/example/

ErrorLog /var/log/httpd/example.com-error\_log

CustomLog /var/log/httpd/example.com-access\_log common

&lt;/VirtualHost&gt;