PHP code example of beziworld / laravel-hestiacp

1. Go to this page and download the library: Download beziworld/laravel-hestiacp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

beziworld / laravel-hestiacp example snippets


use Beziworld\HestiaCP\Exceptions\CommandException;
use Beziworld\HestiaCP\Facades\HestiaCP;

class ProvisionHosting implements ShouldQueue
{
    public function handle(): void
    {
        $user = $this->order->username;

        try {
            HestiaCP::users()->create($user, $password = Str::password(16), $this->order->email, package: 'business');
            HestiaCP::domains()->create($user, $this->order->domain);
            HestiaCP::dns()->create($user, $this->order->domain, config('hosting.ip'));
            HestiaCP::email()->createDomain($user, $this->order->domain);
            HestiaCP::email()->createAccount($user, $this->order->domain, 'office', Str::password(16), quotaMb: 2048);
            HestiaCP::ssl()->letsEncrypt($user, $this->order->domain, ['www.' . $this->order->domain]);
        } catch (CommandException $e) {
            // e.g. "Hestia v-add-user failed (E_EXISTS): user john exists"
            $this->fail($e);

            return;
        }

        Mail::to($this->order->email)->send(new HostingReady($this->order, $password));
    }
}

$users = HestiaCP::users()->all();                 // list<User>
$user = HestiaCP::users()->get('john');            // ?User, null when it does not exist

HestiaCP::users()->suspend('john');
HestiaCP::users()->unsuspend('john');
HestiaCP::users()->changePackage('john', 'premium');
HestiaCP::users()->changePassword('john', $newPassword);
HestiaCP::users()->packages();                      // packages with their limits
HestiaCP::users()->delete('john');                  // with all domains, mail and databases

HestiaCP::domains()->create('john', 'example.com');                         // default IP, www alias
HestiaCP::domains()->addAlias('john', 'example.com', ['shop.example.com']);
HestiaCP::domains()->changeBackendTemplate('john', 'example.com', 'PHP-8_3');
HestiaCP::domains()->redirect('john', 'example.net', 'https://example.com');

foreach (HestiaCP::domains()->all('john') as $domain) {
    // $domain->domain, $domain->aliases, $domain->ssl, $domain->letsEncrypt, $domain->diskUsedMb
}

HestiaCP::ftp()->create('john', 'example.com', 'deploy', $password, 'public_html');   // becomes john_deploy

HestiaCP::ssl()->letsEncrypt('john', 'example.com', ['www.example.com']);
HestiaCP::ssl()->letsEncrypt('john', 'example.com', mail: true);       // mail.example.com
HestiaCP::ssl()->forceHttps('john', 'example.com');
HestiaCP::ssl()->certificate('john', 'example.com');                   // NOT_AFTER, ISSUER, ...

HestiaCP::dns()->create('john', 'example.com', '192.0.2.10', ['ns1.example.com', 'ns2.example.com']);

HestiaCP::dns()->addRecord('john', 'example.com', 'www', 'A', '192.0.2.10');
HestiaCP::dns()->addRecord('john', 'example.com', '@', 'MX', 'mail.example.com.', priority: 10);
HestiaCP::dns()->addRecord('john', 'example.com', '_dmarc', 'TXT', 'v=DMARC1; p=quarantine', ttl: 300);

$mx = HestiaCP::dns()->findRecords('john', 'example.com', '@', 'MX');
HestiaCP::dns()->updateRecord('john', 'example.com', $mx[0]->id, '@', 'MX', 'mail2.example.com.', priority: 20);
HestiaCP::dns()->deleteRecord('john', 'example.com', $mx[0]->id);

$mail = HestiaCP::email();

$mail->createDomain('john', 'example.com', antispam: true, antivirus: true, dkim: true);
$mail->createAccount('john', 'example.com', 'office', $password, quotaMb: 2048);   // null: unlimited
$mail->changeQuota('john', 'example.com', 'office', 4096);
$mail->addAlias('john', 'example.com', 'office', 'sales');
$mail->addForward('john', 'example.com', 'office', '[email protected]');
$mail->setCatchAll('john', 'example.com', '[email protected]');

$mail->dkimRecord('john', 'example.com');   // ['name' => 'mail._domainkey', 'ttl' => 3600, 'value' => 'v=DKIM1; ...']
$mail->accounts('john', 'example.com');     // list<MailAccount>

HestiaCP::databases()->create('john', 'shop', 'shop', $password);   // creates john_shop with user john_shop
HestiaCP::databases()->changePassword('john', 'john_shop', $rotated);

HestiaCP::cron()->create('john', '* * * * *', 'php /home/john/web/example.com/app/artisan schedule:run');

HestiaCP::backups()->schedule('john');                       // queued, the user is notified by e-mail
$latest = HestiaCP::backups()->latest('john');               // ?Backup
HestiaCP::backups()->restore('john', $latest->file, web: 'example.com', dns: '', mail: '', databases: '*', cron: '', userDirectories: '');

// config/hestiacp.php
'connections' => [
    'default' => [...],
    'node2' => ['url' => env('HESTIA2_URL'), 'hash' => env('HESTIA2_HASH')],   // ACCESS:SECRET
],

HestiaCP::connection('node2')->users()->all();

HestiaCP::using(['url' => $server->url, 'hash' => decrypt($server->api_hash)])->system()->info();

HestiaCP::query('v-list-user-ips', ['john']);          // "json" is appended
HestiaCP::execute('v-change-user-shell', ['john', 'bash']);
HestiaCP::system()->stoppedServices();
bash
php artisan hestiacp:check