# Apache配置HSTS安全策略

> 果客户机使用的是集成式环境如：WAMP、PHPStudy、Lamp、Apache都可以参考本向导进行配置。 查找配置文件 在开启配置之前，需要找到 Apahce 的主配置文件 httpd.conf。 开启 mod_headers.so 模块 请使用 Notpad++ 编辑器（不要使用 Windows

- 原文: https://www.ssldun.com/article/certificate-483.html
- 栏目: 证书使用
- 发布: 2022-01-21
- 更新: 2022-01-21
- 关键词: 证书安装,SSL证书
- 引用政策: https://www.ssldun.com/citation-policy.html

## 正文

果客户机使用的是集成式环境如：WAMP、PHPStudy、Lamp、Apache都可以参考本向导进行配置。
查找配置文件
在开启配置之前，需要找到 Apahce 的主配置文件 httpd.conf。
开启 mod_headers.so 模块
请使用 Notpad++ 编辑器（不要使用 Windows 记事本）打开查找到 httpd.conf 配置文件。
在编辑器内按下查找快捷键 Control+F 唤出内容查找器，输入 mod_headers.so 并点击查找。
去除下面这行之前的#号，并保存配置文件。
LoadModule headers_module modules/mod_mod_headers.so
查找已经开启 SSL 的站点配置文件
在客户机上查找站点配置文件，站点配置文件通常位于 httpd.conf、vhost-ssl.conf、vhost.conf 或者自定义的配置文件内。
为了方便理解，这里我们假设找到的 SSL 站点配置文件为 mydomain-ssl.conf
开启站点 HSTS
插入 HSTS 代码
Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
编辑配置 mydomain-ssl.conf 内容如下
<VirtualHost *:443>
DocumentRoot "wwwmydomain"
ServerName mydomain.com:443
ServerAlias www.mydomain.com
ErrorLog "logs/error_log"
TransferLog "logs/access_log"

SSLEngine on
SSLCertificateFile "/conf/ssl/mydomain.pem"
SSLCertificateKeyFile "/conf/ssl/mydomain.key"
SSLCertificateChainFile "/conf/ssl/ca-chain.pem"
Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
</VirtualHost>
保存您正在编辑的配置文件
这里需要注意的是配置完 HSTS 命令后，务必修改其它文件。
重启和测试
重新启动您的 Apache，清除浏览器缓存，访问2次查看效果。
您也可以通过chrome浏览器的开发者选项→网络项查看服务器返回的头信息，来判断是否已经开启HSTS
根据您对HSTS的配置，所查看到的 Strict-Transport-Security 配置值可能不一样。
恭喜您！您的网站已经开启 HSTS 安全策略。
