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

3
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"

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

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


很好的话题,如果你在问题中添加更多相关的标签,比如shellbashsslssl-certificatenpm等,格式化代码部分得当一些,展示你遇到的错误和日志,而不是简单地说“它不工作”。 - endo64
感谢您的回复。实际上没有错误。在完成并提到的所有终端操作之后,它应该在我的本地主机上显示“连接已安全”,但仍然显示“您与此站点的连接不安全”。我错过了什么吗?我按照以下步骤进行操作:- https://medium.com/@jonsamp/how-to-set-up-https-on-localhost-for-macos-b597bcf935ee - user13575758
1个回答

5

经过几天的努力,我终于找到了一种方法来运行具有https的localhost。我想这个解决方案适用于任何macOS版本。

引用自:https://letsencrypt.org/docs/certificates-for-localhost/

我发现了这个minica工具,它是一个简单的CA,旨在用于CA操作员也操作每个将使用证书的主机的情况。当要求生成证书时,它会自动生成密钥和证书。它不提供OCSP或CRL服务。例如,Minica适用于为RPC系统或微服务生成证书。

https://github.com/jsha/minica

步骤1

brew install minica

Step 2(第二步)
minica --domains localhost

请按照以下要求进行翻译:
Generate a root key and cert in minica-key.pem, and minica.pem, then generate and sign an end-entity key and cert, storing them in ./localhost/
在minica-key.pem和minica.pem中生成根密钥和证书,然后生成并签署终端实体密钥和证书,并将它们存储在./localhost/中。 enter image description here 添加这些证书和密钥文件到你的配置服务器文件。例如,在/etc/apache2/extra/httpd-ssl.conf中进行配置。
<VirtualHost _default_:443>
    DocumentRoot "/Volumes/WORK/www/webapp"
    ServerName localhost

    SSLEngine on
    SSLCertificateFile /Users/xxxx/selfsigned-certs/localhost/cert.pem
    SSLCertificateKeyFile /Users/xxxx/selfsigned-certs/localhost/key.pem

    <Directory "/Volumes/WORK/www/webapp">
        Options All
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

第四步

重启服务器

第五步

在钥匙串访问中,选择 "文件" ---> "导入项目" 导入 "minica.pem" 文件

enter image description here

第六步

在 Chrome 浏览器中访问 https://localhost 时,您将收到一个不受信任的证书消息 要使其成为受信任的证书,您必须前往:
chrome://settings/security --> "管理证书"
从那里打开 "minica..." 证书并将其设置为可信任

enter image description here

第七步

重新打开 https://localhost ---> 它将识别证书为有效和受信任的证书 enter image description here

enter image description here



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接