下載openssl並安裝,下載地址:http://slproweb.com/products/Win32OpenSSL.html
假設安裝路徑為C:\"Program Files"\OpenSSL-Win64\bin\openssl.exe
如果沒有加入系統環境變量,下面的命令里的openssl要換成安裝路徑的值
一、生成秘鑰
openssl genrsa 1024 > server.key

這是用128位rsa算法生成密鑰,得到server.key文件 > 是輸出文件的標識符
二、生成未簽署的server.csr
openssl req -new -key server.key > server.csr
這是用步驟1的密鑰生成證書請求文件server.csr, 這一步會有很多參數,需要一一輸入
Country Name (2 letter code) [AU]:CN ISO國家代碼(只支持兩位字符)
State or Province Name (full name) [Some-State]:ZJ所在省份
Locality Name (eg, city) []:HZ所在城市
Organization Name (eg, company):SW_TECH公司名稱
Organizational Unit Name (eg, section) []:SW_TECH組織名稱
Common Name (eg, YOUR name) []:kedou.com申請證書的域名
Email Address []:admin@admin.com 管理員郵箱
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 交換密鑰
An optional company name []: 注:Common Name必須和httpd.conf中server name必須一致,否則apache不能啟動 (啟動apache時錯誤提示為:RSA server certificate CommonName (CN) `Kedou' does NOT match server name!? )
四、簽署服務器證書文件server.crt
openssl req -x509 -days 365 -key server.key -in server.csr > server.crt
說明:這是用步驟1,2的的密鑰和證書請求生成證書server.crt,-days參數指明證書有效期,單位為天,x509表示生成的為X.509證書。
五、這一步可選,配置apache的httpd.conf.
在conf目錄下的httpd_ssl.conf文件是關於ssl的配置,是httpd.conf的一部分,在 httpd.conf中找到給文件的引用,移除對應的注釋
Include conf/extra/httpd-ssl.conf
LoadModule ssl_module modules/mod_ssl.so
修改httpd-ssl.conf文件 並配置
SSLCertificateFile "D:/phpStudy/Apache/conf/ssl/server.crt" (具體的服務器地址)
SSLCertificateKeyFile "D:/phpStudy/Apache/conf/ssl/server.key" (具體的服務器地址)
CustomLog "D:/PHPStudy/Apache/logs/ssl_request.log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
配置
<VirtualHost _default_:443>
# General setup for the virtual host
DocumentRoot "D:/PHPStudy/WWW/" (域名帶訪問的目錄)
ServerName www.rgweb.com:443 (域名)
ServerAdmin admin@admin.com (管理員郵箱)
ErrorLog "D:/PHPStudy/Apache/logs/ssl_error.log.txt" (寫入日志)
TransferLog "D:/PHPStudy/Apache/logs/ssl_trans_error.log.txt" (寫入日志)
至此重啟apache
瀏覽器輸入 https 加你配置的域名
注:錯誤:WARNING: can't open config file: /apache24/conf/openssl.cnf
解決:將openssl_conf 放入環境變量 set openssl_conf=../conf/openssl.cnf
錯 :Unable to write ‘random state
解決:是因為沒有用管理員進入cmd,使用管理員身份進入cmd 就解決了
六、用flask等程序的話直接調用生成證書即可
flask使用證書,如果報ssl錯誤,需注意證書在代碼里的位置是否有調換,
'server.crt','server.key'這兩個證書出現在代碼里的位置不可以調換
app.run('0.0.0.0', debug=True, port=5000, ssl_context=('server.crt','server.key'))
