This commit is contained in:
Bozhidar 2024-05-14 12:12:10 +03:00
parent 139a7cbe43
commit b94b1083b3
4 changed files with 149 additions and 87 deletions

View file

@ -129,7 +129,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 +147,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 +211,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,15 +340,8 @@ 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');
// }
// }
// Certificate setup
$catchMainDomain = '';
$domainExp = explode('.', $this->domain);
@ -382,22 +379,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);
@ -406,34 +411,9 @@ 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,93 @@
<?php
namespace App\VirtualHosts;
use App\Models\Domain;
class ApacheBuild
{
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(false);
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);
}
}

View file

@ -1,9 +0,0 @@
<?php
namespace App\VirtualHosts;
class ApacheBuildAll
{
}

View file

@ -1,4 +1,3 @@
<VirtualHost *:{{$port}}>
@if(!empty($serverAdmin))
@ -135,4 +134,3 @@
@endif
</VirtualHost>