Skip to content

Commit e7956f8

Browse files
authored
Update build.php
1 parent 2942c93 commit e7956f8

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

src/tasks/build.php

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
*/
2222

2323
if (!defined('DEBUG')) {
24-
//define('DEBUG', !empty($this->task->config['debug']));
25-
26-
define('DEBUG', true);
24+
// define('DEBUG', !empty($this->task->config['debug']));
25+
define('DEBUG', true);
2726
}
2827

2928
if (!defined('TMP_DIR')) {
@@ -87,12 +86,16 @@ public function build($routes)
8786
$restart_nginx = true;
8887

8988
$this->path = "/etc/nginx/proxied/servers/".basename($row['name']);
89+
90+
echo DEBUG ? $this->log('NGINX path: '.$this->path) : null;
9091

9192
// check for config folder
9293
if (!file_exists($this->path)) {
93-
echo DEBUG ? $this->log('Create config directory: '.$this->path) : null;
94+
echo DEBUG ? $this->log('Create config directory (CREATE): '.$this->path) : null;
9495
mkdir($this->path, 0755, true);
95-
}
96+
} else {
97+
echo DEBUG ? $this->log('Create config directory (EXISTS): '.$this->path) : null;
98+
}
9699

97100
// build nginx configs
98101
if (!$this->config_upstream($row)) {
@@ -123,18 +126,7 @@ public function build($routes)
123126
// reload nginx if config test passed
124127
if ($restart_nginx === true) {
125128
if ($this->config_test_nginx()) {
126-
exec('/usr/sbin/nginx -s reload 2>&1', $out, $exit_code);
127-
128-
echo DEBUG ? $this->log('Reloading NGINX: [exit code: '.$exit_code.']: '.print_r($out, true)) : null;
129-
130-
// out is not empty
131-
if (!empty($out)) {
132-
echo DEBUG ? $this->log('NGINX was reloaded with warnings! Some routes may be effected.') : null;
133-
}
134-
// all is good
135-
else {
136-
echo DEBUG ? $this->log('NGINX was reloaded.') : null;
137-
}
129+
$this->reload_nginx();
138130
} else {
139131
echo DEBUG ? $this->log('NGINX was not reloaded.') : null;
140132
}
@@ -154,6 +146,25 @@ public function config_test_nginx()
154146
echo DEBUG ? $this->log('Testing NGINX config: [exit code: '.$exit_code.']: '.print_r($out, true)) : null;
155147
return ($exit_code == 0);
156148
}
149+
150+
/**
151+
*
152+
*/
153+
public function reload_nginx()
154+
{
155+
exec('/usr/sbin/nginx -s reload 2>&1', $out, $exit_code);
156+
157+
echo DEBUG ? $this->log('Reloading NGINX: [exit code: '.$exit_code.']: '.print_r($out, true)) : null;
158+
159+
// out is not empty
160+
if (!empty($out)) {
161+
echo DEBUG ? $this->log('NGINX was reloaded with warnings! Some routes may be effected.') : null;
162+
}
163+
// all is good
164+
else {
165+
echo DEBUG ? $this->log('NGINX was reloaded.') : null;
166+
}
167+
}
157168

158169
/**
159170
*
@@ -163,11 +174,13 @@ public function config_http($row)
163174
echo DEBUG ? $this->log('Building NGINX HTTP server config.') : null;
164175

165176
$domains = [];
166-
foreach ($row->ownDomain as $domain) {
177+
foreach ($row->with("ORDER BY LENGTH(name) ASC")->ownDomain as $domain) {
167178
$domains[] = $domain->name;
168179
}
180+
181+
echo DEBUG ? $this->log('Domains: '.print_r($domains, true)) : null;
169182

170-
return file_put_contents(
183+
$bytes_written = file_put_contents(
171184
$this->path.'/http.conf',
172185
'# Auto generated on '.date_create()->format('Y-m-d H:i:s').'
173186
# Do not edit this file as any changes will be overwritten!
@@ -214,6 +227,13 @@ public function config_http($row)
214227
}
215228
'
216229
);
230+
231+
if ($bytes_written && $this->config_test_nginx()) {
232+
$this->reload_nginx();
233+
sleep(1);
234+
}
235+
236+
return $bytes_written;
217237
}
218238

219239
/**
@@ -234,9 +254,11 @@ public function config_https($row)
234254
echo DEBUG ? $this->log('Building NGINX HTTPS server config.') : null;
235255

236256
$domains = [];
237-
foreach ($row->ownDomain as $domain) {
257+
foreach ($row->with("ORDER BY LENGTH(name) ASC")->ownDomain as $domain) {
238258
$domains[] = $domain->name;
239259
}
260+
261+
echo DEBUG ? $this->log('Domains: '.print_r($domains, true)) : null;
240262

241263
// check certs for letsencrypt
242264
if (isset($row['ssl_type']) && $row['ssl_type'] == 'letsencrypt') {
@@ -331,9 +353,7 @@ public function config_https($row)
331353

332354
try {
333355
//
334-
$ac = new ACMECert(false);
335-
336-
// crurrentcersts are not v2 but are acme-01, so need to delete
356+
$ac = new ACMECert();
337357

338358
if (!is_file(LETS_ENCRYPT_CERTS_PATH.'/_account/private.pem')) {
339359
echo DEBUG ? $this->log('Starting new account registration.') : null;
@@ -357,7 +377,7 @@ public function config_https($row)
357377
$domain_config = [];
358378
foreach ($domains as $key => $value) {
359379
$domain_config[$value] = [
360-
'challenge' => 'http-01', 'docroot' => '/usr/share/nginx/html/letsencrypt/'.$value
380+
'challenge' => 'http-01', 'docroot' => '/usr/share/nginx/html/letsencrypt/'.$domains[0]
361381
];
362382
}
363383

@@ -621,8 +641,17 @@ public function getAccountID(){
621641
return $this->kid_header['kid'];
622642
}
623643

624-
public function log($txt){
625-
error_log($txt);
644+
public function log($message){
645+
echo DEBUG ? " - ".$message."\n" : null;
646+
if (LOG) {
647+
if (!file_exists(TMP_DIR.'/logs')) {
648+
mkdir(TMP_DIR.'/logs', 0755, true);
649+
}
650+
$log = '['.date("c").'] '.$message.PHP_EOL;
651+
file_put_contents(TMP_DIR.'/logs/nginx.'.date("d-m-Y").'.txt', $log, FILE_APPEND);
652+
653+
shell_exec('chown www-data:www-data '.TMP_DIR.'/logs -R');
654+
}
626655
}
627656

628657
protected function get_openssl_error(){

0 commit comments

Comments
 (0)