PhyrePanel/web/app/Actions/ApacheWebsiteCreate.php
2024-04-03 16:28:40 +03:00

123 lines
3.6 KiB
PHP

<?php
namespace App\Actions;
use App\FileManagerApi;
use App\ShellApi;
class ApacheWebsiteCreate
{
public $domain;
public $user;
public $email;
public $password;
public function setDomain($domain)
{
$this->domain = $domain;
}
public function setUser($user)
{
$this->user = $user;
}
public function setEmail($email)
{
$this->email = $email;
}
public function setPassword($password)
{
$this->password = $password;
}
public function handle()
{
$getLinuxUser = new GetLinuxUser();
$getLinuxUser->setUsername($this->user);
$linuxUser = $getLinuxUser->handle();
if (empty($linuxUser)) {
$createLinuxWebUser = new CreateLinuxWebUser();
$createLinuxWebUser->setUsername($this->user);
$createLinuxWebUser->setEmail($this->email);
$createLinuxWebUser->setPassword($this->password);
$createLinuxWebUserOutput = $createLinuxWebUser->handle();
$linuxUser = $getLinuxUser->handle();
}
$allDomainsRoot = '/home/'.$this->user.'/domains';
$domainRoot = '/home/'.$this->user.'/domains/'.$this->domain;
$homeRoot = '/home/'.$this->user;
//copy php suexe to {'home'}/fcgi-bin
// $fileManagerApi = new FileManagerApi();
$settings = [
'port' => 80,
'domain' => $this->domain,
'domainPublic' => $domainRoot . '/public_html',
'domainRoot' => $domainRoot,
'homeRoot' => '/home/' . $this->user,
'user' => $this->user,
'group' => $this->user,
// 'group' => 'www-data',
'enableRuid2' => true,
];
$apache2Sample = view('actions.samples.ubuntu.apache2-conf', $settings)->render();
$fileManagerApi = new FileManagerApi();
$fileManagerApi->mkdir($settings['domainRoot']);
$fileManagerApi->mkdir($settings['domainPublic']);
ShellApi::exec('chmod -R 775 /etc/apache2/sites-available/');
$fileManagerApi->filePutContents('/etc/apache2/sites-available/' . $settings['domain'] . '.conf', $apache2Sample);
$indexContent = '
<?php print_r($_ENV); ?>
<?php echo get_current_user(); ?>
<?php echo phpinfo(); ?>
';
$fileManagerApi->filePutContents($settings['domainPublic'] . '/index.php', $indexContent);
ShellApi::exec('chown -R ' . $settings['user'] . ':' . $settings['group'] . ' ' . $allDomainsRoot);
ShellApi::exec('chown -R ' . $settings['user'] . ':' . $settings['group'] . ' ' . $homeRoot);
ShellApi::exec('chown -R ' . $settings['user'] . ':' . $settings['group'] . ' ' . $settings['domainRoot']);
ShellApi::exec('chown -R ' . $settings['user'] . ':' . $settings['group'] . ' ' . $settings['domainPublic']);
ShellApi::exec('chmod -R 755 ' . $allDomainsRoot);
ShellApi::exec('chmod -R 755 ' . $homeRoot);
ShellApi::exec('chmod -R 755 ' . $settings['domainRoot']);
ShellApi::exec('chmod -R 755 ' . $settings['domainPublic']);
ShellApi::exec('a2ensite ' . $settings['domain'] . '.conf');
ShellApi::exec('systemctl reload apache2');
return [
'domain' => $this->domain,
'domainPublic' => $settings['domainPublic'],
'domainRoot' => $settings['domainRoot'],
'homeRoot' => $settings['homeRoot'],
'user' => $this->user,
'email' => $this->email,
'linuxUser' => $linuxUser,
'apache2Sample' => $apache2Sample
];
}
}