在 macOS 的本地主机上设置 HTTPS [mac os catalina 10.15.2]

4 https localhost

cd ~/
mkdir .localhost-ssl

sudo openssl genrsa -out ~/.localhost-ssl/localhost.key 2048

sudo openssl req -new -x509 -key ~/.localhost-ssl/localhost.key -out ~/.localhost-ssl/localhost.crt -days 3650 -subj /CN=localhost

sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ~/.localhost-ssl/localhost.crt

npm install -g http-server
echo " 
function https-server() {
  http-server --ssl --cert ~/.localhost-ssl/localhost.crt --key ~/.localhost-ssl/localhost.key
}
" >> ~/.bash_profile

source ~/.bash_profile

echo "You're ready to use https on localhost "
echo "Navigate to a project directory and run:"
echo ""
echo "https-server"
Run Code Online (Sandbox Code Playgroud)

它不起作用...这段代码有什么问题吗?

代码来源:https://gist.github.com/jonsamp/587b78b7698be7c7fd570164a586e6b7

Cri*_*ris 5

经过几天的努力,我终于找到了一种使用 https 运行 localhost 的方法。\ xc2 \xa0我猜这个解决方案适用于任何 macOS 版本。\n \n来自:\xc2\xa0 https://letsencrypt.org/docs/ certificates-for-localhost/ \n \n我发现这个minica工具是一个简单的 CA,旨在用于 CA 操作员还操作将使用证书的每个主机的情况。当要求生成证书时,它会自动生成密钥和证书。它不提供 OCSP 或 CRL 服务。例如,Minica 适用于为 RPC 系统或微服务生成证书。



\n

https://github.com/jsha/minica

\n

步骤1

\n
brew install minica\n
Run Code Online (Sandbox Code Playgroud)\n

第2步

\n
minica --domains localhost\n
Run Code Online (Sandbox Code Playgroud)\n
\n

在 minica-key.pem 和 minica.pem 中生成根密钥和证书,然后\n生成并签署最终实体密钥和证书,将它们存储在\n./localhost/

\n在此输入图像描述

\n
\n

步骤3

\n
\n

将这些证书和密钥文件添加到您的配置服务器文件中。例如配置在:/etc/apache2/extra/httpd-ssl.conf

\n
\n
<VirtualHost _default_:443>\n    DocumentRoot "/Volumes/WORK/www/webapp"\n    ServerName localhost\n\n    SSLEngine on\n    SSLCertificateFile /Users/xxxx/selfsigned-certs/localhost/cert.pem\n    SSLCertificateKeyFile /Users/xxxx/selfsigned-certs/localhost/key.pem\n\n    <Directory "/Volumes/WORK/www/webapp">\n        Options All\n        MultiviewsMatch Any\n        AllowOverride All\n        Require all granted\n        Order allow,deny\n        Allow from all\n    </Directory>\n</VirtualHost>\n
Run Code Online (Sandbox Code Playgroud)\n

步骤4

\n
\n

重启服务器

\n
\n

步骤5

\n
\n

在钥匙串访问 ---> 文件 ---> 导入项目\n导入“minica.pem”文件

\n

在此输入图像描述

\n
\n

步骤6

\n
\n

在 Chrome 浏览器中,当访问 https://localhost 时,您将收到一条带有不可信证书的消息\n要使其成为可信证书,您必须转到:
\n chrome://settings/security -->“管理证书”
\n来自打开“minica...”证书并将其设置为受信任

\n

在此输入图像描述

\n
\n

步骤7

\n
\n

重新打开 https://localhost ---> 它将把证书识别为有效且受信任的证书\n在此输入图像描述

\n

在此输入图像描述

\n
\n
\n编码愉快!\n