New greener region discount. Save 3% on Upsun resource usage. Learn how.
HomepageHomepageBeta
LoginFree trial
FeaturesPricingBlogAbout us
Blog

Upsun: the missing PaaS to scale Laravel applications

PaaSPHPconfigurationGitLaravel
03 January 2024
Thomas di Luccio
Thomas di Luccio
DevRel Engineer

Laravel is a simple, scalable, secure PHP framework powered by a vibrant and active community. It’s the starting point for many great web applications and online businesses, designed to allow developers to focus on creating value for their customers and users, not battling with a rigid framework.

Upsun and Laravel both share the same belief that developers should focus more on building features and exploring new technologies and grounds, and less on recreating the tools supporting their ventures again and again. With so much shared DNA and qualities, it’s safe to say that Upsun is the perfect PaaS for hosting and scaling Laravel applications.

Create without sweating the small stuff

It’s about focusing on what matters most: being free to create great applications without sweating the small stuff. For all the web artisans, Upsun helps you reach the stars with your Laravel apps, one deployment at a time.

Upsun empowers development teams with the flexibility to build—and the firepower to run—diverse applications on a single, self-service PaaS. By automating infrastructure management and security, Upsun frees every developer to easily experiment, quickly iterate, and responsibly deploy applications at scale. But, how?

Whatever the complexity of your applications, Upsun empowers you to fully control your environment through your terminal and YAML files. We’re here to take you through the steps on how to do this by running your Laravel applications on Upsun—see full documentation here.

Upsun CLI for the web artisans: how to set it up

First, download and install the Upsun CLI, where you can then discover all Upsun commands with upsun list—everything you’ll need to boldly code what no developer has coded before is here.

Once you have the Upsun CLI; you will need a project. You can clone your Laravel application from its Git repository or create a new Laravel application from scratch and initialize a Git repository.

We recommend you initialize a Git repository before creating an Upsun project as this one will then be your main environment track to your main branch. Otherwise, the main environment might track a main branch while your default branch may be a master and therefore handled as a secondary environment.

It’s now time to create an Upsun project with upsun project:create. Alternatively, you can create a project with the UI, and fetch it locally with upsun get PROJECT_ID.

The Upsun CLI has a convenient project:init command designed to bootstrap configurations and help you get started in no time. This command has a cool shortcut: upsun ify. It’s as simple as that, but how does it work?

Upsunify your Laravel app

The upsun ify command automatically detects the framework, runtime, and dependency managers being used. It’s then up to you to name your Upsun project and select the different services you intend to use.

The upsun ify command provides a list of services the configuration of which will be bootstraped for you. Use arrows to move the selector, space to select a service, and type to filter the list.

You can add as many services to your application as you wish—take a look at the documentation to know everything you need to shape your Upsun project to your liking.

Depending on application complexity, the upsun ify command might not generate the whole configuration files for you but will get you started with 80-90% of it ready. Our stack guides, documentation, and Discord community are also always on hand to help you complete a top-notch configuration in no time.

Laravel unleashed: crafting a decoupled ecosystem ready to scale

Upsun is the PaaS made to help you scale your Laravel applications in any direction you need to, regardless of how seemingly complex your application may be. You may want to scale your project horizontally by handling more technologies. Your project might not be a single Laravel application. It might be bundled with workers, web services in NodeJS and Go, and a data layer in Python. This is the tech ecosystem, with Laravel at its core, needed to power your company product or service. And Upsun can help you manage it.

Our documentation provides a guide for building multiple applications Uspun projects. The .upsun/config.yaml file contains an application’s top-level key filed with the configuration of your first applications. You could either choose to add more entries to this file or split the configuration into multiple files, one per application. While provisioning the infrastructure for our project, Upsun also globes all of the YAML files into the .upsun folder in one meta-configuration file.

Configure writable cache directories

Laravel requires specific cache folders to be writable. Upsun requires us to explicitly define which folders are writable after the build—these folders are called mounts. To ensure the bootstrap/cache and storage directories are writable, check the mounts definition of your .upsun/config.yaml file, as seen below. A Laravel-specific cache folder should be created while the application is being deployed if one is missing.

applications:
      app:
        ...
        mounts:
          ...
      
        "bootstrap/cache":
          source: "storage"
          source_path: "cache"
                     
        "storage":
          source: "storage"
          source_path: "storage"

Multiple hooks run as part of the process of building and deploying your application. These are places where you can run custom scripts, such as this deploy hook that ensures caches are correctly set up and warmed:

applications:
      app:
        ...
        hooks: 
          deploy: |
            set -eux
               
            # ensure the cache directories are available
            mkdir -p storage/framework/cache/data
            mkdir -p storage/framework/views
            mkdir -p storage/framework/sessions
               
            # clear all caches and dumped files
            php artisan optimize:clear
               
            # run all available migrations
            php artisan migrate --force

Hooks provide even more automation superpower as you’ll be able to run several commands at a critical moment of the deployment lifecycle including build, deploy, and post_deploy.

A bridge between Laravel and Upsun

Laravel expects all configurations to come in through environment variables with specific names, in a specific format. Upsun provides configuration information as environment variables in a different specific format.

The platformsh/laravel-bridge library maps the Upsun variables to the format Laravel expects for common values and makes our artisans' lives much easier. Let’s add it with:

composer require platformsh/laravel-bridge

Craft for sustainable growth: allocating resources as demand soars

In today's era of digital sustainability, developers must remain firmly in the driver's seat. Being in control ensures optimal resource allocation, directly correlating with an application's success, and carries a larger environmental responsibility.

Excess resource usage isn't just a matter of wasted costs; it directly translates to unnecessary carbon emissions. By meticulously tuning resources to match an application's growth trajectory, developers can create efficient, high-performance applications without the ecological baggage. Upsun aims to empower developers with this dual mission: fostering digital growth while championing environmental stewardship.

The upsunify command and the documentation should have let you describe the infrastructure needed for your application. Then from the first deployment, resource allocation is done by default with default resources which can be customized at any time.

Full control of resource allocation

Your project definition should then be taken into account. You still have to define what resources should be used to run your application and this will be the final requirement. It’s a two-step process having you describe your infrastructure in a YAML file and defining the resource allocation to control the costs.

Let’s say you defined a backend application powered by Laravel, a frontend using NodeJS or Bun, and a MySQL database. We now have to ensure that each of those containers benefits from the optimal amount of resources.

Let’s have a look at what happens behind the scenes to fully understand the possibility you now have. All the images available to you have a default profile: BALANCED, HIGH_CPU, HIGH_MEMORY, depending if you want to favor CPU or memory, or prefer an equilibrium between both requirements. This translates into a ratio of memory per CPU unit.

Images come with an optimized default profile value that will be the preferred choice in most cases. But, we want you to be in the driver's seat. You can eventually override the default value and define explicitly which profile you want to use by extending the services definition in your .upsun/config.yaml file.

services:
      database:
        type: mysql:10.6
        container_profile: HIGH_MEMORY

Once this is defined, or most probably left as it is, we are going to tell how much CPU, therefore memory as well, we want to allocate each container. This container size can be 0.1, 0.25, 0.5, 1, 2, 4. The quantity of memory associated with each CPU level is available in the documentation.

Our node frontend is super lightweight and only requires 0.1 CPU, the minimum. We made a lot of improvements on our backend (thanks Blackfire!), and 0.25 is enough to deliver. Our database is massive and needs more resources to stay fast:    

upsun resources:set --size frontend:0.1,backend:.25,database:1

Our backend requires redundancy. Let’s give it 3 instances:

upsun resources:set --count backend:3

Let’s finish our configuration by giving 512 MB disk to the backend application and 2 GB to the database service:

upsun resources:set --disk backend:512,database:2048

No need to redeploy (upsun redeploy). Our changes have already been taken into account. This also means that only a couple of CLI commands are required to increase, or lower, the resources allocated in the future.

Imagine you are about to launch a new exciting product that will drive large crowds to your site. A few commands just before the “One more thing” moment in your keynote will ensure your application stays afloat and continues to deliver even with intense and sustained traffic.

Observability on every level

Each Upsun project comes with a complete Observability solution empowering developers and operations teams with the capability to oversee their applications, pinpoint errors and anomalies, and proactively address issues before they escalate.

Upsun observability tools include:

With observability on every level, developers can make data-driven decisions and gain valuable insights into the behavior of their applications, enabling them to continuously enhance their systems for optimal performance and efficiency.

Git-driven infrastructure

Not only will Upsun empower developers by letting them describe the infrastructure they need in simple YAML files. It also has Git be the single source of truth. Especially, each environment created on your project is bound to git branches.

Let’s imagine you want to work on a specific feature for your application. You’ll create an environment based on your production environment, and most likely your main branch. Upsun will automatically provision a clone of your application, with all its related services and data based on the last available backup. You will even have a development URL provisioned as well so you can experience your feature and collaborate with your team.

There is an integration with GitHub and the main third-party Git providers you might want to consider. This integration lets you have environments automatically provisioned for you every time you create branches, pull, or merge requests. Merging a branch automatically redeploys the target environment. How cool is that?

You therefore have entire control over your production and non-production environments. This is also true for resource allocation. You can decide to reduce the resources allocated to the different containers or your non-production environment with the same upsun resources:set command while keeping your production environment resource requirements high.

Web artisans, the world is yours. What will you build next with Laravel and Upsun?

In conclusion, Upsun is purpose-built to give developers the best of both worlds: the ability to scale resources in sync with their Laravel application’s growing demands, while also being environmentally conscious. The developer's role has never been more critical; you're not just coding an application, you're architecting the future of digital sustainability and performance.

We understand that no development journey is undertaken alone. It's a collective endeavor that thrives on shared knowledge and collaborative problem-solving. That's why we invite you to continue the conversation and share your experiences, questions, or insights on our dedicated social media channels.

Come join a space where Laravel developers like yourself can collaborate, learn, and grow together in crafting the next generation of scalable, decoupled, and eco-friendly applications. We can’t wait to hear what you're working on and how we can better support you in your development journey.

Let’s build anything together with Laravel and Upsun.

Upsun Logo|Powered by Platform.sh
Join the community