网站环境:
centos7、apache2.4.41
1、设置禁止访问网站目录
在/etc/httpd/conf/httpd.conf中
把# Options Indexes FollowSymLinks
改成Options FollowSymLinks
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
# Options Indexes FollowSymLinks
#禁用访问目录
Options FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
2、Apache配置虚拟主机
在/etc/httpd/conf.d/virtual.conf中
<VirtualHost *:80>
#网站管理员邮箱
ServerAdmin [email protected]
#绑定的主域
ServerName ainio.wang
#网站主目录
DocumentRoot "/var/www/html/"
#错误日志
#错误日志在/var/logs/httpd/文件夹
ErrorLog "logs/ainio.wang-error.log"
#访问日志
#访问日志在/var/logs/httpd/文件夹
CustomLog "logs/ainio.wang-access.log" common
</VirtualHost>
3、安装SSL证书
3.1、申请ssl证书
https://console.cloud.tencent.com/ssl
3.2、ssl安装教程参考
https://cloud.tencent.com/document/product/400/4143
3.3、ssl安装教程
1、把文件上传到服务器
在解压的文件
文件夹名称:Apache
文件夹内容:
1_root_bundle.crt 证书文件
2_www.domain.com.crt 证书文件
3_www.domain.com.key 私钥文件
将已获取到的 1_root_bundle.crt 证书文件、2_www.domain.com.crt 证书文件以及 3_www.domain.com.key 私钥文件从本地目录拷贝到 Apache 服务器的 /etc/httpd/ssl 目录下。
说明:
若无 /etc/httpd/ssl 目录,可通过 mkdir /etc/httpd/ssl 命令行创建。
2、配置httpd.conf
在 /etc/httpd/conf 目录下的 httpd.conf 配置文件找到 Include conf.modules.d/*.conf(用于加载配置 SSL 的配置目录)配置语句,并确认该配置语句未被注释。若已注释,请去掉首行的注释符号(#),保存配置文件。
3、配置00-ssl.conf
在 /etc/httpd/conf.modules.d 目录下的 00-ssl.conf 配置文件找到 LoadModule ssl_module modules/mod_ssl.so(用于加载 SSL 模块)配置语句,并确认该配置语句未被注释,若已注释,请去掉首行的注释符号(#),保存配置文件。
3.1、注意:
若以上配置文件中均未找到 LoadModule ssl_module modules/mod_ssl.so 和 Include conf.modules.d/*.conf 配置语句,请确认是否已经安装 mod_ssl.so 模块。若未安装 mod_ssl.so 模块,您可通过执行yum install mod_ssl 命令进行安装。
4、编辑 /etc/httpd/conf.d 目录下的 ssl.conf 配置文件。修改如下内容:
<VirtualHost 0.0.0.0:443>
DocumentRoot "/var/www/html"
#填写证书名称
ServerName www.domain.com
#启用 SSL 功能
SSLEngine on
#证书文件的路径
SSLCertificateFile /etc/httpd/ssl/2_www.domain.com.crt
#私钥文件的路径
SSLCertificateKeyFile /etc/httpd/ssl/3_www.domain.com.key
#证书链文件的路径
SSLCertificateChainFile /etc/httpd/ssl/1_root_bundle.crt
</VirtualHost>
5、重新启动 Apache 服务器,即可使用 https://www.domain.com 进行访问。
6、HTTP 自动跳转 HTTPS 的安全配置
方法1:
6.1、编辑 /etc/httpd/conf
目录下的 httpd.conf
配置文件。
6.2、请确认该配置文件是否存在LoadModule rewrite_module modules/mod_rewrite.so
。
若存在,请去掉LoadModule rewrite_module modules/mod_rewrite.so
前面的注释符号(#)号。并执行 步骤4。
6.3、若不存在,请执行 步骤3。
请您在/etc/httpd/conf.modules.d
中新建一个 *.conf 文件,例如 00-rewrite.conf
。在新建文件中添加以下内容:
LoadModule rewrite_module modules/mod_rewrite.so
6.4、在 httpd.conf
配置文件中添加如下内容:
<Directory "/var/www/html">
# 新增
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Directory>
6.5、重新启动 Apache 服务器,即可使用 http://www.domain.com 进行访问。
方法2:
1、在/etc/httpd/conf.modules.d
中新建一个 *.conf 文件,例如 00-rewrite.conf
。在新建文件中添加以下内容:
<Directory "/var/www/html">
# 新增
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Directory>
2、重新启动 Apache 服务器即可