implementing discovering static sites
This commit is contained in:
2 files changed
+44
-14
No files matched your search
@@ -2,16 +2,18 @@
|
||||
|
||||
Local HTTPS router for PHP and Laravel projects.
|
||||
|
||||
`php-dev-router` discovers PHP apps in a projects directory, generates nginx
|
||||
virtual hosts, adds exact local host records to `/etc/hosts`, and can run as a
|
||||
small systemd watcher so newly added projects become available automatically.
|
||||
`php-dev-router` discovers PHP apps and static HTML sites in a projects
|
||||
directory, generates nginx virtual hosts, adds exact local host records to
|
||||
`/etc/hosts`, and can run as a small systemd watcher so newly added projects
|
||||
become available automatically.
|
||||
|
||||
It is built for development workstations, not production servers.
|
||||
|
||||
## Features
|
||||
|
||||
- Detects Laravel apps, public front-controller apps, and generic PHP apps.
|
||||
- Generates HTTPS nginx server blocks backed by PHP-FPM.
|
||||
- Detects Laravel apps, public front-controller apps, generic PHP apps, and
|
||||
static HTML sites.
|
||||
- Generates HTTPS nginx server blocks, with PHP-FPM routing where needed.
|
||||
- Maps only detected project hostnames to `127.0.0.1`.
|
||||
- Avoids wildcard DNS hijacking, so unrelated real domains keep resolving
|
||||
normally.
|
||||
@@ -23,7 +25,7 @@ It is built for development workstations, not production servers.
|
||||
- Linux with systemd
|
||||
- PHP 8.2 or newer
|
||||
- nginx
|
||||
- PHP-FPM
|
||||
- PHP-FPM for PHP apps
|
||||
- A local or trusted development certificate/key for the configured domain
|
||||
|
||||
The default example config uses `php.test`. Browsers will still require a
|
||||
@@ -57,7 +59,7 @@ Start from `config.example.json`.
|
||||
|
||||
Important options:
|
||||
|
||||
- `projects_root`: directory that contains your PHP projects.
|
||||
- `projects_root`: directory that contains your PHP projects and static sites.
|
||||
- `domain`: suffix used for generated hostnames.
|
||||
- `ssl_certificate` / `ssl_certificate_key`: certificate used by nginx.
|
||||
- `php_fpm_socket`: PHP-FPM socket passed to nginx.
|
||||
@@ -88,6 +90,9 @@ installs should let the tool run `nginx -t` before reload.
|
||||
- Laravel app: `artisan` and `public/index.php`, served from `public`.
|
||||
- Public front-controller app: `public/index.php`, served from `public`.
|
||||
- Generic PHP app: `index.php`, served from that directory.
|
||||
- Public static HTML site: `public/index.html` or `public/index.htm`, served
|
||||
from `public`.
|
||||
- Static HTML site: `index.html` or `index.htm`, served from that directory.
|
||||
|
||||
Ignored directories include hidden/tooling directories, `vendor`, `node_modules`,
|
||||
`storage`, and common IDE/cache paths.
|
||||
|
||||
+32
-7
@@ -106,7 +106,7 @@ Usage:
|
||||
php-dev-router install [--dry-run]
|
||||
|
||||
Commands:
|
||||
scan Discover PHP/Laravel apps and show hostnames.
|
||||
scan Discover PHP/Laravel/static apps and show hostnames.
|
||||
apply Generate nginx config and exact local hosts entries.
|
||||
watch Re-apply when the project inventory changes.
|
||||
doctor Check config, certs, services, generated files, and DNS.
|
||||
@@ -124,7 +124,7 @@ function commandScan(array $config, array $options): void
|
||||
}
|
||||
|
||||
if ($apps === []) {
|
||||
echo "No serveable PHP apps found in {$config['projects_root']}\n";
|
||||
echo "No serveable PHP or static HTML apps found in {$config['projects_root']}\n";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -252,13 +252,14 @@ function discoverApps(array $config): array
|
||||
$host = "{$slug}-{$hash}.{$config['domain']}";
|
||||
}
|
||||
$usedHosts[$host] = true;
|
||||
$indexFile = $candidate['index'] ?? 'index.php';
|
||||
$apps[] = [
|
||||
'host' => $host,
|
||||
'name' => $slug,
|
||||
'type' => $candidate['type'],
|
||||
'app_dir' => $candidate['app_dir'],
|
||||
'root' => $candidate['root'],
|
||||
'index' => $candidate['root'] . '/index.php',
|
||||
'index' => $candidate['root'] . "/{$indexFile}",
|
||||
];
|
||||
}
|
||||
|
||||
@@ -275,9 +276,13 @@ function scanDirectory(string $dir, string $base, int $depth, array $config, arr
|
||||
$dir = rtrim($dir, '/');
|
||||
if (is_file("{$dir}/public/index.php")) {
|
||||
$type = is_file("{$dir}/artisan") ? 'laravel' : 'public-front-controller';
|
||||
$candidates[] = ['app_dir' => $dir, 'root' => "{$dir}/public", 'type' => $type];
|
||||
$candidates[] = ['app_dir' => $dir, 'root' => "{$dir}/public", 'type' => $type, 'index' => 'index.php'];
|
||||
} elseif (is_file("{$dir}/index.php")) {
|
||||
$candidates[] = ['app_dir' => $dir, 'root' => $dir, 'type' => 'generic-php'];
|
||||
$candidates[] = ['app_dir' => $dir, 'root' => $dir, 'type' => 'generic-php', 'index' => 'index.php'];
|
||||
} elseif ($index = firstExistingFile($dir, ['public/index.html', 'public/index.htm'])) {
|
||||
$candidates[] = ['app_dir' => $dir, 'root' => "{$dir}/public", 'type' => 'public-static-html', 'index' => basename($index)];
|
||||
} elseif ($index = firstExistingFile($dir, ['index.html', 'index.htm'])) {
|
||||
$candidates[] = ['app_dir' => $dir, 'root' => $dir, 'type' => 'static-html', 'index' => basename($index)];
|
||||
}
|
||||
|
||||
$children = @scandir($dir);
|
||||
@@ -315,6 +320,17 @@ function shouldSkip(string $path, string $base, array $skipDirs): bool
|
||||
return false;
|
||||
}
|
||||
|
||||
function firstExistingFile(string $dir, array $files): ?string
|
||||
{
|
||||
foreach ($files as $file) {
|
||||
if (is_file("{$dir}/{$file}")) {
|
||||
return $file;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function renderNginx(array $config, array $apps): string
|
||||
{
|
||||
$lines = [
|
||||
@@ -329,6 +345,7 @@ function renderNginx(array $config, array $apps): string
|
||||
$cert = nginxEscape($config['ssl_certificate']);
|
||||
$key = nginxEscape($config['ssl_certificate_key']);
|
||||
$socket = nginxEscape($config['php_fpm_socket']);
|
||||
$isStatic = isStaticApp($app);
|
||||
|
||||
$lines[] = "server {";
|
||||
$lines[] = " listen 80;";
|
||||
@@ -344,13 +361,15 @@ function renderNginx(array $config, array $apps): string
|
||||
$lines[] = " ssl_certificate_key \"{$key}\";";
|
||||
$lines[] = "";
|
||||
$lines[] = " root \"{$root}\";";
|
||||
$lines[] = " index index.php index.html;";
|
||||
$lines[] = $isStatic ? " index index.html index.htm;" : " index index.php index.html index.htm;";
|
||||
$lines[] = "";
|
||||
$lines[] = " client_max_body_size 128m;";
|
||||
$lines[] = "";
|
||||
$lines[] = " location / {";
|
||||
$lines[] = " try_files \$uri \$uri/ /index.php?\$query_string;";
|
||||
$lines[] = $isStatic ? " try_files \$uri \$uri/ =404;" : " try_files \$uri \$uri/ /index.php?\$query_string;";
|
||||
$lines[] = " }";
|
||||
|
||||
if (!$isStatic) {
|
||||
$lines[] = "";
|
||||
$lines[] = " location ~ \\.php$ {";
|
||||
$lines[] = " try_files \$uri =404;";
|
||||
@@ -358,6 +377,7 @@ function renderNginx(array $config, array $apps): string
|
||||
$lines[] = " fastcgi_index index.php;";
|
||||
$lines[] = " include fastcgi.conf;";
|
||||
$lines[] = " }";
|
||||
}
|
||||
$lines[] = "";
|
||||
$lines[] = " location ~ /\\.(?!well-known).* {";
|
||||
$lines[] = " deny all;";
|
||||
@@ -369,6 +389,11 @@ function renderNginx(array $config, array $apps): string
|
||||
return implode("\n", $lines);
|
||||
}
|
||||
|
||||
function isStaticApp(array $app): bool
|
||||
{
|
||||
return in_array($app['type'], ['public-static-html', 'static-html'], true);
|
||||
}
|
||||
|
||||
function renderHostsBlock(array $apps): string
|
||||
{
|
||||
$lines = [HOSTS_BEGIN];
|
||||
|
||||
Reference in new issue
Block a user