Compare commits

...

9 commits

Author SHA1 Message Date
Bozhidar 0f8b14764b update 2024-05-14 12:40:53 +03:00
Bozhidar 691bcb7c85 Update ApacheBuild.php 2024-05-14 12:36:52 +03:00
Bozhidar cfc42dc1a2 update 2024-05-14 12:35:53 +03:00
Bozhidar 5c646346e2 update 2024-05-14 12:33:01 +03:00
Bozhidar b94b1083b3 update 2024-05-14 12:12:10 +03:00
Bozhidar 139a7cbe43 Update apache2-conf.blade.php 2024-05-14 11:55:33 +03:00
Bozhidar bc75d09a19 Update Domain.php 2024-05-14 11:37:54 +03:00
Bozhidar 16e5cc2cb1 update 2024-05-14 11:33:46 +03:00
Bozhidar bd13837008 Update WildcardDomain.php 2024-05-14 11:14:14 +03:00
7 changed files with 635 additions and 119 deletions

View file

@ -18,7 +18,7 @@ use Illuminate\Support\Str;
use Modules\LetsEncrypt\Filament\Clusters\LetsEncryptCluster;
use Outerweb\FilamentSettings\Filament\Pages\Settings as BaseSettings;
class WildcardMasterDomain extends BaseSettings
class WildcardDomain extends BaseSettings
{
protected static ?string $navigationGroup = 'Let\'s Encrypt';
@ -35,7 +35,7 @@ class WildcardMasterDomain extends BaseSettings
public static function getNavigationLabel() : string
{
return 'Wildcard Master Domain';
return 'Wildcard Domain';
}
public function getFormActions() : array
{
@ -47,6 +47,7 @@ class WildcardMasterDomain extends BaseSettings
public function installCertificates()
{
$masterDomain = new MasterDomain();
$masterDomain->domain = setting('general.wildcard_domain');
if (file_exists($this->installLogFilePath)) {
unlink($this->installLogFilePath);
@ -61,6 +62,7 @@ class WildcardMasterDomain extends BaseSettings
'locality' => $masterDomain->locality,
'organization' => $masterDomain->organization
])->render();
$acmeConfigYaml = preg_replace('~(*ANY)\A\s*\R|\s*(?!\r\n)\s$~mu', '', $acmeConfigYaml);
file_put_contents($masterDomain->domainRoot.'/acme-wildcard-config.yaml', $acmeConfigYaml);
@ -176,9 +178,9 @@ class WildcardMasterDomain extends BaseSettings
Wizard\Step::make('Install')
//->description('Install a wildcard SSL certificate for the master domain')
->schema([
TextInput::make('master_domain')
TextInput::make('wildcard_domain')
->helperText('Install a wildcard SSL certificate for the master domain')
->placeholder(setting('general.master_domain'))
->placeholder(setting('general.wildcard_domain'))
->disabled(),
])->afterValidation(function () {
if (file_exists($this->installLogFilePath)) {

View file

@ -3,6 +3,7 @@
namespace App\Console\Commands;
use App\Models\Domain;
use App\VirtualHosts\ApacheBuild;
use Illuminate\Console\Command;
class RunDomainRepair extends Command
@ -26,13 +27,8 @@ class RunDomainRepair extends Command
*/
public function handle()
{
$getAllDomains = Domain::all();
if ($getAllDomains->count() > 0) {
foreach ($getAllDomains as $domain) {
$this->info('Repair domain: ' . $domain->domain);
$domain->configureVirtualHost(false);
}
shell_exec('service apache2 restart');
}
$apache = new ApacheBuild();
$apache->fixPermissions();
$apache->build();
}
}

View file

@ -6,6 +6,7 @@ use App\Actions\ApacheWebsiteDelete;
use App\Events\DomainIsCreated;
use App\Events\ModelDomainDeleting;
use App\ShellApi;
use App\VirtualHosts\ApacheBuild;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Modules\Docker\App\Models\DockerContainer;
@ -76,18 +77,23 @@ class Domain extends Model
$model->save();
$model->configureVirtualHost();
$model->configureVirtualHost(true);
event(new DomainIsCreated($model));
});
static::updating(function ($model) {
$model->configureVirtualHost();
$model->configureVirtualHost(true);
});
static::saved(function ($model) {
$model->configureVirtualHost();
$model->configureVirtualHost(true);
$apacheBuild = new ApacheBuild();
$apacheBuild->build();
});
static::deleting(function ($model) {
@ -102,24 +108,6 @@ class Domain extends Model
ShellApi::safeDelete($model->domain_root, ['/home/' . $findHostingSubscription->system_username]);
$whiteListedPathsForDelete = [
'/etc/apache2/sites-available',
'/etc/apache2/sites-enabled',
];
$apacheConf = '/etc/apache2/sites-available/'.$model->domain.'.conf';
ShellApi::safeDelete($apacheConf, $whiteListedPathsForDelete);
$apacheConfEnabled = '/etc/apache2/sites-enabled/'.$model->domain.'.conf';
ShellApi::safeDelete($apacheConfEnabled, $whiteListedPathsForDelete);
// SSL
$apacheSSLConf = '/etc/apache2/sites-available/'.$model->domain.'-ssl.conf';
ShellApi::safeDelete($apacheSSLConf, $whiteListedPathsForDelete);
$apacheSSLConfEnabled = '/etc/apache2/sites-enabled/'.$model->domain.'-ssl.conf';
ShellApi::safeDelete($apacheSSLConfEnabled, $whiteListedPathsForDelete);
});
}
@ -129,7 +117,7 @@ class Domain extends Model
return $this->belongsTo(HostingSubscription::class);
}
public function configureVirtualHost($reloadApache = true)
public function configureVirtualHost($fixPermissions = false)
{
$findHostingSubscription = \App\Models\HostingSubscription::where('id', $this->hosting_subscription_id)
->first();
@ -147,14 +135,16 @@ class Domain extends Model
throw new \Exception('Domain root not found');
}
if (!is_dir($this->domain_root)) {
mkdir($this->domain_root, 0711, true);
}
if (!is_dir($this->domain_public)) {
mkdir($this->domain_public, 0755, true);
}
if (!is_dir($this->home_root)) {
mkdir($this->home_root, 0711, true);
if ($fixPermissions) {
if (!is_dir($this->domain_root)) {
mkdir($this->domain_root, 0711, true);
}
if (!is_dir($this->domain_public)) {
mkdir($this->domain_public, 0755, true);
}
if (!is_dir($this->home_root)) {
mkdir($this->home_root, 0711, true);
}
}
if ($this->is_installed_default_app_template == null) {
@ -209,34 +199,36 @@ class Domain extends Model
$webUserGroup = $findHostingSubscription->system_username;
// Fix file permissions
shell_exec('chown -R '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->home_root);
shell_exec('chown -R '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->domain_root);
shell_exec('chown -R '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->domain_public);
if ($fixPermissions) {
// Fix file permissions
shell_exec('chown -R ' . $findHostingSubscription->system_username . ':' . $webUserGroup . ' ' . $this->home_root);
shell_exec('chown -R ' . $findHostingSubscription->system_username . ':' . $webUserGroup . ' ' . $this->domain_root);
shell_exec('chown -R ' . $findHostingSubscription->system_username . ':' . $webUserGroup . ' ' . $this->domain_public);
shell_exec('chmod -R 0711 '.$this->home_root);
shell_exec('chmod -R 0711 '.$this->domain_root);
shell_exec('chmod -R 775 '.$this->domain_public);
shell_exec('chmod -R 0711 ' . $this->home_root);
shell_exec('chmod -R 0711 ' . $this->domain_root);
shell_exec('chmod -R 775 ' . $this->domain_public);
if (!is_dir($this->domain_root.'/logs/apache2')) {
shell_exec('mkdir -p '.$this->domain_root.'/logs/apache2');
}
shell_exec('chown -R '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->domain_root.'/logs/apache2');
shell_exec('chmod -R 775 '.$this->domain_root.'/logs/apache2');
if (!is_dir($this->domain_root . '/logs/apache2')) {
shell_exec('mkdir -p ' . $this->domain_root . '/logs/apache2');
}
shell_exec('chown -R ' . $findHostingSubscription->system_username . ':' . $webUserGroup . ' ' . $this->domain_root . '/logs/apache2');
shell_exec('chmod -R 775 ' . $this->domain_root . '/logs/apache2');
if (!is_file($this->domain_root.'/logs/apache2/bytes.log')) {
shell_exec('touch '.$this->domain_root.'/logs/apache2/bytes.log');
}
if (!is_file($this->domain_root.'/logs/apache2/access.log')) {
shell_exec('touch '.$this->domain_root.'/logs/apache2/access.log');
}
if (!is_file($this->domain_root.'/logs/apache2/error.log')) {
shell_exec('touch '.$this->domain_root.'/logs/apache2/error.log');
}
if (!is_file($this->domain_root . '/logs/apache2/bytes.log')) {
shell_exec('touch ' . $this->domain_root . '/logs/apache2/bytes.log');
}
if (!is_file($this->domain_root . '/logs/apache2/access.log')) {
shell_exec('touch ' . $this->domain_root . '/logs/apache2/access.log');
}
if (!is_file($this->domain_root . '/logs/apache2/error.log')) {
shell_exec('touch ' . $this->domain_root . '/logs/apache2/error.log');
}
shell_exec('chmod -R 775 '.$this->domain_root.'/logs/apache2/bytes.log');
shell_exec('chmod -R 775 '.$this->domain_root.'/logs/apache2/access.log');
shell_exec('chmod -R 775 '.$this->domain_root.'/logs/apache2/error.log');
shell_exec('chmod -R 775 ' . $this->domain_root . '/logs/apache2/bytes.log');
shell_exec('chmod -R 775 ' . $this->domain_root . '/logs/apache2/access.log');
shell_exec('chmod -R 775 ' . $this->domain_root . '/logs/apache2/error.log');
}
$appType = 'php';
$appVersion = '8.3';
@ -336,16 +328,6 @@ class Domain extends Model
$apacheBaseConfig = $apacheVirtualHostBuilder->buildConfig();
if (!empty($apacheBaseConfig)) {
file_put_contents('/etc/apache2/sites-available/'.$this->domain.'.conf', $apacheBaseConfig);
// check symlink exists
$symlinkExists = file_exists('/etc/apache2/sites-enabled/'.$this->domain.'.conf');
if (!$symlinkExists) {
shell_exec('ln -s /etc/apache2/sites-available/' . $this->domain . '.conf /etc/apache2/sites-enabled/' . $this->domain . '.conf');
}
}
$catchMainDomain = '';
$domainExp = explode('.', $this->domain);
if (count($domainExp) > 0) {
@ -374,6 +356,7 @@ class Domain extends Model
}
}
$apacheBaseConfigWithSSL = null;
if ($findDomainSSLCertificate) {
$sslCertificateFile = $this->home_root . '/certs/' . $this->domain . '/public/cert.pem';
@ -381,22 +364,30 @@ class Domain extends Model
$sslCertificateChainFile = $this->home_root . '/certs/' . $this->domain . '/public/fullchain.pem';
if (!empty($findDomainSSLCertificate->certificate)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/public')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/public', 0755, true);
if (!file_exists($sslCertificateFile)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/public')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/public', 0755, true);
}
file_put_contents($sslCertificateFile, $findDomainSSLCertificate->certificate);
}
file_put_contents($sslCertificateFile, $findDomainSSLCertificate->certificate);
}
if (!empty($findDomainSSLCertificate->private_key)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/private')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/private', 0755, true);
if (!file_exists($sslCertificateKeyFile)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/private')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/private', 0755, true);
}
file_put_contents($sslCertificateKeyFile, $findDomainSSLCertificate->private_key);
}
file_put_contents($sslCertificateKeyFile, $findDomainSSLCertificate->private_key);
}
if (!empty($findDomainSSLCertificate->certificate_chain)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/public')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/public', 0755, true);
if (!file_exists($sslCertificateChainFile)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/public')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/public', 0755, true);
}
file_put_contents($sslCertificateChainFile, $findDomainSSLCertificate->certificate_chain);
}
file_put_contents($sslCertificateChainFile, $findDomainSSLCertificate->certificate_chain);
}
$apacheVirtualHostBuilder->setPort(443);
@ -405,33 +396,13 @@ class Domain extends Model
$apacheVirtualHostBuilder->setSSLCertificateChainFile($sslCertificateChainFile);
$apacheBaseConfigWithSSL = $apacheVirtualHostBuilder->buildConfig();
if (!empty($apacheBaseConfigWithSSL)) {
// Add SSL options conf file
$apache2SSLOptionsSample = view('actions.samples.ubuntu.apache2-ssl-options-conf')->render();
$apache2SSLOptionsFilePath = '/etc/apache2/phyre/options-ssl-apache.conf';
if (!file_exists($apache2SSLOptionsFilePath)) {
if (!is_dir('/etc/apache2/phyre')) {
mkdir('/etc/apache2/phyre');
}
file_put_contents($apache2SSLOptionsFilePath, $apache2SSLOptionsSample);
}
file_put_contents('/etc/apache2/sites-available/'.$this->domain.'-ssl.conf', $apacheBaseConfigWithSSL);
if (!is_link('/etc/apache2/sites-enabled/' . $this->domain . '-ssl.conf')) {
shell_exec('ln -s /etc/apache2/sites-available/' . $this->domain . '-ssl.conf /etc/apache2/sites-enabled/' . $this->domain . '-ssl.conf');
}
}
}
// Reload apache
if ($reloadApache) {
shell_exec('systemctl reload apache2');
}
return [
'apacheBaseConfig' => $apacheBaseConfig,
'apacheBaseConfigWithSSL' => $apacheBaseConfigWithSSL,
];
}
}

View file

@ -0,0 +1,437 @@
<?php
namespace App\Models;
use App\Actions\ApacheWebsiteDelete;
use App\Events\DomainIsCreated;
use App\Events\ModelDomainDeleting;
use App\ShellApi;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Modules\Docker\App\Models\DockerContainer;
class Domain extends Model
{
public const STATUS_ACTIVE = 'active';
public const STATUS_SUSPENDED = 'suspended';
public const STATUS_DELETED = 'deleted';
public const STATUS_DEACTIVATED = 'deactivated';
protected $fillable = [
'domain',
'domain_root',
'ip',
'hosting_subscription_id',
'server_application_type',
'server_application_settings',
'status'
];
protected $casts = [
'server_application_settings' => 'array',
];
protected static function booted(): void
{
static::addGlobalScope('customer', function (Builder $query) {
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
}
});
}
public static function boot()
{
parent::boot();
static::created(function ($model) {
$findHostingSubscription = HostingSubscription::where('id', $model->hosting_subscription_id)->first();
if (! $findHostingSubscription) {
return;
}
$findHostingPlan = HostingPlan::where('id', $findHostingSubscription->hosting_plan_id)->first();
if (! $findHostingPlan) {
return;
}
$model->server_application_type = $findHostingPlan->default_server_application_type;
$model->server_application_settings = $findHostingPlan->default_server_application_settings;
if ($model->is_main == 1) {
// $allDomainsRoot = '/home/'.$this->user.'/public_html';
$model->domain_root = '/home/'.$findHostingSubscription->system_username;
$model->domain_public = '/home/'.$findHostingSubscription->system_username.'/public_html';
$model->home_root = '/home/'.$findHostingSubscription->system_username;
} else {
// $allDomainsRoot = '/home/'.$model->user.'/domains';
$model->domain_root = '/home/'.$findHostingSubscription->system_username.'/domains/'.$model->domain;
$model->domain_public = $model->domain_root.'/public_html';
$model->home_root = '/home/'.$findHostingSubscription->user;
}
$model->save();
$model->configureVirtualHost();
event(new DomainIsCreated($model));
});
static::updating(function ($model) {
$model->configureVirtualHost();
});
static::saved(function ($model) {
$model->configureVirtualHost();
});
static::deleting(function ($model) {
if (empty($model->domain_public)) {
return;
}
$findHostingSubscription = HostingSubscription::where('id', $model->hosting_subscription_id)->first();
if (! $findHostingSubscription) {
return;
}
ShellApi::safeDelete($model->domain_root, ['/home/' . $findHostingSubscription->system_username]);
$whiteListedPathsForDelete = [
'/etc/apache2/sites-available',
'/etc/apache2/sites-enabled',
];
$apacheConf = '/etc/apache2/sites-available/'.$model->domain.'.conf';
ShellApi::safeDelete($apacheConf, $whiteListedPathsForDelete);
$apacheConfEnabled = '/etc/apache2/sites-enabled/'.$model->domain.'.conf';
ShellApi::safeDelete($apacheConfEnabled, $whiteListedPathsForDelete);
// SSL
$apacheSSLConf = '/etc/apache2/sites-available/'.$model->domain.'-ssl.conf';
ShellApi::safeDelete($apacheSSLConf, $whiteListedPathsForDelete);
$apacheSSLConfEnabled = '/etc/apache2/sites-enabled/'.$model->domain.'-ssl.conf';
ShellApi::safeDelete($apacheSSLConfEnabled, $whiteListedPathsForDelete);
});
}
public function hostingSubscription()
{
return $this->belongsTo(HostingSubscription::class);
}
public function configureVirtualHost($reloadApache = true)
{
$findHostingSubscription = \App\Models\HostingSubscription::where('id', $this->hosting_subscription_id)
->first();
if (!$findHostingSubscription) {
throw new \Exception('Hosting subscription not found');
}
$findHostingPlan = \App\Models\HostingPlan::where('id', $findHostingSubscription->hosting_plan_id)
->first();
if (!$findHostingPlan) {
throw new \Exception('Hosting plan not found');
}
if (empty($this->domain_root)) {
throw new \Exception('Domain root not found');
}
if (!is_dir($this->domain_root)) {
mkdir($this->domain_root, 0711, true);
}
if (!is_dir($this->domain_public)) {
mkdir($this->domain_public, 0755, true);
}
if (!is_dir($this->home_root)) {
mkdir($this->home_root, 0711, true);
}
if ($this->is_installed_default_app_template == null) {
$this->is_installed_default_app_template = 1;
$this->save();
if ($this->server_application_type == 'apache_php') {
if (!is_file($this->domain_public . '/index.php')) {
$indexContent = view('actions.samples.apache.php.app-php-sample')->render();
file_put_contents($this->domain_public . '/index.php', $indexContent);
}
if (!is_dir($this->domain_public . '/templates')) {
mkdir($this->domain_public . '/templates', 0755, true);
}
if (!is_file($this->domain_public . '/templates/index.html')) {
$indexContent = view('actions.samples.apache.php.app-index-html')->render();
file_put_contents($this->domain_public . '/templates/index.html', $indexContent);
}
}
if ($this->server_application_type == 'apache_nodejs') {
if (!is_file($this->domain_public . '/app.js')) {
$indexContent = view('actions.samples.apache.nodejs.app-nodejs-sample')->render();
file_put_contents($this->domain_public . '/app.js', $indexContent);
}
if (!is_dir($this->domain_public . '/templates')) {
mkdir($this->domain_public . '/templates', 0755, true);
}
if (!is_file($this->domain_public . '/templates/index.html')) {
$indexContent = view('actions.samples.apache.nodejs.app-index-html')->render();
file_put_contents($this->domain_public . '/templates/index.html', $indexContent);
}
}
if ($this->server_application_type == 'apache_python') {
if (!is_file($this->domain_public . '/app.py')) {
$indexContent = view('actions.samples.apache.python.app-python-sample')->render();
file_put_contents($this->domain_public . '/app.py', $indexContent);
}
if (!is_file($this->domain_public . '/passenger_wsgi.py')) {
$indexContent = view('actions.samples.apache.python.app-passanger-wsgi-sample')->render();
file_put_contents($this->domain_public . '/passenger_wsgi.py', $indexContent);
}
if (!is_dir($this->domain_public . '/templates')) {
mkdir($this->domain_public . '/templates', 0755, true);
}
if (!is_file($this->domain_public . '/templates/index.html')) {
$indexContent = view('actions.samples.apache.python.app-index-html')->render();
file_put_contents($this->domain_public . '/templates/index.html', $indexContent);
}
}
}
$webUserGroup = $findHostingSubscription->system_username;
// Fix file permissions
shell_exec('chown -R '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->home_root);
shell_exec('chown -R '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->domain_root);
shell_exec('chown -R '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->domain_public);
shell_exec('chmod -R 0711 '.$this->home_root);
shell_exec('chmod -R 0711 '.$this->domain_root);
shell_exec('chmod -R 775 '.$this->domain_public);
if (!is_dir($this->domain_root.'/logs/apache2')) {
shell_exec('mkdir -p '.$this->domain_root.'/logs/apache2');
}
shell_exec('chown -R '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->domain_root.'/logs/apache2');
shell_exec('chmod -R 775 '.$this->domain_root.'/logs/apache2');
if (!is_file($this->domain_root.'/logs/apache2/bytes.log')) {
shell_exec('touch '.$this->domain_root.'/logs/apache2/bytes.log');
}
if (!is_file($this->domain_root.'/logs/apache2/access.log')) {
shell_exec('touch '.$this->domain_root.'/logs/apache2/access.log');
}
if (!is_file($this->domain_root.'/logs/apache2/error.log')) {
shell_exec('touch '.$this->domain_root.'/logs/apache2/error.log');
}
shell_exec('chmod -R 775 '.$this->domain_root.'/logs/apache2/bytes.log');
shell_exec('chmod -R 775 '.$this->domain_root.'/logs/apache2/access.log');
shell_exec('chmod -R 775 '.$this->domain_root.'/logs/apache2/error.log');
$appType = 'php';
$appVersion = '8.3';
if ($this->server_application_type == 'apache_php') {
if (isset($this->server_application_settings['php_version'])) {
$appVersion = $this->server_application_settings['php_version'];
}
if (!is_dir($this->domain_public . '/cgi-bin')) {
mkdir($this->domain_public . '/cgi-bin', 0755, true);
}
file_put_contents($this->domain_public . '/cgi-bin/php', '#!/usr/bin/php-cgi' . $appVersion . ' -cphp' . $appVersion . '-cgi.ini');
shell_exec('chown '.$findHostingSubscription->system_username.':'.$webUserGroup.' '.$this->domain_public . '/cgi-bin/php');
shell_exec('chmod -f 751 '.$this->domain_public . '/cgi-bin/php');
}
$apacheVirtualHostBuilder = new \App\VirtualHosts\ApacheVirtualHostBuilder();
$apacheVirtualHostBuilder->setDomain($this->domain);
$apacheVirtualHostBuilder->setDomainPublic($this->domain_public);
$apacheVirtualHostBuilder->setDomainRoot($this->domain_root);
$apacheVirtualHostBuilder->setHomeRoot($this->home_root);
$apacheVirtualHostBuilder->setUser($findHostingSubscription->system_username);
$apacheVirtualHostBuilder->setUserGroup($webUserGroup);
if ($this->status == self::STATUS_SUSPENDED) {
$suspendedPath = '/var/www/html/suspended';
if (!is_dir($suspendedPath)) {
mkdir($suspendedPath, 0755, true);
}
if (!is_file($suspendedPath . '/index.html')) {
$suspendedPageHtmlPath = base_path('resources/views/actions/samples/apache/html/app-suspended-page.html');
file_put_contents($suspendedPath . '/index.html', file_get_contents($suspendedPageHtmlPath));
}
$apacheVirtualHostBuilder->setDomainRoot($suspendedPath);
$apacheVirtualHostBuilder->setDomainPublic($suspendedPath);
} else if ($this->status == self::STATUS_DEACTIVATED) {
$deactivatedPath = '/var/www/html/deactivated';
if (!is_dir($deactivatedPath)) {
mkdir($deactivatedPath, 0755, true);
}
if (!is_file($deactivatedPath . '/index.html')) {
$deactivatedPageHtmlPath = base_path('resources/views/actions/samples/apache/html/app-deactivated-page.html');
file_put_contents($deactivatedPath . '/index.html', file_get_contents($deactivatedPageHtmlPath));
}
$apacheVirtualHostBuilder->setDomainRoot($deactivatedPath);
$apacheVirtualHostBuilder->setDomainPublic($deactivatedPath);
} else {
$apacheVirtualHostBuilder->setEnableLogs(true);
$apacheVirtualHostBuilder->setAdditionalServices($findHostingPlan->additional_services);
$apacheVirtualHostBuilder->setAppType($appType);
$apacheVirtualHostBuilder->setAppVersion($appVersion);
if ($this->server_application_type == 'apache_nodejs') {
$apacheVirtualHostBuilder->setAppType('nodejs');
$apacheVirtualHostBuilder->setPassengerAppRoot($this->domain_public);
$apacheVirtualHostBuilder->setPassengerAppType('node');
$apacheVirtualHostBuilder->setPassengerStartupFile('app.js');
if (isset($this->server_application_settings['nodejs_version'])) {
$apacheVirtualHostBuilder->setAppVersion($this->server_application_settings['nodejs_version']);
}
}
if ($this->server_application_type == 'apache_python') {
$apacheVirtualHostBuilder->setAppType('python');
$apacheVirtualHostBuilder->setPassengerAppRoot($this->domain_public);
$apacheVirtualHostBuilder->setPassengerAppType('python');
if (isset($this->server_application_settings['python_version'])) {
$apacheVirtualHostBuilder->setAppVersion($this->server_application_settings['python_version']);
}
}
if ($this->server_application_type == 'apache_ruby') {
$apacheVirtualHostBuilder->setAppType('ruby');
$apacheVirtualHostBuilder->setPassengerAppRoot($this->domain_public);
$apacheVirtualHostBuilder->setPassengerAppType('ruby');
if (isset($this->server_application_settings['ruby_version'])) {
$apacheVirtualHostBuilder->setAppVersion($this->server_application_settings['ruby_version']);
}
}
if ($this->server_application_type == 'apache_docker') {
if (isset($this->server_application_settings['docker_container_id'])) {
$findDockerContainer = DockerContainer::where('id', $this->server_application_settings['docker_container_id'])
->first();
if ($findDockerContainer) {
$apacheVirtualHostBuilder->setProxyPass('http://127.0.0.1:' . $findDockerContainer->external_port . '/');
$apacheVirtualHostBuilder->setAppType('docker');
$apacheVirtualHostBuilder->setAppVersion($appVersion);
}
}
}
}
$apacheBaseConfig = $apacheVirtualHostBuilder->buildConfig();
if (!empty($apacheBaseConfig)) {
file_put_contents('/etc/apache2/sites-available/'.$this->domain.'.conf', $apacheBaseConfig);
// check symlink exists
$symlinkExists = file_exists('/etc/apache2/sites-enabled/'.$this->domain.'.conf');
if (!$symlinkExists) {
shell_exec('ln -s /etc/apache2/sites-available/' . $this->domain . '.conf /etc/apache2/sites-enabled/' . $this->domain . '.conf');
}
}
$catchMainDomain = '';
$domainExp = explode('.', $this->domain);
if (count($domainExp) > 0) {
unset($domainExp[0]);
$catchMainDomain = implode('.', $domainExp);
}
$findDomainSSLCertificate = null;
$findMainDomainSSLCertificate = \App\Models\DomainSslCertificate::where('domain', $this->domain)
->first();
if ($findMainDomainSSLCertificate) {
$findDomainSSLCertificate = $findMainDomainSSLCertificate;
} else {
$findDomainSSLCertificateWildcard = \App\Models\DomainSslCertificate::where('domain', '*.' . $this->domain)
->where('is_wildcard', 1)
->first();
if ($findDomainSSLCertificateWildcard) {
$findDomainSSLCertificate = $findDomainSSLCertificateWildcard;
} else {
$findMainDomainWildcardSSLCertificate = \App\Models\DomainSslCertificate::where('domain', '*.'.$catchMainDomain)
->first();
if ($findMainDomainWildcardSSLCertificate) {
$findDomainSSLCertificate = $findMainDomainWildcardSSLCertificate;
}
}
}
if ($findDomainSSLCertificate) {
$sslCertificateFile = $this->home_root . '/certs/' . $this->domain . '/public/cert.pem';
$sslCertificateKeyFile = $this->home_root . '/certs/' . $this->domain . '/private/key.private.pem';
$sslCertificateChainFile = $this->home_root . '/certs/' . $this->domain . '/public/fullchain.pem';
if (!empty($findDomainSSLCertificate->certificate)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/public')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/public', 0755, true);
}
file_put_contents($sslCertificateFile, $findDomainSSLCertificate->certificate);
}
if (!empty($findDomainSSLCertificate->private_key)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/private')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/private', 0755, true);
}
file_put_contents($sslCertificateKeyFile, $findDomainSSLCertificate->private_key);
}
if (!empty($findDomainSSLCertificate->certificate_chain)) {
if (!is_dir($this->home_root . '/certs/' . $this->domain . '/public')) {
mkdir($this->home_root . '/certs/' . $this->domain . '/public', 0755, true);
}
file_put_contents($sslCertificateChainFile, $findDomainSSLCertificate->certificate_chain);
}
$apacheVirtualHostBuilder->setPort(443);
$apacheVirtualHostBuilder->setSSLCertificateFile($sslCertificateFile);
$apacheVirtualHostBuilder->setSSLCertificateKeyFile($sslCertificateKeyFile);
$apacheVirtualHostBuilder->setSSLCertificateChainFile($sslCertificateChainFile);
$apacheBaseConfigWithSSL = $apacheVirtualHostBuilder->buildConfig();
if (!empty($apacheBaseConfigWithSSL)) {
// Add SSL options conf file
$apache2SSLOptionsSample = view('actions.samples.ubuntu.apache2-ssl-options-conf')->render();
$apache2SSLOptionsFilePath = '/etc/apache2/phyre/options-ssl-apache.conf';
if (!file_exists($apache2SSLOptionsFilePath)) {
if (!is_dir('/etc/apache2/phyre')) {
mkdir('/etc/apache2/phyre');
}
file_put_contents($apache2SSLOptionsFilePath, $apache2SSLOptionsSample);
}
file_put_contents('/etc/apache2/sites-available/'.$this->domain.'-ssl.conf', $apacheBaseConfigWithSSL);
if (!is_link('/etc/apache2/sites-enabled/' . $this->domain . '-ssl.conf')) {
shell_exec('ln -s /etc/apache2/sites-available/' . $this->domain . '-ssl.conf /etc/apache2/sites-enabled/' . $this->domain . '-ssl.conf');
}
}
}
// Reload apache
if ($reloadApache) {
shell_exec('systemctl reload apache2');
}
}
}

View file

@ -0,0 +1,102 @@
<?php
namespace App\VirtualHosts;
use App\Models\Domain;
class ApacheBuild
{
public $fixPermissions = false;
public function fixPermissions()
{
$this->fixPermissions = true;
}
public function build()
{
$virtualHostMerged = '
#=========================================================================#
# PHYRE HOSTING PANEL - Default Web Domain Template #
# DO NOT MODIFY THIS FILE! CHANGES WILL BE LOST WHEN REBUILDING DOMAINS #
# https://phyrepanel.com/docs/server-administration/web-templates.html #
#=========================================================================#
DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Listen 80
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
';
$getAllDomains = Domain::all();
foreach ($getAllDomains as $domain) {
$domainVirtualHost = $domain->configureVirtualHost($this->fixPermissions);
if (isset($domainVirtualHost['apacheBaseConfig'])) {
$virtualHostMerged .= $domainVirtualHost['apacheBaseConfig'] . "\n\n";
}
if (isset($domainVirtualHost['apacheBaseConfigWithSSL'])) {
$virtualHostMerged .= $domainVirtualHost['apacheBaseConfigWithSSL'] . "\n\n";
}
}
file_put_contents('/etc/apache2/apache2.conf', $virtualHostMerged);
shell_exec('systemctl reload apache2');
}
}

View file

@ -1,9 +1,3 @@
#=========================================================================#
# PHYRE HOSTING PANEL - Default Web Domain Template #
# DO NOT MODIFY THIS FILE! CHANGES WILL BE LOST WHEN REBUILDING DOMAINS #
# https://phyrepanel.com/docs/server-administration/web-templates.html #
#=========================================================================#
<VirtualHost *:{{$port}}>
@if(!empty($serverAdmin))
@ -121,9 +115,22 @@
@endif
Include /etc/apache2/phyre/options-ssl-apache.conf
SSLEngine on
# Intermediate configuration, tweak to your needs
SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
SSLHonorCipherOrder off
SSLSessionTickets off
SSLOptions +StrictRequire
# Add vhost name to log entries:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
@endif
</VirtualHost>

View file

@ -171,10 +171,11 @@ class AHostingSubscriptionCreateTest extends ActionTestCase
$domainData = $callDomainDetailsResponseData[0];
// Check virtual host is created
$virtualHostFile = '/etc/apache2/sites-available/'.$hostingSubscriptionDomain.'.conf';
$virtualHostFile = '/etc/apache2/apache2.conf';
$this->assertFileExists($virtualHostFile);
$virtualHostFileContent = file_get_contents($virtualHostFile);
$this->assertStringContainsString('ServerName '.$hostingSubscriptionDomain, $virtualHostFileContent);
//$this->assertStringContainsString('ServerAlias www.'.$hostingSubscriptionDomain, $virtualHostFileContent);
@ -183,7 +184,7 @@ class AHostingSubscriptionCreateTest extends ActionTestCase
$this->assertStringContainsString('php_admin_value open_basedir '.$domainData['home_root'], $virtualHostFileContent);
// Check virtual host is enabled
$this->assertFileExists('/etc/apache2/sites-enabled/'.$hostingSubscriptionDomain.'.conf');
$this->assertFileExists('/etc/apache2/apache2.conf');
// Check apache config is valid
shell_exec('apachectl -t >> /tmp/apache_config_check.txt 2>&1');