因此梅干上網爬了一下文章,在反覆的實際操作後,終於成功的在localhost或127.0.0.1,透過OPENSSL發送SSL憑證,並將發送出來本機SSL憑證,安裝到MAMP的免費版中,因此想要在本機環境中,安裝SSL憑證的朋友,也一塊來看看囉!
安裝OPENSSL
brew update
brew install openssl
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
brew install openssl
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
Step1
當安裝好openssl後,在終端機中,輸入openssl version當看到版號時,表示已將openssl安裝完成。
Step2
接著將下方的文字,儲存在MAMP/htdocs目錄底下,並命名為ssl.conf。
ssl.conf
[req]
prompt = no
default_md = sha256
default_bits = 2048
distinguished_name = dn
x509_extensions = v3_req [dn]
C = TW
ST = Taiwan
L = Taipei
O = Duotify Inc.
OU = IT Department
emailAddress = admin@example.com
CN = localhost [v3_req]
subjectAltName = @alt_names [alt_names]
DNS.1 = *.localhost
DNS.2 = localhost
IP.1 = 127.0.0.1
prompt = no
default_md = sha256
default_bits = 2048
distinguished_name = dn
x509_extensions = v3_req [dn]
C = TW
ST = Taiwan
L = Taipei
O = Duotify Inc.
OU = IT Department
emailAddress = admin@example.com
CN = localhost [v3_req]
subjectAltName = @alt_names [alt_names]
DNS.1 = *.localhost
DNS.2 = localhost
IP.1 = 127.0.0.1
Step3
這時再開啟終端機,並進入htdocs的目錄底下,接著輸入下方的指令,分別就會產生私密金鑰 (server.key) 與 憑證檔案 (server.crt)二個檔案
openssl req -x509 -new -nodes -sha256 -utf8 -days 3650 -newkey rsa:2048 -keyout server.key -out server.crt -config ssl.conf
Step4
在產生完畢後,再輸入下方的指令,,將剛剛所產生好的二個檔案,加到鑰匙圈中,這時會需要輸入電腦的管理者密碼。
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain server.crt
Step5
這時再到MAMP/conf/apache/httpd.conf,再把下方的文字貼到文件中,並將它儲存後重啟MAMP。
# Custom code for SSL
Listen 443 NameVirtualHost *:443 <VirtualHost *:443>
DocumentRoot "/Applications/MAMP/htdocs"
ServerName localhost
SSLEngine on
SSLCertificateFile "/Applications/MAMP/htdocs/server.crt"
SSLCertificateKeyFile "/Applications/MAMP/htdocs/server.key"
</VirtualHost>
Listen 443 NameVirtualHost *:443 <VirtualHost *:443>
DocumentRoot "/Applications/MAMP/htdocs"
ServerName localhost
SSLEngine on
SSLCertificateFile "/Applications/MAMP/htdocs/server.crt"
SSLCertificateKeyFile "/Applications/MAMP/htdocs/server.key"
</VirtualHost>
Step6
在重啟MAMP後,就可在127.0.0.1下開啟https啦!!