設定 ssl 憑證給 MAMP 開發環境使用

建立 ssl 資料夾,並新增 v3.ext 檔案,內容如下:

authorityKeyIdentifier = keyid, issuer basicConstraints = CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment subjectAltName = @alt_names [alt_names] DNS.1 = localhost

新增 server.csr.cnf 檔案,內容如下:

[req]
default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = dn
[dn]
C = TW
ST = Taipei
L = Taipei
O = Company name
OU = Web Development
CN = localhost
emailAddress=webmaster@example.com

使用指令產生檔案:

$openssl genrsa -des3 -out ~/ssl/rootCA.key 2048
$openssl req -x509 -new -nodes -key ~/ssl/rootCA.key -sha256 -days 1024 -out ~/ssl/rootCA.pem
$openssl req -new -sha256 -nodes -out server.csr -newkey rsa:2048 -keyout server.key -config <(cat server.csr.cnf)
$openssl x509 -req -in server.csr -CA ~/ssl/rootCA.pem -CAkey ~/ssl/rootCA.key -CAcreateserial -out server.crt -days 1024 -sha256 -extfile v3.ext
$openssl x509 -text -in server.crt -noout

完成後把 server.crt, server.key 檔案複製到 MAMP 相對目錄下。

修改 apache 設定檔:

# Secure (SSL/TLS) connections
Include /Applications/MAMP/conf/apache/extra/httpd-ssl.conf

修改 ssl 設定檔:

<VirtualHost *:443>
DocumentRoot "/Applications/MAMP/htdocs"
ServerName localhost:443
ServerAdmin webmaster@localhost
ErrorLog "/Applications/MAMP/logs/ssl_error_log"
TransferLog "/Applications/MAMP/logs/ssl_access_log"
SSLEngine on
SSLCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLProxyCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES
SSLCertificateFile "/Applications/MAMP/conf/apache/server.crt"
SSLCertificateKeyFile "/Applications/MAMP/conf/apache/server.key"
<FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/Applications/MAMP/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>
BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0
CustomLog "/Applications/MAMP/logs/ssl_request_log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>

** 最後如果發生無法順利重啟服務,修改:

#SSLMutex  "file:/Applications/MAMP/Library/logs/ssl_mutex"
Mutex  default

** 成功後瀏覽器的網址是 https://localhost:443https://localhost:8890。