add apache configs from modules

This commit is contained in:
Bozhidar Slaveykov 2024-04-04 19:30:22 +03:00
parent fb48e43f80
commit 4a82572934
14 changed files with 162 additions and 17 deletions

View file

@ -6,6 +6,7 @@ use App\Events\HostingAccountIsCreated;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Modules\LetsEncrypt\LetsEncryptApacheVirtualHostConfig;
use Modules\LetsEncrypt\Listeners\HostingAccountIsCreatedListener;
use Modules\LetsEncrypt\Providers\Filament\AdminPanelProvider;
@ -37,6 +38,9 @@ class LetsEncryptServiceProvider extends ServiceProvider
{
$this->app->register(RouteServiceProvider::class);
$this->app->register(AdminPanelProvider::class);
app()->virtualHostManager->registerConfig(LetsEncryptApacheVirtualHostConfig::class, $this->moduleNameLower);
}
/**

View file

@ -0,0 +1,13 @@
<?php
namespace Modules\LetsEncrypt;
use App\VirtualHosts\ApacheVirtualHostConfigBase;
class LetsEncryptApacheVirtualHostConfig extends ApacheVirtualHostConfigBase
{
public array $phpAdminValueOpenBaseDirs = [
'/usr/share/letsencrypt'
];
}

View file

@ -3,10 +3,12 @@
namespace Modules\Microweber\App\Providers;
use App\Events\HostingAccountIsCreated;
use App\VirtualHosts\ApacheVirtualHostManager;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Modules\Microweber\Listeners\HostingAccountIsCreatedListener;
use Modules\Microweber\MicroweberApacheVirtualHostConfig;
class MicroweberServiceProvider extends ServiceProvider
{
@ -35,6 +37,8 @@ class MicroweberServiceProvider extends ServiceProvider
public function register(): void
{
$this->app->register(RouteServiceProvider::class);
app()->virtualHostManager->registerConfig(MicroweberApacheVirtualHostConfig::class, $this->moduleNameLower);
}
/**

View file

@ -0,0 +1,13 @@
<?php
namespace Modules\Microweber;
use App\VirtualHosts\ApacheVirtualHostConfigBase;
class MicroweberApacheVirtualHostConfig extends ApacheVirtualHostConfigBase
{
public array $phpAdminValueOpenBaseDirs = [
'/usr/share/microweber'
];
}

View file

@ -6,5 +6,6 @@ return [
'app'=>'/usr/share/microweber/latest',
'modules'=>'/usr/share/microweber/latest/userfiles/modules',
'templates'=>'/usr/share/microweber/latest/userfiles/templates'
]
],
];

View file

@ -4,6 +4,7 @@ namespace App\Actions;
use App\FileManagerApi;
use App\ShellApi;
use App\VirtualHosts\ApacheVirtualHostManager;
class ApacheWebsiteCreate
{
@ -11,6 +12,8 @@ class ApacheWebsiteCreate
public $user;
public $email;
public $password;
public $additionalServices = [];
public $features = [];
public function setDomain($domain)
{
@ -32,6 +35,16 @@ class ApacheWebsiteCreate
$this->password = $password;
}
public function setAdditionalServices($additionalServices)
{
$this->additionalServices = $additionalServices;
}
public function setFeatures($features)
{
$this->features = $features;
}
public function handle()
{
@ -54,6 +67,8 @@ class ApacheWebsiteCreate
$domainPublic = $domainRoot . '/public_html';
$homeRoot = '/home/'.$this->user;
$apacheVirtualHostConfigs = app()->virtualHostManager->getConfigs($this->additionalServices);
$settings = [
'port' => 80,
'domain' => $this->domain,
@ -64,6 +79,8 @@ class ApacheWebsiteCreate
'group' => 'www-data',
'enableRuid2' => true,
];
$settings = array_merge($settings, $apacheVirtualHostConfigs);
$apache2Sample = view('actions.samples.ubuntu.apache2-conf', $settings)->render();
if (!is_dir($homeRoot)) {

View file

@ -5,6 +5,8 @@ namespace App\Listeners;
use App\Actions\ApacheWebsiteCreate;
use App\Events\ModelWebsiteCreated;
use App\Models\Customer;
use App\Models\HostingPackage;
use App\Models\HostingPlan;
use Cassandra\Custom;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
@ -35,11 +37,18 @@ class ModelWebsiteCreatedListener
return;
}
$findHostingPlan = HostingPlan::where('id', $findWebsite->hosting_plan_id)->first();
if (!$findHostingPlan) {
return;
}
$newApacheWebsite = new ApacheWebsiteCreate();
$newApacheWebsite->setDomain($findWebsite->domain);
$newApacheWebsite->setUser($findCustomer->username);
$newApacheWebsite->setEmail($findCustomer->email);
$newApacheWebsite->setPassword(Str::random(16));
$newApacheWebsite->setAdditionalServices($findHostingPlan->additional_services);
$newApacheWebsite->setFeatures($findHostingPlan->features);
$create = $newApacheWebsite->handle();

View file

@ -1,11 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class HostingPackage extends Model
{
use HasFactory;
}

View file

@ -0,0 +1,14 @@
<?php
namespace App;
use Illuminate\Foundation\Application;
/**
* @property \App\FileManagerApi $file_manager_api
* @property \App\VirtualHosts\ApacheVirtualHostManager $virtualHostManager
*/
class PhyreLaravelApplication extends Application {
}

View file

@ -12,6 +12,7 @@ use App\Listeners\ModelWebsiteCreatedListener;
use App\Listeners\ModelWebsiteDeletingListener;
use App\Policies\CustomerPolicy;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
@ -23,7 +24,13 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
//
App::singleton('file_manager_api', function () {
return new \App\FileManagerApi();
});
App::singleton('virtualHostManager', function () {
return new \App\VirtualHosts\ApacheVirtualHostManager();
});
}
/**
@ -31,14 +38,13 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot(): void
{
Gate::define('delete-customer', [CustomerPolicy::class, 'delete']);
Filament::serving(function () {
// Using Vite
Filament::registerViteTheme('resources/css/app.css');
});
Gate::define('delete-customer', [CustomerPolicy::class, 'delete']);
Event::listen(ModelWebsiteCreated::class,ModelWebsiteCreatedListener::class);
Event::listen(ModelWebsiteDeleting::class,ModelWebsiteDeletingListener::class);
Event::listen(ModelCustomerCreated::class,ModelCustomerCreatedListener::class);

View file

@ -0,0 +1,18 @@
<?php
namespace App\VirtualHosts;
abstract class ApacheVirtualHostConfigBase
{
public array $phpAdminValueOpenBaseDirs = [
];
public function getConfig()
{
$configValues = [];
$configValues['phpAdminValueOpenBaseDirs'] = $this->phpAdminValueOpenBaseDirs;
return $configValues;
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace App\VirtualHosts;
class ApacheVirtualHostManager
{
public $registerConfigs = [];
public function registerConfig($config, $module = null)
{
$this->registerConfigs[$module][] = $config;
}
public function getConfigs($forModules = [])
{
$allConfigs = [];
foreach ($this->registerConfigs as $module => $configs) {
if (!in_array($module, $forModules)) {
continue;
}
foreach ($configs as $config) {
try {
$registerConfigInstance = app()->make($config);
$getConfig = $registerConfigInstance->getConfig();
if (!empty($getConfig)) {
foreach ($getConfig as $key => $value) {
if (!isset($allConfigs[$key])) {
$allConfigs[$key] = [];
}
$allConfigs[$key] = array_merge($allConfigs[$key], $value);
}
}
} catch (\Exception $e) {
// can't create instance
}
}
}
return $allConfigs;
}
}

View file

@ -11,7 +11,7 @@
|
*/
$app = new Illuminate\Foundation\Application(
$app = new \App\PhyreLaravelApplication(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

View file

@ -21,6 +21,21 @@
RMode config
RUidGid {{$user}} {{$group}}
@endif
@php
$appendOpenBaseDirs = $homeRoot;
if (isset($phpAdminValueOpenBaseDirs)
&& is_array($phpAdminValueOpenBaseDirs)
&& !empty($phpAdminValueOpenBaseDirs)) {
$appendOpenBaseDirs .= ':' . implode(':', $phpAdminValueOpenBaseDirs);
}
@endphp
php_admin_value open_basedir {{$appendOpenBaseDirs}}
php_admin_value upload_tmp_dir {{$homeRoot}}/tmp
php_admin_value session.save_path {{$homeRoot}}/tmp
php_admin_value sys_temp_dir {{$homeRoot}}/tmp
</Directory>