Apache中http变https

下载SSL证书

只看此博客中一、二、三、四步

安装SSL证书

1)首先上传SSL证书,我用FileZilla Client上传在/root/ssl中:
这里写图片描述
2)修改apache配置文件 httpd-ssl.conf
我服务器系统是ubuntu,输入命令find / -name httpd-ssl.conf,出现以下结果:
这里写图片描述
这有几个版本的apache,用的是哪一个呢?
我的解决方法是查看phpinfo,如下:
这里写图片描述
可以看出,路径为apache71,那么就修改apache71中的httpd-ssl.conf,共修改3处。

#  DocumentRoot "/usr/local/apache71/htdocs"
DocumentRoot "您的网站根目录"

#  SSLCertificateFile "/usr/local/apache71/conf/server.crt"
SSLCertificateFile "/root/ssl/certificate.crt"

#  SSLCertificateKeyFile "/usr/local/apache71/conf/server.key"
SSLCertificateKeyFile "/root/ssl/private.key"

代码中注释为原内容,下一行是修改的值,接着用service httpd restart重启apache

http强制https

在网站根目录中输入命令vi .htaccess,写入以下代码:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

代码参考于这个博客

最后可以在你的网站首页输入http访问,它将自动跳转到https。

猜你喜欢

转载自blog.csdn.net/s1124yy/article/details/79597265
今日推荐