Codemason Blog | Codemason
Codemason Blog | Codemason

Codemason is a cloud platform for hosting apps without the hassle. Our superpower is making complex things simple. Build, scale and manage hundreds of sites with ease on Codemason.

Ben Magg
Author

Founder of Codemason

Share


Our Newsletter


Subscribe to our newsletter to get the latest on building, deploying, managing and scaling great apps and businesses.

Tags


Twitter


Codemason Blog | Codemason

How to deploy Laravel Spark

Deploy your Laravel Spark app in minutes using Codemason.

Ben MaggBen Magg

This post will walk you through deploying your Laravel Spark app on Codemason.

Laravel Spark is scaffolding for SaaS projects. It saves you weeks of development by providing all the boilerplate you need for a SaaS app. This way, you can get back to focusing on what matters.

By the end of this tutorial, you will have deployed a Laravel Spark app on Codemason.

This guide assumes that you already have:

Prepare your Spark app

First, let's create a Spark app for us to work with. Of course, you can skip this step if you already have a Spark app you're working on.

spark new pebble

Initialize a Git repository

If you're starting fresh, you will need to initialize a git repository for your Spark app and make your initial commit. It's good practice and required for Codemason.

You can initialise a git repository in the current folder by running:

$ git init
$ git add .
$ git commit -m "Building something amazing!"

Craft your app

Now it's time to get your Spark codebase "Codemason ready". We use Docker at Codemason but don't worry, you don't need to know anything about Docker.

We just need to add a couple of files to your Spark app before it's ready to go. Using Craft Kits, it's all done automatically for you with one command.

mason craft laravel 

Commit these new files to source control

git add .
git commit -m "Build scripts"

Create an app

Now it's time to create an app on Codemason for your Spark project.

Creating an app on Codemason can be done via the UI or via the CLI:

mason create pebble

This command creates an app on Codemason for you and prepares a git remote repository which is used to transport your code to the Codemason Build pipeline.

Push it to the new git remote to kick off your first build:

git push codemason master

Deploying Laravel Spark

You can deploy to Codemason via the UI or via the CLI. We'll deploy through the CLI for this tutorial.

First we'll create our web service:

mason services:create pebble/web -p 80:80 --env-file .env

Then we'll create our db service.

NOTE: Remember to change the credentials in the snippet below so they match what your app is expecting.

mason services:create pebble/db --image mariadb -p 3306:3306 \
	--env MYSQL_DATABASE=pebble \
	--env MYSQL_USER=demo \
	--env MYSQL_PASSWORD=secret \
	--env MYSQL_ROOT_PASSWORD=supersecret 

Updating your app

git push codemason master

Run the upgrade command. Be sure to specify the service you wish to upgrade in the following format <application>/<service>.

mason services:upgrade pebble/web 

🎉🎉🎉 Party! 🎉🎉🎉

You have successfully deployed Laravel Spark! Go ahead and check it out.

Best Practices

The following these best practices are particularly useful and important when you're wanting to move to production and make sure your app is scalable.

Like any app on Codemason, your Laravel Spark app will be run in a container. Containers have an ephemeral filesystem by default. This means unless you mount a volume, any changes to the filesystem will be discarded when the container is upgraded. The changes outlined below mostly focused on preparing you for that.

Logging destination

Laravel logs errors and messages to disk by default. It's a good idea to change this so your logs are sent to the output stream.

The quickest way is to just change this is to just set your LOG_CHANNEL env variable to errorlog.

NOTE: You may want to consider integrating error tracking software as well (e.g. Sentry) to you track, manage and monitor your errors.

Learn more about Laravel Logging...

Sessions

By default, Laravel uses the filesystem for session storage and since the filesystem is ephemeral when you upgrade your app, the sessions stored on your disk will be lost and your users will be logged out.

Switching to the database session driver is a great solution to this and, like many things in Laravel, it is incredibly easy to do!

Create your sessions table migration:

php artisan session:table
php artisan migrate

Then set the SESSION_DRIVER env variable to database

Learn more about Laravel Sessions...

Cache

Laravel uses the filesystem for cache storage. Switch to the database driver to persist your cache.

Create your cache table migration:

php artisan cache:table
php artisan migrate

Then set the CACHE_DRIVER env variable to database

Learn more about Laravel Cache...

Storage

You will either need to mount a volume or use a driver such as Amazon S3 to store your files.

Mount a volume
If you don't intend to scale your app, you can simply mount a volume for your storage directory.

We will use a volume to replace the need to create a symbolic link from public/storage to storage/app/public - pretty cool!

mason services:upgrade pebble/web \
    --volume /home/my-app/storage:/app/storage/app \
    --volume /home/my-app/storage/public:/app/public/storage

S3 Driver
Switching to a cloud driver like s3 is particularly important if you intend to scale your app across multiple servers, as each instance of your app will need to be able to access the filesystem.

The s3 driver will persist your storage on Amazon S3,
granting each instance of your scaled app access to all the files.

Simply set the FILESYSTEM_CLOUD env variable to s3 and update your AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION, AWS_BUCKET, AWS_URL env variables accordingly.

Learn more about Laravel Filesystem


About Codemason

We ❤️ Laravel Spark. We used Laravel Spark for Codemason so we could focus on building functionality our users loved, instead of writing boilerplate code. Codemason is the Laravel Spark of servers/hosting 🙃

Working on a Laravel Spark project? People who use Spark know how important their time is. Save yourself hours of fighting with servers by using Codemason.

Don't forget to use the coupon SPARK-3-FREE to get 3 months free access!

Ben Magg
Author

Ben Magg

Founder of Codemason

Comments