Hold on to your hats and get ready to build a multi-tenant app. Something that can scale to your customers needs and potentially generate some revenue. Multi-tenant infrastructure is a great way to utilize one code base for all of your tenants and save your time in the process.
I will be going over the setup of a multi-tenant application from scratch with Laravel. I will be using MySQL and Ubuntu. Most of these steps will be applicable to other Linux-based distros as well.
The whole point of a multi-tenant app is to give each tenant an individualized experience while using the same code base. I will be leveraging the Hyn\Multi-Tenant package to handle most of these functions. With this we can give each tenant its own database and this database is resolved during the beginning of the request life cycle. For instance, each tenant could use a different sub-domain (i.e. test.app.itplog.com). There is middleware built-in to the package that will automatically resolve to the Tenant’s database.
For this to work, there also needs to be a system database that keeps track of all of the Tenants. Each Tenant has a unique ID which can have multiple sub-domains. The package has two models that help keep track of everything, Website, and Hostname. The Website model contains the unique ID. This could essentially be called the Tenant. The Hostname contains the actual sub-domain which is used during the resolve process.
It is also good to note that each tenant doesn’t need to have it’s own separate database. You can do Schema separation and table prefixes to separate the Tenant’s information.
Enough blabbing, let’s get to the setup!
So I am starting from a fresh install of Ubuntu 18.04 Server. First things first, let’s update. For PHP 7.2 to work on older versions, you need to add the required repository and use php-7.2 in the package name.
sudo apt-get update
sudo apt-get upgrade
We need to install Apache, MySQL, and PHP 7.2.
sudo apt-get install apache2sudo apt-get install mysql-server mysql-clientsudo apt-get install php php-fpm php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-phpsudo apt-get install zip unzip git
MySQL will prompt for some additional setup. Just put in a root password. We will be using this password in our Laravel configuration so make sure to write it down!
We need to setup Apache so that it serves each of our Tenant sub-domains. We need to create a conf file in /etc/apache2/sites-available. Then we need to enable that site within Apache.
Run the below command to create the file.
sudo nano /etc/apache2/site-available/laravel.conf
Now paste the following text into that file. You will need to change the directories to match where your app files actually exist. Also change your domain name to match.
# public path, serving content
# default document handling
DirectoryIndex index.html index.php
# allow cross domain loading of resources
Header set Access-Control-Allow-Origin "*"
CustomLog /var/www/app-root/storage/logs/app-root.access.log combined
Require all granted
This will allow Apache to respond to itplog.com and all sub-domains.
We now need to enable the site and some modules. Run the commands below:
sudo a2enmod headers
sudo a2enmod rewrite
sudo a2ensite laravel
Apache should be all set now!
There isn’t a whole lot to setup here. We will need to create a database.
Use the below command to connect into your mysql instance. It will prompt for your password.
mysql -u root -p
Now run the command to create your system database that Laravel will use.
CREATE DATABASE tenancy
Now we are on to the Laravel stuff. We need to install Composer first.
sudo curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
This will make it so you can run
composer right from the cli.
Now browse to the
\var\www directory where you are putting your app files.
Then run the composer command to create a new project. You can change new-app to the actual name of you project.
composer create-project --prefer-dist laravel/laravel new-app
Once that finishes you should see the new directory. The next part in the series will cover installing the required packages and internal Laravel setup.
You can also clone this git repo for the full project files instead of creating a new app.
That is everything needed for the infrastructure of a multi-tenant app. Next I will be going over the Laravel configuration.
Check out the series below!
Part 0 — Laravel Multi-tenant App Setup ←You are here!
Part 1 — Laravel Passport and Hyn\Tenancy
Part 2 — VueJS and Laravel API
Part 3 — Laravel Multi-Tenant Testing
Join our community Slack and read our weekly Faun topics ⬇