開通443端口通信
如果是服務器部署在雲端,記得開啟443端口通信許可
生產ssl證書
第一種:免費申請網站:https://freessl.cn/
第二種:openssl(phpstudy 自帶)
1,設置openssl環境變量
使用DOS命令進入Apache bin目錄,因為該目錄才有libeay32.dll
、openssl.exe
、ssleay32.dll
等文件。
使用DOS命令在bin目錄下執行命令:set OPENSSL_CONF=..\conf\openssl.cnf,通過此命令設置openssl的環境變量,如果不執行此命令,后面的操作會報錯。
set OPENSSL_CONF=..\conf\openssl.cnf
2,生成服務器的私鑰
注釋:這是使用128位的RSA算法生成的密鑰,還可以使用其他的算法生成密鑰,相關的用法可以使用搜索引擎搜索。4096是密鑰的長度,這個值最好使用4096以上的值,必須是2的整數次方。
openssl genrsa 4096 > server.key
3,生成未簽署的server.csr
openssl req -new -key server.key > server.csr Country Name (2 letter code) [AU]:CN ISO國家代碼(只支持兩位字符) State or Province Name (full name) [Some-State]:Hu Bei 所在省份 Locality Name (eg, city) []:Wu Han 所在城市 Organization Name (eg, company) [Internet Widgits Pty Ltd]:Mark Company 公司名稱 Organizational Unit Name (eg, section) []:IT 組織名稱 Common Name (e.g. server FQDN or YOUR name) []:www.phpmarker.com 申請證書的域名 Email Address []:phpmarker@163.com 管理員郵箱 Please enter the following ‘extra’ attributes to be sent with your certificate request A challenge password []: 交換密鑰 可空 An optional company name []: 可空
可能會報錯:無法定位序數xxx於動態鏈接庫libeay32.dll,解決辦法:將apache的bin目錄下的libeay32.dll文件復制到c:\windows\system32下。
4,簽署服務器證書文件server.crt
這個命令使用第三步和第四步生成的密鑰和證書來生成證書server.crt,-days參數表示證書有效期,單位為天,x509表示生成的是X.509證書。
openssl req -x509 -days 365 -key server.key -in server.csr > server.crt
5,查看證書詳細信息
openssl x509 -noout -text -in server.crt
部署SSL
1,確認apache是否有ssl模塊
配置Apache服務器支持https協議和SSL證書,最基本的要求是Apache包含openssl模塊。還好apache/bin目錄下有libeay32.dll
、openssl.exe
、ssleay32.dll
,自帶了ssl模塊,若沒有該模塊,需自行下載單獨的openssl。
2,打開apache的配置文件conf/httpd.conf,去掉ssl模塊前面的#
LoadModule ssl_module modules/mod_ssl.so
...
Include conf/extra/httpd-ssl.conf
3,備份修改httpd-ssl.conf
Listen 443 SSLPassPhraseDialog builtin
<VirtualHost _default_:443> DocumentRoot "C:\Program Files\api" ServerName api.test.com ServerAlias api.test.com ErrorLog "C:\Program Files\api\logs\websslapi-error.log" TransferLog "C:\Program Files\api\logs\websslapi-access.log" <Directory "C:\Program Files\api"> Options FollowSymLinks ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted </Directory> SSLEngine on SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL SSLCertificateFile "C:\phpstudy\Apache\conf\ssl\api.test.com.crt" SSLCertificateKeyFile "C:\phpstudy\Apache\conf\ssl\api.test.com.key" SSLCertificateChainFile "C:\phpstudy\Apache\conf\ssl\api.test.com_ca_bundle.crt" SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire BrowserMatch ".*MSIE.*" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 CustomLog "C:\Program Files\api\logs\ssl_request.log" \ "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" </VirtualHost>
4,測試配置
C:\phpstudy\Apache\bin>httpd.exe -t
Syntax OK
5,動態加載新配置
C:\phpstudy\Apache\bin>httpd.exe -k restart -n apache2a
6,打開瀏覽器測試
httpd-ssl.conf