Skip to content

Mobytes/multi-tenant

 
 

Repository files navigation

Multi tenancy

Latest Stable Version License Build Status Code Coverage StyleCI

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

Reading material:

What is multi tenancy

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.

Multi tenancy how?

In its most abstract sense you can use this package 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 package page.

Example tenant website & demo

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.

Requirements

All packages for multi tenancy require Laravel 5.1 LTS.

Automatic installation

Only use this method on a new/bare ubuntu 15.04 server with at least 1GB memory.

  • Log in as root, or any user that has sudo.
  • Run: bash -c "$(wget -O - https://hyn.me/media/installer.sh)"
  • Wait for the three questions for a tenant name, email address and first hostname.

Your installation is now located in /var/www/ and a clean multi tenancy installation is available with:

  • apache2
  • latest php 5.6 stable
  • beanstalkd for queue handling, supervisor is verifying the service is up
  • redis for cache and session
  • mariadb

You still have to set up:

Manual installation

Composer

Include the dependancy in your composer.json:

composer require hyn/multi-tenant

Service provider

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
 */
Hyn\Framework\FrameworkServiceProvider::class,

Please note this says FrameworkServiceProvider from the Framework package, registering the MultiTenantServiceProvider will break multi tenancy features!

Third party eloquent models (optional)

To support multi tenancy in other (3rd party) packages, replace the class alias for Eloquent under aliases in your config/app.php:

'Eloquent'  => Hyn\Framework\Models\AbstractModel::class,

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.

Queue

Hyn uses the queue heavily to generate config files, create databases and unix users without blocking the application. In order to work properly setup the queue functionality.

Please note the queue has to run as root for configuration files and other task to be run without issues.

System database connection

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.

Default/fallback hostname

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.

Run the setup

Go into your terminal and run the following artisan command to finish installation of multi tenancy.

php artisan multi-tenant:setup --tenant=EXAMPLE --email=foo@example.com --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.

Chat or critical bug

If you'd like to hang out with us or would like to discuss a critical vulnerability; please contact me on gitter.

Q&A

Q: How do you pronounce hyn?

A: You would pronounce it just like hine with the same sound as dine.

Q: Why not use/help/extend AuraEQ?

A: AuraEQ is different in comparison to hyn in the sense that it 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 pricing model 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 for additional support.

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 line Include /<laravel installation>/storage/webserver/apache/*.conf. Now reload or restart apache.

About

Run multiple websites using the same laravel installation while keeping tenant specific data separated

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.7%
  • Shell 0.3%