Apache的SSL配置

创建证书

$ openssl genrsa -out server.key 2048
$ openssl req -new -days 365 -key server.key -out server.csr
$ openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 365

使用mkcert创建证书。

mkcert 是一款用于在本地创建和管理本地 HTTPS 开发证书的工具。

$ brew install mkcert
$ brew install nss
$ mkcert -key-file server.key -cert-file server.crt localhost

Apache的设置。

Apache2.4.41 可将其改写为 “Apache 版本号为 2.4.41″。

LoadModule ssl_module libexec/apache2/mod_ssl.so
LoadModule socache_shmcb_module libexec/apache2/mod_socache_shmcb.so

Include /private/etc/apache2/extra/httpd-ssl.conf
SSLCertificateFile "/private/etc/apache2/server.crt"
SSLCertificateKeyFile "/private/etc/apache2/server.key"

将已签名的证书(server.crt)和RSA私钥(server.key)放置在/private/etc/apache2目录下作为证明。

将HTTP重定向到HTTPS

LoadModule rewrite_module libexec/apache2/mod_rewrite.so

# Redirect http to https
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

启动apache

$ apachectl configtest
Syntax OK

$ apachectl start
bannerAds