Configuration
Configuring multi tenancy.
Queue
Although mentioned in the Requirements, a queue running as root is required for the integration into the webserver. Using that queue, the webserver automatically will be gracefully reloaded when configuration files are written.
System database connection
In order for tenancy to create separate databases for each tenants, a system connection must be configured.
In your config/database.php
file make sure a database connection is configured with the name hyn
. Also prevent any other connection listed as tenant
, this package will dynamically create a connection to the tenant database using that config connection identifier.
The system connection must have the rights to create, alter and delete users and databases and grant rights to others. This behavior is almost identical to a root (admin) user.
For security reasons do not configure that user for this connection. You could create such a user manually using the following sql command.
create database `hyn_multi_tenancy`;
create user `hyn`@'localhost' identified by '<your_strong_random_string>';
grant all privileges on *.* to 'hyn'@'localhost' with grant option;
Using the above snippet you would then add in your config config/database.php
as hyn
key under connections
:
'hyn' => [
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'hyn_multi_tenancy',
'username' => 'hyn',
'password' => '<your_strong_random_string>',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
In case you're wondering, you can still set the hyn
connection as your default
in the config/database.php
. In order to be as unobtrusive as possible this is not forced for any hyn package.
Default/fallback hostname (optional)
If you configured your webserver to redirect all hits to the multi tenancy installation you can ensure that those requests that match no configured hostname will be redirected to a default fallback. To do so simply edit your .env
file in the root of the installation and add the following.
HYN_MULTI_TENANCY_HOSTNAME=example.app
The entered hostname will be used to fallback if a hostname is hitting on the application that is unknown in the database, thus showing the fallback website. If you don't define this environment variable, a backtrace is generated.
Multi-tenant configuration file
There are quite some settings you can overrule by publishing the configuration file. In order to do so just use the default artisan method.
php artisan vendor:publish --tag=multi-tenant-config
The configuration is now editable under config/multi-tenant.php
. Each of the configurable settings is documented, so you should be able to understand it as is.
Run setup
The last step is to run the setup. This will create a first tenant and possibly create a dashboard with your admin user.
Run with elevated privileges
Please run the setup with elevated privileges, otherwise the application cannot hook into the webserver.
sudo php artisan multi-tenant:setup --customer=EXAMPLE [email protected] --hostname=example.com --webserver=(nginx|apache|no)
After the setup has been done, you can use the following locations:
- Webserver configuration files:
storage/webserver/
- Tenant seperated directories:
storage/multi-tenant/
If you decide not to integrate into the webserver (no option on webserver), it's up to you to setup your webserver to identify the seperate tenant websites.
Updated less than a minute ago