本地环境配置网站HTTPS

原创 · 金汉江 · 2019-05-10

一 生成自签名证书

1 生成2048位的加密私钥

openssl genrsa -out server.key 2048

2 生成证书签名请求(CSR),这里需要填写许多信息,如国家,省市,公司等

openssl req -new -key server.key -out server.csr

Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:sh
Locality Name (eg, city) []:shanghai
Organization Name (eg, company) [Internet Widgits Pty Ltd]:mabang
Organizational Unit Name (eg, section) []:mabang
# 注意这里的主机名一定要和httpd.conf文件中的ServerName api.5yoho.cn保持一致,否则会报错。
Common Name (e.g. server FQDN or YOUR name) []:api.5yoho.cn
Email Address []:service@5yoho.cn

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:mabang

3 生成类型为X509的自签名证书。有效期设置3650天,即有效期为10年

openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

二 Apache 配置证书

1 修改conf/httpd.conf, 取消ssl及proxy相关注释

#LoadModule ssl_module modules/mod_ssl.so (去掉前面的#号)
#Include conf/extra/httpd-ssl.conf (去掉前面的#号)
#LoadModule proxy_module modules/mod_proxy.so (去掉前面的#号)
#LoadModule proxy_http_module modules/mod_proxy_http.so (去掉前面的#号)

2 修改conf/extra/httpd-ssl.conf文件

<VirtualHost *:443>
   ServerAdmin service@z7zba.com
   ServerName www.z7zba.com

   SSLEngine On
   SSLProxyEngine On
   SSLProxyVerify none
   SSLProxyCheckPeerCN off
   SSLProxyCheckPeerName off

   SSLCertificateFile "/usr/local/apache/conf/ssl/www.z7zba.com.crt"
    SSLCertificateKeyFile "/usr/local/apache/conf/ssl/www.z7zba.com.key"

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyPass / http://127.0.0.1:8080/
   ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

3 相关说明

配置文件参数 说明
LoadModule 加载SSL模块
Listen 监听443端口
DocumentRoot 网页目录
ServerName 站点域名
SSLEngine on 启用SSL功能
SSLCertificateFile 证书文件
SSLCertificateKeyFile 私钥文件
SSLCertificateChainFile 证书链文件

4 检查配置是否正常并重启Apache

[root@www ~]# /usr/local/apache/bin/httpd -t
Syntax OK
[root@www ~]# systemctl restart httpd