首先生成自签名证书,使用OpenSSL创建私钥、证书请求和自签证书;然后配置Apache或Nginx的SSL模块,指定证书和私钥路径;最后将证书导入系统受信任根证书颁发机构,实现本地HTTPS安全访问。

在本地开发环境中使用HTTPS,需要一个SSL证书。自签名证书是快速实现这一目标的方式,尤其适合测试和调试。一键PHP环境(如phpstudy、XAMPP、WampServer等)通常内置了Apache或Nginx,我们可以借助OpenSSL工具生成自签名证书并配置到服务中。
大多数一键PHP环境已经自带OpenSSL,检查方法如下:
在项目目录或conf/ssl目录下执行以下命令:
# 1. 生成私钥(key)
openssl genrsa -out localhost.key 2048
<h1>2. 生成证书请求文件(csr),填写本地信息即可</h1><p>openssl req -new -key localhost.key -out localhost.csr -subj "/C=CN/ST=Guangdong/L=Shenzhen/O=DevOps/CN=localhost"</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p><h1>3. 自签名生成证书(crt),有效期365天</h1><p>openssl x509 -req -days 365 -in localhost.csr -signkey localhost.key -out localhost.crt</p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/1960">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680456053464.png" alt="小羊标书">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/1960">小羊标书</a>
                            <p>一键生成百页标书,让投标更简单高效</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="小羊标书">
                                <span>62</span>
                            </div>
                        </div>
                        <a href="/ai/1960" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="小羊标书">
                        </a>
                    </div>
                完成后你会得到三个文件:localhost.key、localhost.csr、localhost.crt,其中key和crt是配置所需。
以Apache为例(常见于XAMPP、phpstudy):
SSLCertificateFile "C:/phpstudy_pro/localhost.crt"
SSLCertificateKeyFile "C:/phpstudy_pro/localhost.key"
    对于Nginx(部分版本支持),在server块中添加:
listen 443 ssl; ssl_certificate C:/path/to/localhost.crt; ssl_certificate_key C:/path/to/localhost.key;
访问 https://localhost 时,浏览器会提示“不安全”,因为证书是自签的。可手动信任:
基本上就这些。生成自签名证书不复杂,关键是路径配置正确,并让系统信任它。本地开发足够用了。
以上就是一键PHP环境如何生成自签名证书_本地HTTPS证书创建的详细内容,更多请关注php中文网其它相关文章!
                        
                        PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号