Skip to content

Commit e2660bb

Browse files
committed
update
1 parent b96356c commit e2660bb

File tree

5 files changed

+103
-54
lines changed

5 files changed

+103
-54
lines changed

web/Modules/Caddy/App/Jobs/CaddyBuild.php

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function handle(): void
3737

3838
// Attempt recovery
3939
$this->attemptRecovery();
40-
40+
4141
// Re-throw exception for job retry mechanism
4242
throw $e;
4343
}
@@ -89,15 +89,24 @@ protected function validatePrerequisites(): void
8989

9090
if (!is_writable($configDir)) {
9191
throw new \Exception("Caddy config directory is not writable: {$configDir}");
92-
}
93-
94-
// Check if log directory exists and is writable
92+
} // Check if log directory exists and is writable
9593
if (!is_dir($caddyLogPath)) {
96-
if (!mkdir($caddyLogPath, 0755, true)) {
94+
if (!mkdir($caddyLogPath, 0777, true)) {
9795
throw new \Exception("Cannot create Caddy log directory: {$caddyLogPath}");
9896
}
99-
} if (!is_writable($caddyLogPath)) {
100-
throw new \Exception("Caddy log directory is not writable: {$caddyLogPath}");
97+
// Ensure caddy user can write to the log directory with broader permissions
98+
shell_exec("chown -R caddy:caddy {$caddyLogPath}");
99+
shell_exec("chmod -R 777 {$caddyLogPath}");
100+
}
101+
102+
if (!is_writable($caddyLogPath)) {
103+
// Try to fix permissions with broader access (777) for multi-user write access
104+
shell_exec("chown -R caddy:caddy {$caddyLogPath}");
105+
shell_exec("chmod -R 777 {$caddyLogPath}");
106+
107+
if (!is_writable($caddyLogPath)) {
108+
throw new \Exception("Caddy log directory is not writable: {$caddyLogPath}");
109+
}
101110
}
102111
}
103112

@@ -200,12 +209,10 @@ protected function generateCaddyfile(): void
200209

201210
// Reload Caddy configuration
202211
shell_exec('systemctl reload caddy');
203-
}
204-
205-
private function createCaddyBlock(Domain $domain, $apacheHttpPort): ?array
212+
} private function createCaddyBlock(Domain $domain, $apacheHttpPort): ?array
206213
{
207-
if ($domain->status === Domain::STATUS_SUSPENDED ||
208-
$domain->status === Domain::STATUS_DEACTIVATED ||
214+
if ($domain->status === Domain::STATUS_SUSPENDED ||
215+
$domain->status === Domain::STATUS_DEACTIVATED ||
209216
$domain->status === Domain::STATUS_BROKEN) {
210217
return null;
211218
}
@@ -216,9 +223,7 @@ private function createCaddyBlock(Domain $domain, $apacheHttpPort): ?array
216223
'enable_ssl' => true,
217224
'enable_www' => true,
218225
];
219-
}
220-
221-
private function createMasterDomainCaddyBlock(MasterDomain $masterDomain, $apacheHttpPort): ?array
226+
} private function createMasterDomainCaddyBlock(MasterDomain $masterDomain, $apacheHttpPort): ?array
222227
{
223228
if (empty($masterDomain->domain)) {
224229
return null;
@@ -229,46 +234,47 @@ private function createMasterDomainCaddyBlock(MasterDomain $masterDomain, $apach
229234
'proxy_to' => "127.0.0.1:{$apacheHttpPort}",
230235
'enable_ssl' => true,
231236
'enable_www' => true,
232-
'is_master' => true, ];
233-
} /**
237+
'is_master' => true,
238+
];
239+
}/**
234240
* Validate generated configuration before applying
235241
*/
236242
protected function validateGeneratedConfig(): void
237243
{
238244
$caddyConfigPath = '/etc/caddy/Caddyfile';
239245
$caddyBinary = '/usr/bin/caddy';
240-
246+
241247
if (!file_exists($caddyConfigPath)) {
242248
throw new \Exception("Generated Caddyfile not found at: {$caddyConfigPath}");
243249
}
244-
250+
245251
// Format Caddyfile to fix inconsistencies if Caddy binary is available
246252
if (is_executable($caddyBinary)) {
247253
$formatCommand = "{$caddyBinary} fmt --overwrite {$caddyConfigPath} 2>&1";
248254
$formatOutput = shell_exec($formatCommand);
249255
$formatExitCode = shell_exec("echo $?");
250-
256+
251257
if (trim($formatExitCode) === '0') {
252258
\Log::info('Caddyfile formatted successfully');
253259
} else {
254260
\Log::warning('Caddyfile formatting failed: ' . $formatOutput);
255261
}
256-
262+
257263
// Validate syntax using Caddy binary
258264
$command = "{$caddyBinary} validate --config {$caddyConfigPath} 2>&1";
259265
$output = shell_exec($command);
260266
$exitCode = shell_exec("echo $?");
261-
267+
262268
if (trim($exitCode) !== '0') {
263269
throw new \Exception("Caddyfile validation failed: {$output}");
264270
}
265-
271+
266272
\Log::info('Caddyfile validation passed');
267273
} else {
268274
\Log::warning('Caddy binary not found, skipping syntax validation and formatting');
269275
}
270276
}
271-
277+
272278
/**
273279
* Apply Caddy configuration and reload service
274280
*/
@@ -277,14 +283,14 @@ protected function applyCaddyConfiguration(): void
277283
try {
278284
// Create backup of current configuration
279285
$this->backupCurrentConfig();
280-
286+
281287
// Reload Caddy service to apply new configuration
282288
$this->reloadCaddyService();
283-
289+
284290
\Log::info('Caddy configuration applied successfully');
285291
} catch (\Exception $e) {
286292
\Log::error('Failed to apply Caddy configuration: ' . $e->getMessage());
287-
293+
288294
// Restore backup on failure
289295
$this->restoreConfigBackup();
290296
throw $e;
@@ -297,12 +303,12 @@ protected function backupCurrentConfig(): void
297303
{
298304
$caddyConfigPath = '/etc/caddy/Caddyfile';
299305
$backupPath = $caddyConfigPath . '.backup.' . date('Y-m-d-H-i-s');
300-
306+
301307
if (file_exists($caddyConfigPath)) {
302308
if (!copy($caddyConfigPath, $backupPath)) {
303309
throw new \Exception("Failed to create configuration backup at: {$backupPath}");
304310
}
305-
311+
306312
\Log::info("Configuration backup created: {$backupPath}");
307313
}
308314
}
@@ -313,13 +319,13 @@ protected function restoreConfigBackup(): void
313319
{
314320
$caddyConfigPath = '/etc/caddy/Caddyfile';
315321
$backupDir = dirname($caddyConfigPath);
316-
322+
317323
// Find the most recent backup
318324
$backups = glob($backupDir . '/Caddyfile.backup.*');
319325
if (!empty($backups)) {
320326
rsort($backups); // Sort by name (newest first)
321327
$latestBackup = $backups[0];
322-
328+
323329
if (copy($latestBackup, $caddyConfigPath)) {
324330
\Log::info("Configuration restored from backup: {$latestBackup}");
325331
$this->reloadCaddyService();
@@ -328,50 +334,51 @@ protected function restoreConfigBackup(): void
328334
}
329335
}
330336
}
331-
337+
332338
/**
333339
* Reload Caddy service
334340
*/
335341
protected function reloadCaddyService(): void
336342
{
337343
$commands = [
338344
'systemctl reload caddy',
339-
'systemctl restart caddy', // Fallback if reload fails
345+
'systemctl restart caddy',
346+
340347
];
341-
348+
342349
foreach ($commands as $command) {
343350
$output = shell_exec("{$command} 2>&1");
344351
$exitCode = shell_exec("echo $?");
345-
352+
346353
if (trim($exitCode) === '0') {
347354
\Log::info("Caddy service reloaded successfully using: {$command}");
348355
return;
349356
}
350-
357+
351358
\Log::warning("Command failed: {$command}, output: {$output}");
352359
}
353-
360+
354361
throw new \Exception("Failed to reload Caddy service");
355362
}
356-
363+
357364
/**
358365
* Attempt recovery on job failure
359366
*/
360367
protected function attemptRecovery(): void
361368
{
362369
try {
363370
\Log::info('Attempting Caddy configuration recovery');
364-
371+
365372
// Try to restore from backup
366373
$this->restoreConfigBackup();
367-
374+
368375
// Check if service is still running
369376
$status = shell_exec('systemctl is-active caddy 2>/dev/null');
370377
if (trim($status) !== 'active') {
371378
\Log::warning('Caddy service is not active, attempting to start');
372379
shell_exec('systemctl start caddy 2>&1');
373380
}
374-
381+
375382
\Log::info('Recovery attempt completed');
376383
} catch (\Exception $e) {
377384
\Log::error('Recovery attempt failed: ' . $e->getMessage());
@@ -385,12 +392,12 @@ protected function cleanupOldBackups(): void
385392
$caddyConfigPath = '/etc/caddy/Caddyfile';
386393
$backupDir = dirname($caddyConfigPath);
387394
$maxBackups = 10;
388-
395+
389396
$backups = glob($backupDir . '/Caddyfile.backup.*');
390397
if (count($backups) > $maxBackups) {
391398
rsort($backups); // Sort by name (newest first)
392399
$oldBackups = array_slice($backups, $maxBackups);
393-
400+
394401
foreach ($oldBackups as $backup) {
395402
if (unlink($backup)) {
396403
\Log::info("Removed old backup: {$backup}");

web/Modules/Caddy/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ sudo chown -R caddy:caddy /etc/caddy
237237
sudo chown -R caddy:caddy /var/log/caddy
238238
sudo chown -R caddy:caddy /var/lib/caddy
239239

240-
# Fix permissions
240+
# Fix permissions (777 for log directory to allow multi-user write access)
241241
sudo chmod 755 /etc/caddy
242242
sudo chmod 644 /etc/caddy/Caddyfile
243-
sudo chmod 755 /var/log/caddy
243+
sudo chmod 777 /var/log/caddy
244244
```
245245

246246
### Health Checks

web/Modules/Caddy/resources/views/caddyfile-build.blade.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
{
22
email {{ $caddyEmail }}
33
admin off
4-
auto_https on
5-
4+
65
# Global options
76
servers {
8-
protocol {
9-
experimental_http3
10-
}
7+
protocols h1 h2 h3
118
}
129
}
1310

@@ -32,9 +29,7 @@
3229
Referrer-Policy "strict-origin-when-cross-origin"
3330
-Server
3431
-X-Powered-By
35-
}
36-
37-
# Handle OPTIONS requests for CORS
32+
} # Handle OPTIONS requests for CORS
3833
@options method OPTIONS
3934
respond @options 204
4035

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# Fix Caddy Log Directory Permissions
4+
echo "Fixing Caddy log directory permissions..."
5+
6+
# Create log directory if it doesn't exist
7+
sudo mkdir -p /var/log/caddy
8+
9+
# Set broader permissions (777) to allow write access from any user
10+
sudo chmod 777 /var/log/caddy
11+
12+
# Also ensure the caddy user owns the directory
13+
sudo chown -R caddy:caddy /var/log/caddy
14+
15+
# Set permissions for existing log files to be writable
16+
sudo find /var/log/caddy -name "*.log" -type f -exec chmod 666 {} \;
17+
18+
# Verify the permissions
19+
echo "Current permissions for /var/log/caddy:"
20+
ls -la /var/log/caddy
21+
22+
echo "Log directory permissions fixed!"
23+
24+
# Test write access
25+
sudo -u caddy touch /var/log/caddy/test-write.log
26+
if [ $? -eq 0 ]; then
27+
echo "✓ Write test successful"
28+
sudo rm -f /var/log/caddy/test-write.log
29+
else
30+
echo "✗ Write test failed"
31+
fi

web/Modules/Caddy/shell-scripts/install-caddy.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ sudo apt update -y
88
# Install required packages
99
sudo apt-get install net-tools curl -y
1010

11+
12+
sudo groupadd --system caddy
13+
14+
sudo useradd --system \
15+
--gid caddy \
16+
--create-home \
17+
--home-dir /var/lib/caddy \
18+
--shell /usr/sbin/nologin \
19+
--comment "Caddy web server" \
20+
caddy
21+
22+
1123
# Install Caddy
1224
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
1325
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
@@ -38,8 +50,12 @@ sudo chown -R caddy:caddy /var/lib/caddy
3850
sudo chown -R caddy:caddy /var/log/caddy
3951
sudo chmod 755 /etc/caddy
4052
sudo chmod 644 /etc/caddy/Caddyfile
53+
sudo chmod 777 /var/log/caddy # Allow write access from any user
54+
55+
56+
57+
4158

42-
# Enable and start Caddy service
4359
sudo systemctl enable caddy
4460
sudo systemctl start caddy
4561

0 commit comments

Comments
 (0)