1. Go to this page and download the library: Download skoerfgen/acmecert library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
skoerfgen / acmecert example snippets
$handler=function($opts){
// Write code to setup the challenge token here.
// Return a function that gets called when the challenge token should be removed again:
return function($opts){
// Write code to remove previously setup challenge token.
};
};
$ac->getCertificateChain(..., ..., $handler);
$ac->loadAccountKey('file://'.'account_key.pem');
$domain_config=array(
'example.com'=>array('challenge'=>'http-01','docroot'=>'/var/www/vhosts/example.com'),
'*.example.com'=>array('challenge'=>'dns-01'),
'test.example.org'=>array('challenge'=>'tls-alpn-01')
);
$handler=function($opts) use ($ac){
switch($opts['config']['challenge']){
case 'http-01': // automatic example: challenge directory/file is created..
$fn=$opts['config']['docroot'].$opts['key'];
@mkdir(dirname($fn),0777,true);
file_put_contents($fn,$opts['value']);
return function($opts) use ($fn){ // ..and removed after validation completed
unlink($fn);
};
break;
case 'dns-01': // manual example:
echo 'Create DNS-TXT-Record '.$opts['key'].' with value '.$opts['value']."\n";
readline('Ready?');
return function($opts){
echo 'Remove DNS-TXT-Record '.$opts['key'].' with value '.$opts['value']."\n";
};
break;
case 'tls-alpn-01':
$cert=$ac->generateALPNCertificate('file://'.'some_private_key.pem',$opts['domain'],$opts['value']);
// Use $cert and some_private_key.pem(<- does not have to be a specific key,
// just make sure you generated one) to serve the certificate for $opts['domain']
// This example uses an included ALPN Responder - a standalone https-server
// written in a few lines of node.js - which is able to complete this challenge.
// store the generated verification certificate to be used by the ALPN Responder.
file_put_contents('alpn_cert.pem',$cert);
// To keep this example simple, the included Example ALPN Responder listens on port 443,
// so - for the sake of this example - you have to stop the webserver here, like:
shell_exec('/etc/init.d/apache2 stop');
// Start ALPN Responder (
$chains=$ac->getCertificateChains('file://'.'cert_private_key.pem',$domain_config,$handler);
if (isset($chains['ISRG Root X1'])){ // use alternate chain 'ISRG Root X1'
$fullchain=$chains['ISRG Root X1'];
}else{ // use default chain if 'ISRG Root X1' is not present
$fullchain=reset($chains);
}
file_put_contents('fullchain.pem',$fullchain);
$ret=$ac->getARI('file://'.'fullchain.pem',$ari_cert_id);
if ($ret['suggestedWindow']['start']-time()>0) {
die('Certificate still good, exiting..');
}
$settings=array(
'replaces'=>$ari_cert_id
);
$ac->getCertificateChain(..., ..., ..., $settings);
$percent=$ac->getRemainingPercent('file://'.'fullchain.pem'); // certificate or certificate-chain
if ($precent>33.333) { // certificate has still more than 1/3 (33.333%) of its lifetime left
die('Certificate still good, exiting..');
}
// get new certificate here..
$days=$ac->getRemainingDays('file://'.'fullchain.pem'); // certificate or certificate-chain
if ($days>30) { // renew 30 days before expiry
die('Certificate still good, exiting..');
}
// get new certificate here..
use skoerfgen\ACMECert\ACME_Exception;
try {
echo $ac->getAccountID().PHP_EOL;
}catch(ACME_Exception $e){
if ($e->getType()=='urn:ietf:params:acme:error:accountDoesNotExist'){
echo 'Account does not exist'.PHP_EOL;
}else{
throw $e; // another error occured
}
}
try {
$cert=$ac->getCertificateChain('file://'.'cert_private_key.pem',$domain_config,$handler);
} catch (\skoerfgen\ACMECert\ACME_Exception $e){
$ac->log($e->getMessage()); // log original error
foreach($e->getSubproblems() as $subproblem){
$ac->log($subproblem->getMessage()); // log sub errors
}
}
public ACMECert::__construct ( string $ca_url = 'https://acme-v02.api.letsencrypt.org/directory' )
public string ACMECert::generateRSAKey ( int $bits = 2048 )
public string ACMECert::generateECKey ( string $curve_name = 'P-384' )
public void ACMECert::loadAccountKey ( mixed $account_key_pem )