Skip to content

Instantly share code, notes, and snippets.

@mklooss
Last active October 6, 2021 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklooss/00548843ed29a54e5237933f173d3117 to your computer and use it in GitHub Desktop.
Save mklooss/00548843ed29a54e5237933f173d3117 to your computer and use it in GitHub Desktop.
Create Based on CertDomains and GoogleDNS the Cert, when ur moving an project
<?php
$hostip = '999.999.999.999';
$certName = 'mostly.server.hostname.tld';
$domainsList = array(
'domain1.tld',
'domain2.tld',
);
$certdns = trim(shell_exec('openssl x509 -noout -text -in /etc/letsencrypt/live/'.$certName.'/cert.pem | grep DNS:'));
$certdns = str_replace('DNS:', '', $certdns);
$certdns = array_map('trim', array_filter((array)explode(',', $certdns)));
$domains = [$certName];
foreach ($domainsList as $domain)
{
$domains[] = $domain;
$domains[] = 'www.'.$domain;
}
$new = false;
foreach ($domains as $domain)
{
if (in_array($domain, $certdns))
{
continue;
}
// tail -1 may should be changed to an other value
// just a hack to get the cname ip!
$ip = trim(shell_exec('dig +short A '.$domain.' @8.8.8.8 | tail -1'));
if ($ip == $hostip) {
$certdns[] = $domain;
$new = true;
}
}
if ($new)
{
echo shell_exec('/etc/init.d/nginx stop');
echo "\n";
$certdns = array_unique($certdns);
$comandList = ' -d '.implode(' -d ', $certdns);
echo shell_exec('letsencrypt certonly --standalone --noninteractive --expand --cert-name '.$certName.' '.$comandList);
echo "\n";
echo shell_exec('/etc/init.d/nginx restart');
echo "\n";
} else {
echo "nothing todo\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment