Last active
October 16, 2024 08:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$hostips = array('999.999.999.999', '888.888.888.888'); | |
$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! | |
foreach ($hostips as $hostip) | |
{ | |
$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