apache2.4配置https

 

1、获取证书

1.1 openssl生成SSL证书(自行百度)

1.2 腾讯云,阿里云,百度云等等都有提供免费的SSL证书

 

2、证书安装

编辑Apache根目录下 conf/httpd.conf 文件,找到#LoadModule ssl_module/mod_ssl.so 和 #Include conf/extra/httpd-ssl.conf,去掉前面的#号注释;

编辑Apache跟目录下 conf/extra/httpd-ssl.conf 文件,修改如下内容:

<VirtualHost *:443>
    DocumentRoot "/var/www/html"
    ServerName www.domain.com:443
    SSLEngine on
    SSLCertificateFile /usr/local/apache/conf/2_www.domain.com_cert.crt
    SSLCertificateKeyFile /usr/local/apache/conf/3_www.domain.com.key
    SSLCertificateChainFile /usr/local/apache/conf/1_root_bundle.crt
</VirtualHost>


配置完成后,重新启动 Apache 就可以使用https://www.domain.com来访问了。
注:

配置文件参数说明
SSLEngine on启用SSL功能
SSLCertificateFile证书文件
SSLCertificateKeyFile私钥文件
SSLCertificateChainFile

证书链文件

 

 

 

 

 

 

 

3、apache配置重定向

在.htaccess文件中加入如下代码:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1] </IfModule>

在.htaccess文件中加入如ps:如果项目使用了.htaccess隐藏了index.php(例如tp3.2),可在.htaccess文件中加入如下代码:

<IfModule mod_rewrite.c>
RewriteEngine On RewriteCond
%{HTTPS} off RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php
</IfModule>

也可以利用php做https的跳转:

<?php if ($_SERVER["HTTPS"] <> "on")
    {
    $xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

    header("Location: ".$xredir);
    }
?>

然后伪静态就正常去掉index.php功能:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
</IfModule>

配置完成后,http就可以重定向到https了。

 

4、遇到的问题

4.1 外网443端口被防火墙关闭

4.2 在windows Server 2012服务器上配置httpd-ssl.conf时VirtualHost *:443 * 号使用域名出错,这里建议用*/ip

转载于:https://www.cnblogs.com/jie-hu/p/8034226.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值