Let’s Build A SaaS Application From Scratch Part 2 — Initial Project Setup
Work in progress. Pages may be modified every now and then.
Disclaimer
I’ve followed Alan Thing’s guide on how to setup local development environment in Mac with Homebrew. I modified it a little bit to be able to use nginx instead of apache. Head over there if you want to use the same configuration as mine. That being said, before you continue, please make sure you’ve installed and setup PHP, MySQL, and Apache or Nginx.
Create a project
Let’s create a new Laravel application. I’m using the Laravel installer found in the Laravel Documentation:
$ laravel new church
Setup database connection
In order for us to be able to store data on our application, we need a database. We’ll be using MySQL as our database. We will be creating a system database and different local churches will have their own (tenant) databases.
In order for the application to be able to create different tenant databases per church, we need to setup a master database user with global privileges.
I’m currently using MacOS as my development platform so I’ll be showing how to attain the above task using SequelPro.
Setup MySQL database
- Head over to https://sequelpro.com/download, download SequelPro and install it.
- Open SequelPro. Click on Database > Add Database and name the database name as
church
. - Click on Database > User Accounts…
- Click the + button and set the username and password to
church
. This will be the username and password we’ll be using for our system/master connection.
- Click on Global Privileges and click on the Check All button
- Click on Schema Privileges, select the
church
schema, select all available privileges, and click on the arrow left to grant these selected privileges to your database user. Click on Apply to apply changes.
Now we have setup the database, database username, database password. We’ll now let our application know about these settings.
Open up .env
file and fill the database section as follows.
DB_CONNECTION=system
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=church
DB_USERNAME=church
DB_PASSWORD=churchLIMIT_UUID_LENGTH_32=true
As you can see we named our DB_CONNECTION
to system. This is not necessary, but I find it a good practice since we are building a multi-tenant application, and using the default connection name mysql
would be confusing in the long run. In addition to this, the multi-tenant package use the term system
to refer to the main database, so we’ll be using this as well for clarity and uniformity with the package.
LIMIT_UUID_LENGTH_32=true
— I will explain this later. Enter this in your .env
file for now.
Open up app/config/database.php
and rename the connection mysql
to system.
This is the same will be the DB_CONNECTION
that the .env
file will use.
Web server configuration
We’ll be using Nginx as our web server. There is a webserver feature in hyn/multi-tenant
for both apache and nginx but I’d like to have full control of my web server configs. So we’ll do it by hand.
Feel free to skip this part if you’ve setup your web server configs already.
I like to use https in my local development so we’ll redirect all http calls to https.
We won’t be discussing how to create self signed certificate in this tutorial.
There’s a big chance that the ports that I’m using above are different on your setup. So kindly adjust as needed.
Install hyn/multi-tenant
Run the composer command in the root of your project directory:
composer require hyn/multi-tenant
Run php artisan vendor:publish --tag=tenancy
to publish vendor files for hyn/multi-tenant.
Notice that there are 2 copied files. We’ll be using tenancy.php
and we’ll leave webserver.php
unmodified.
Continued on Part 3.