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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM