Apache服务器配置SSL证书时,遇到了一些问题,特意记录下。本屌腾讯云服务器
配置过程:
①在腾讯云给域名签发了SSL证书后,需要将其下载到本地。
解压证书文件夹找到Apache文件夹,可以看到三个文件,将其副本到服务器Apache根目录的conf文件夹里的ssl文件夹中。
②打开apache安装目录下conf目录中的httpd.conf文件,找到以下内容并去掉“#”
#LoadModule ssl_module modules/mod_ssl.so (如果找不到请确认是否编译过 openssl 插件)
#Include conf/extra/httpd-ssl.conf(删除行首的配置语句注释符号“#”,保存后退出)
③打开apache安装目录下conf / extra / httpd-ssl.conf文件,进行以下配置:
Listen 443
<VirtualHost *:443>
DocumentRoot "C:/phpstudy/PHPTutorial/WWW"
ServerName www.youdomain.cn
ServerAlias youdomain.cn
SSLEngine on
SSLProtocol TLSv1 TLSv1.1 TLSv1.2
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLCertificateFile "C:/phpstudy/Apache/PHPTutorial/conf/2_www.youdomain.cn.crt"
SSLCertificateKeyFile "C:/phpstudy/PHPTutorial/Apache/conf/3_www.youdomain.cn.key"
SSLCertificateChainFile "C:/phpstudy/PHPTutorial/Apache/conf/1_root_bundle.crt"
<Directory "C:/phpstudy/PHPTutorial/WWW/>
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
中途遇到的坑:
当取消掉包括开始的#时,会发现Apache服务器将无法正常启动,查阅博客帖子后,有大佬说可能是httpd-ssl.conf文件不正确导致的,打开Apache根目录下的bin目录,找到httpd.exe工具对httpd-ssl.conf文件进行校正
运行校验,发现Apache自带的httpd-ssl.conf配置文件就是错的,需要自行调整路径
调试配置路径直到不报错,将系统替换证书名称替换成自己的证书名称,启动Apache服务器,SSL证书配置完成,外网可以通过https正常访问。
————————————————
另外如果遇到如下错误,报错的地方屏蔽掉Listen 443,重新运行httpd.exe,没有错误了重启apache服务,输入域名小锁就加上了。点我查看实验结果
——————————————————————————
还有一个问题记录下,当我输入www.xxxx.com(我的域名),不会使用https链接, 直接用http打开了,这个是http重定向的问题。查了下以下是解决方法:
最后,解决方案是在 VirtualHost 节点里,添加如下配置:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
完整配置参数如下:
<VirtualHost *:80>
ServerName www.simbanet.cn
ServerAlias other.simbanet.cn
DocumentRoot "C:\wwwroot\mysite4"
# ErrorLog "logs\simbanet.com_error_apache.log"
# CustomLog "logs\simbanet.com_error_apache.log" common
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
<Directory "C:\wwwroot\mysite4">
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
这样,对应使用域名访问的时候就会自动跳转到 https 。