servnest/public/ht/le.php

51 lines
2.0 KiB
PHP
Raw Normal View History

<?php require "../../common/html.php"; ?>
2021-02-17 22:48:49 +01:00
<p>
2022-04-18 16:05:00 +02:00
Installer un certificat Let's Encrypt
2021-02-17 22:48:49 +01:00
</p>
2021-01-22 21:58:46 +01:00
2021-02-17 22:48:49 +01:00
<form method="post">
2022-04-18 16:05:00 +02:00
<label for="domain">Domaine ciblé</label><br>
<select required="" name="domain">
<option value="" disabled="" selected="">---</option>
<?php
if (isset($_SESSION['username'])) {
2022-04-18 16:05:00 +02:00
$sites = selectSites($_SESSION['username'], "dns", "http", false);
$leAvailable = selectSites($_SESSION['username'], "dns", "http", true);
foreach ($sites as $site)
echo '<option value="' . $site['domain'] . '">' . $site['domain'] . " (/" . $site['siteDir'] . ")</option>";
}
?>
2022-04-18 16:05:00 +02:00
</select>
<br>
<input value="Valider" type="submit">
2021-02-17 22:48:49 +01:00
</form>
2021-01-22 21:58:46 +01:00
2021-02-17 22:48:49 +01:00
<?php
2021-01-22 21:58:46 +01:00
switchToFormProcess();
2021-08-05 14:04:33 +02:00
exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['certbot_path'] . " certonly --dry-run --test-cert --webroot --webroot-path /srv/acme --register-unsafely-without-email --agree-tos --domain " . $_POST['domain'], $output, $returnCode);
2021-10-03 18:03:08 +02:00
// Log Certbot response
addNiverLog($_SESSION['username'] . " installed a Let's Encrypt certificate on their site", $output, $returnCode);
2021-10-03 18:03:08 +02:00
// Abort if Certbot failed
if ($returnCode !== 0)
serverError("Certbot failed to get a Let's Encrypt certificate.");
2021-10-03 18:03:08 +02:00
// Replace self-signed certificate by Let's Encrypt certificate in Nginx configuration
$conf = file_get_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['domain'] . ".conf");
$conf = preg_replace("#/etc/ssl/certs/niver\.crt#", "/etc/letsencrypt/live/" . $_POST['domain'] . "/fullchain.pem", $conf);
$conf = preg_replace("#/etc/ssl/private/niver\.key#", "/etc/letsencrypt/live/" . $_POST['domain'] . "/privkey.pem", $conf);
file_put_contents(CONF['ht']['nginx_config_path'] . "/" . $_POST['domain'] . ".conf", $conf);
2021-01-22 21:58:46 +01:00
// Reload Nginx
exec(CONF['ht']['sudo_path'] . " " . CONF['ht']['systemctl_path'] . " reload nginx", $output, $returnCode);
2021-10-03 18:03:08 +02:00
// Abort if Nginx reload failed
if ($returnCode !== 0)
serverError("Nginx configuration reload failed.");
2021-01-22 21:58:46 +01:00
success("La connexion avec votre site utilise désomais un certificat TLS émis par Let's Encrypt.");