Please note this package is under development. A working version can be seen in production on hyn.me. This package is compatible only with Laravel 5.1 LTS. If you have questions or wish to participate reach out to me in any way.
This package allows for multi tenancy websites on one installation of Laravel.
The goals for this and its related packages are:
- Unobtrusive multi tenancy for Laravel 5.1 LTS
- Provide proper insight into tenants and webserver
- Flexibility for developers, use it the way you want
For more information visit the hyn.me website.
Referring to wikipedia;
Multitenancy refers to a software architecture in which a single instance of a software runs on a server and serves multiple tenants.
In its most abstract sense you can use hyn to manage multiple websites with only one application installation.
- Multiple websites running on one code base.
- Multiple hostnames configured per website.
Each website has its own folder on disk, allowing:
- seperation of routes, templates, translations etc
- custom files (media, themes and packages)
Also each website has its own database, this ensures that in no way one website can access data from another website. The distinction also gives proper division of responsibilities to the system (global) and tenant (local) databases.
For more information visit the hyn.me website.
One website running on the multi tenant installation of hyn.me is dummy.hyn.me.
A demo showing the back-end will be available soon.
All packages of hyn (including multi tenancy) require Laravel 5.1 LTS.
Include the dependancy in your composer.json:
composer require hyn-me/multi-tenant
Register the service provider in your config/app.php
within the providers
array:
/*
* HynMe packages
* @info FrameworkServiceProvider will load any available Service Provider from other hyn-me packages
*/
'HynMe\Framework\FrameworkServiceProvider',
Please note this says FrameworkServiceProvider from the Framework package, registering the MultiTenantServiceProvider will break multi tenancy features!
To support multi tenancy in other (3rd party) packages, replace the class alias for Eloquent under aliases
in your config/app.php
:
'Eloquent' => 'HynMe\Framework\Models\AbstractModel',
This will ensure that all extended classes will by default connect with the tenant database instead of the system database.
If you want to manually connect to the tenant database, set the $connection
property of your class to tenant
.
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 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:
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 database.php
as hyn
key under connections
:
'hyn' => [
'driver' => 'mysql',
'host' => 'localhost',
'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 database.php
. In order to be as unobtrusive as possible this is not forced for any hyn package.
As a last step edit your .env
file in the root of your laravel installation and set a default hostname.
HYN_MULTI_TENANCY_HOSTNAME=<hostname>
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.
Go into your terminal and run the following artisan command to finish installation of multi tenancy.
php artisan multi-tenant:setup --tenant=EXAMPLE [email protected] --hostname=example.com --webserver=(nginx|apache|no)
Either run this command as root or under sudo if you want to configure your webserver service as well. Currently supported are apache2 and nginx.
Please note, if you decide to skip the configuration of the webserver you will have to configure it by yourself. Example files are generated in the storage/webserver
directories.
Q: How do you pronounce hyn?
A: You would pronounce it just like
hine
with the same sound asdine
.
Q: Why not use/help/extend AuraEQ?
A: AuraEQ is different in comparison to hyn in the sense that is uses the same database with specific columns per table to identify different tenants. Hyn aims to keep tenants seperated by giving a tenant website it's own database, disk folder, routes, vendor packages etc.
Q: Why not use/help/extends tenanti?
A: One primary goal of hyn is to remain unobtrusive, meaning you should use the package the way you want, without the need to completely change how you code/work/play. Also I think auto selecting the tenant website based on the configured hostnames is easier for website development companies to work with.
Q: Why do you need root or sudo to run the setup or the queue?
A: Sudo or root is only required to register the webserver configuration files into the webserver services. Running the queue under root allows the tasks to immediately update the webserver once new configuration files are written.
Q: Will you make this package paid in the future?
A: No. If any commercial move takes place, it will be at least a freemium where additional, optional packages will be made available for a fee. The core packages will always remain available under the MIT license.
Q: I have a bug, feature request or technical question.
A: Visit the issues page on github.
Q: I have need for more direct support, advice or consultation for implementation.
A: Contact me or other experienced implementation developers on gitter.
Q: Why does the user for the hyn
connection need grant
rights?
A: In order for hyn to create databases and give each tenant website its own database user, it needs to be allowed to grant those rights to dynamically generated users?
Q: Is hyn multi tenancy more vulnerable to hacking?
A: Using this package will not make your application more open to attacks. For instance the laravel application generates a random hash after installation, hyn uses this unique hash for generating tenant database passwords.
Q: Are these hyn packages a CMS?
A: No. The packages are meant for developers or development companies who want to run identical code on several websites, without the need to duplicate the code. This while also allowing for per-website different settings, vendor packages etc.
Q: Hooking apache config files to OSX apache webservice?
A: Edit
/etc/apache2/httpd.conf
and at the bottom at a lineInclude /<laravel installation>/storage/webserver/apache/*.conf
. Now reload or restart apache.