前往网站首页 / 本地测试专用!使用PHP一键生成自签名泛域名SSL证书

本地测试专用!使用PHP一键生成自签名泛域名SSL证书

2026-09-07

为什么本地开发需要自建SSL证书?

做网站开发、程序本地调试的朋友应该都遇到过这个问题:很多源码、程序的运行环境强制要求 HTTPS,纯 HTTP 协议会直接报错、功能异常,甚至无法进入页面。
但本地测试场景,根本没必要购买付费SSL证书,同时也存在两个硬性问题:
一是 Let's Encrypt 这类免费证书,仅支持公网可访问的域名,内网、本地测试域名无法申请;
二是普通简易自签名证书,不支持泛域名、无SAN扩展,在新版 Chrome、Edge 浏览器中会直接拦截,无法跳过使用。
这也是我使用这款PHP证书生成代码的核心原因:
纯本地运行、零成本、支持通配符域名、兼容现代浏览器,专门解决本地开发的HTTPS环境适配问题。

工具核心优势

相比网上普通的SSL证书生成脚本,这款工具针对性优化了本地测试的痛点,核心亮点很实用:

工具使用方法

使用方式非常简单,零基础也能快速上手:
1. 将完整PHP代码保存为 ssl.php,放置在本地PHP运行环境中(phpstudy、XAMPP等均可);
2. 浏览器访问对应页面,输入需要测试的域名(支持普通域名、泛域名);
3. 点击生成按钮,程序会自动完成密钥创建、证书签名、格式导出;
4. 直接下载对应的 private.pem 私钥和 certificate.pem 证书文件,替换服务器配置即可。

常见使用说明

很多人会疑惑,自签名证书为什么浏览器会提示不安全?这里统一说明:
该证书为本地自签名证书,没有权威机构认证,所以浏览器会默认给出安全提示,这是正常现象,不影响本地测试使用。
如果遇到浏览器强制拦截、无法访问的情况,可在报错页面,在键盘上直接输入 thisisunsafe,即可跳过拦截、正常访问调试。

适用场景

本地源码调试、内网站点部署、反向代理配置、小程序/本地接口HTTPS适配、各类需要强制HTTPS环境的程序测试,所有非正式线上环境,都可以用这款工具快速生成SSL证书,省去繁琐的证书申请、配置步骤。

后续可能会根据需要做其他调整

代码示例

<?php
// SSL 生成逻辑
$generated = false;
$cert = "";
$key = "";

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['domain'])) {
    $domain = htmlspecialchars($_POST['domain']);

    // 证书信息
    $dn = [
        "commonName" => $domain,
        "organizationName" => "Self-Signed CA",
        "organizationalUnitName" => "IT Dept",
        "countryName" => "CN",
    ];

    // 配置项:支持 SHA256 和 v3 扩展 (解决 Edge/Chrome 报错关键)
    $config = [
        "digest_alg" => "sha256",
        "private_key_bits" => 2048,
        "private_key_type" => OPENSSL_KEYTYPE_RSA,
        "x509_extensions" => "v3_req"
    ];

    // 构造临时配置文件以支持 SAN (Subject Alternative Name)
    $tmpConfig = "[req]\ndistinguished_name=req_distinguished_name\nx509_extensions=v3_req\nprompt=no\n[req_distinguished_name]\nCN=$domain\n[v3_req]\nsubjectAltName=DNS:$domain";
    
    $tmpFile = tempnam(sys_get_temp_dir(), 'ssl');
    file_put_contents($tmpFile, $tmpConfig);

    // 1. 生成私钥
    $privkey = openssl_pkey_new($config);

    // 2. 生成 CSR (证书签名请求)
    $csr = openssl_csr_new($dn, $privkey, array('config' => $tmpFile));

    // 3. 自签名生成证书 (有效期 10 年)
    $sscert = openssl_csr_sign($csr, null, $privkey, 3650, array('config' => $tmpFile));

    // 4. 导出 PEM 格式文本
    openssl_x509_export($sscert, $cert);
    openssl_pkey_export($privkey, $key);

    unlink($tmpFile); // 清理临时配置
    $generated = true;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>SSL PEM 证书在线生成</title>
    <style>
        body { font-family: system-ui, -apple-system, sans-serif; background: #f0f2f5; margin: 0; padding: 40px 20px; color: #333; }
        .container { max-width: 850px; margin: 0 auto; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.08); }
        h1 { font-size: 24px; margin-bottom: 25px; color: #1a73e8; border-bottom: 2px solid #f0f2f5; padding-bottom: 15px; }
        .input-box { display: flex; gap: 10px; margin-bottom: 30px; }
        input[type="text"] { flex: 1; padding: 12px 15px; border: 1px solid #ddd; border-radius: 6px; font-size: 16px; outline: none; }
        input[type="text"]:focus { border-color: #1a73e8; box-shadow: 0 0 0 2px rgba(26,115,232,0.2); }
        button { background: #1a73e8; color: #fff; border: none; padding: 12px 25px; border-radius: 6px; font-weight: bold; cursor: pointer; }
        button:hover { background: #1557b0; }
        .label { display: block; font-weight: 600; margin: 20px 0 8px; color: #555; }
        textarea { width: 100%; height: 160px; font-family: 'Courier New', monospace; background: #f8f9fa; border: 1px solid #eaecf0; border-radius: 6px; padding: 15px; box-sizing: border-box; font-size: 13px; resize: vertical; }
        .actions { margin-bottom: 30px; }
        .download { color: #1a73e8; text-decoration: none; font-size: 14px; font-weight: 500; border: 1px solid #1a73e8; padding: 5px 12px; border-radius: 4px; transition: 0.2s; }
        .download:hover { background: #1a73e8; color: #fff; }
        .note { background: #fff4e5; border-left: 4px solid #ffab40; padding: 15px; margin-top: 20px; font-size: 14px; color: #663c00; }
    </style>
</head>
<body>

<div class="container">
    <h1>SSL PEM 证书生成工具</h1>
    
    <form method="POST">
        <div class="input-box">
            <input type="text" name="domain" placeholder="输入域名 (如: reishi.cc)" required>
            <button type="submit">生成证书</button>
        </div>
    </form>

    <?php if ($generated): ?>
        <div class="result-area">
            <span class="label">私钥内容 (Private Key):</span>
            <textarea readonly><?php echo $key; ?></textarea>
            <div class="actions">
                <a class="download" href="dаta:application/x-pem-file;charset=utf-8,<?php echo urlencode($key); ?>" download="private.pem">下载为 private.pem</a>
            </div>

            <span class="label">证书内容 (Certificate):</span>
            <textarea readonly><?php echo $cert; ?></textarea>
            <div class="actions">
                <a class="download" href="dаta:application/x-pem-file;charset=utf-8,<?php echo urlencode($cert); ?>" download="certificate.pem">下载为 certificate.pem</a>
            </div>

            <div class="note">
                <strong>💡 使用提示:</strong><br>
                由于这是自签名证书,浏览器仍会提示不安全。若遇到 HSTS 拦截无法进入,请在报错页面直接输入 <code>thisisunsafe</code> 即可。
            </div>
        </div>
    <?php endif; ?>
</div>

</body>
</html>


返回网站