Localhost mkcert Run web server with https - haiquang9994/dev_env GitHub Wiki

Localhost mkcert - Run web server with https

Help localhost run web server with https

Download

Windows

Download mkcert.exe for windows

MacOS

brew install mkcert

Script autossl

This script auto get all domain .lc of Nginx config files

#!/usr/bin/env php
<?php

$files = glob('/Users/light/Developer/vhosts/*.conf');
#$files = glob('D:\Server\nginx\vhosts\*.conf');

$domains = [];

foreach ($files as $file) {
    preg_match_all("/server_name ([^\;]*)/", file_get_contents($file), $matchs);
    $domains = array_merge($domains, array_map(function ($hostname) {
        return '"' . $hostname . '"';
    }, array_filter(explode(' ', implode(' ', $matchs[1])), function ($hostname) {
        return substr($hostname, '-3') === '.lc';
    })));
}

$cmd = 'mkcert -key-file /opt/homebrew/etc/nginx/ssl/localhost.key -cert-file /opt/homebrew/etc/nginx/ssl/localhost.crt "127.0.0.1" "localhost" ' . implode(' ', $domains);
#$cmd = 'mkcert -key-file D:/Server/nginx/ssl/localhost.key -cert-file D:/Server/nginx/ssl/localhost.crt "127.0.0.1" "localhost" ' . implode(' ', $domains);

shell_exec('mkcert -uninstall');
shell_exec('mkcert -install');
shell_exec($cmd);

Other script

#!/usr/bin/env php
<?php

$domains = ['test.lc'];

$cmd = 'mkcert -key-file /opt/homebrew/etc/nginx/ssl/localhost.key -cert-file /opt/homebrew/etc/nginx/ssl/localhost.crt "127.0.0.1" "localhost" ' . implode(' ', $domains);
#$cmd = 'mkcert -key-file D:/Server/nginx/ssl/localhost.key -cert-file D:/Server/nginx/ssl/localhost.crt "127.0.0.1" "localhost" ' . implode(' ', $domains);

shell_exec('mkcert -uninstall');
shell_exec('mkcert -install');
shell_exec($cmd);

Nginx ssl config

listen 443 ssl;
ssl_certificate "/opt/homebrew/etc/nginx/ssl/localhost.crt";
ssl_certificate_key "/opt/homebrew/etc/nginx/ssl/localhost.key";
#ssl_certificate "D:/Server/nginx/ssl/localhost.crt";
#ssl_certificate_key "D:/Server/nginx/ssl/localhost.key";

Apache2 ssl config

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile "/opt/homebrew/etc/nginx/ssl/localhost.crt"
    SSLCertificateKeyFile "/opt/homebrew/etc/nginx/ssl/localhost.key"
    #SSLCertificateFile "D:/Server/nginx/ssl/localhost.crt"
    #SSLCertificateKeyFile "D:/Server/nginx/ssl/localhost.key"
</VirtualHost>