VaahSaaS: Laravel multi-database tenancy based SAAS CMS - Pre-Configured & Ready to Use.
Please consider starring the project to show your ❤️ and support.
Well, this is a pre-configure and ready to install repo for VaahCMS.
Download this repository and unzip.
Rename .env.example to .env
Run following command:
composer installPublish VaahCms Assets:
php artisan vendor:publish --provider="WebReinvent\VaahCms\VaahCmsServiceProvider" --tag=assetsphp artisan tenancy:install, this will register necessary ServiceProviders, migrations and public tenancy.php configuration files
Open tenancy.php and update the exempt_domains, if you are using localhost, make sure you configure VirtualHost
Then visit following url to setup the CMS:
<base-url>/public/vaahcms/setuphttps://tenancy.samuelstancl.me/docs/v2/getting-started/
- Install
composer require stancl/tenancy php artisan tenancy:install, this will register necessaryServiceProviders,migrationsand publictenancy.phpconfiguration files- In
.envDB_CONNECTIONwill serve as central database which will storedomainandrespectivedatabase details - Note: in
.envfileDB_USERNAMEshould have access to all the database that will be created when new tenants are created. - In
config/tenancy.php, add your main domain in following:
...
'exempt_domains' => [ // e.g. domains which host landing pages, sign up pages, etc
'YourRootDomain.com',
],
...- Run
php artisan migrate - How to create tenancy:
$data = Tenant::new()
->withDomains([$request->subdomain])
->withData(['plan' => 'free'])
->save();- Open
<xampp>\apache\conf\extra\httpd-vhosts.confadd following code:
<VirtualHost yourdomain.com>
DocumentRoot "<xampp>/htdocs/vaahsaas_director/public"
ServerName yourdomain.com
<Directory "<xampp>/htdocs/vaahsaas_director/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost test.yourdomain.com>
DocumentRoot "<xampp>/htdocs/vaahsaas_director/public"
ServerName test.yourdomain.com
<Directory "<xampp>/htdocs/vaahsaas_director/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>- Then open
notepadas administratorC:\Windows\System32\drivers\etc\host - Add following lines:
127.0.0.1 yourdomain.com
127.0.0.1 test.yourdomain.com- You may see some error
test.yourdomain.comlikeTenancy Not Identifiedif you have already not created any tenancy for the domain.
CPanel does not allow mysql_query to create database and this conflicts with stancl/tenancy.
Hence we need to install following package:
composer require webreinvent/laravel-cpanel This is allow us to do CRUD operation for CPanel Databases.
After the installation, we need to create DatabaseManager and register it in config/tenancy.php, code of that will be following:
<?php
declare(strict_types=1);
namespace App\TenantDatabaseManagers; //You should correct this namespace based on where the file is located
use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\DatabaseManager as IlluminateDatabaseManager;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
use WebReinvent\CPanel\CPanel;
class CPanelMySQLDatabaseManager implements TenantDatabaseManager
{
/** @var \Illuminate\Database\Connection */
protected $database;
public function __construct(Repository $config, IlluminateDatabaseManager $databaseManager)
{
$this->database = $databaseManager->connection($config['tenancy.database_manager_connections.mysql']);
}
public function createDatabase(string $name): bool
{
/*$charset = $this->database->getConfig('charset');
$collation = $this->database->getConfig('collation');
return $this->database->statement("CREATE DATABASE `$name` CHARACTER SET `$charset` COLLATE `$collation`");*/
$cpanel = new CPanel();
$response = $cpanel->createDatabase($name);
if(isset($response['status']) && $response['status'] == 'success')
{
$cpanel->setAllPrivilegesOnDatabase(env('DB_USERNAME'), $name);
return true;
} else{
return false;
}
}
public function deleteDatabase(string $name): bool
{
//return $this->database->statement("DROP DATABASE `$name`");
$cpanel = new CPanel();
$response = $cpanel->deleteDatabase($name);
if(isset($response['status']) && $response['status'] == 'success')
{
return true;
} else{
return false;
}
}
public function databaseExists(string $name): bool
{
return (bool) $this->database->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$name'");
}
}After this register it in config/tenancy.php:
...
'database_managers' => [
// Tenant database managers handle the creation & deletion of tenant databases.
'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class,
//'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class, // You should use this for localhost
'mysql' => App\TenantDatabaseManagers\CPanelMySQLDatabaseManager::class, // Correct the namespace
'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class,
],
..- Login
CPanel >> Subdomains, enter*in subdomain, indomainchoosetop level domain, remove_wildcard_fromDocument Root.
After this if you visit any subdomain it will show SSL error, to resolve it we need to install
wildcard ssl.
- Visit
https://www.sslforfree.com/and enter*.yourdomain.comand clickCreate Free Certificate - Add
Arecord to domain as per the instruction. On next page it will show all there SSL Certificate strings. - Login
CPanel >> SSL/TLS >> Manage SSL sites >> Install an SSL Websiteand choose*.yourdomain.comand enter SSL Certificate string to respective section and save. - Now try to visit any sub domain all of them should be working.
- Before deploying VaahSaas, create a folder
vaahsaasand move all files and folder in that exceptpublic - Rename
publicfolder topublic_htmland change the content to following:
require __DIR__.'/../vaahsaas/vendor/autoload.php';
$app = require_once __DIR__.'/../vaahsaas/bootstrap/app.php';- So in root folder you will have two folder
vaahsaasandpublic_htmlwhich can been deployed to therootof cpanel
WebReinvent is a web agency based in Delhi, India. You'll find an overview of all our open source projects on github.
The MIT License (MIT). Please see License File for more information.