GuidesPHPLaravel

Deploy Laravel

This article will teach you how to deploy your Laravel project on Zeabur.

Step 1: Environment Configuration

You can test and build locally first to make sure your project can run normally. You must apply your PHP version in composer.json for Zeabur to deploy.

{
  "require": {
        "php": "8.1"
  }
}

Step 2: Create a Project (Optional)

First, you need to initialize a Laravel project in your local development environment. We can follow the instructions in the Laravel official document to complete this step

After creation, initialize a Git repository for it and deploy it to your GitHub.

Step 3: Deploy Laravel Project

In your project, click the Deploy service or Add new service button, and select Deploy your source code.

deploy

Search for the Laravel Boot code repository you want to deploy, and click Import. Your Laravel Boot application will automatically start deploying.

Step 4: Add an APP_KEY for you Laravel project

Generating an application key or APP_KEY is one of the most important initial steps every time a Laravel developer creates or clones a Laravel application.

Switch to project variable setting page and enter your environment parameter.

You can generate a APP_KEY by enter this command under your Laravel project:

php artisan key:generate

APP_KEY

Connect to MySQL Database

Typically, your Laravel project will need to connect to a MySQL database. You can connect to a MySQL database using the following steps.

  1. You need to first install the ext-pdo_mysql extension on your Laravel project so that Zeabur can enable MySQL PHP support for you. Install this extension in your local environment using the following command:

    composer require ext-pdo_mysql

    Then commit the new composer.json and lock files and push them to your GitHub repository.

  2. Create a MySQL service and then, in your local environment, run the following command to migrate your tables to Zeabur:

    export DATABASE_URL=mysql://<username>:<password>@<host>:<port>/<database>
    php artisan migrate

    The <username>, <password>, <host>, <port>, and <database> mentioned above are your MySQL service details, which you can find in the “Instruction” section of the MySQL service page.

  3. On your Zeabur Laravel service page, set a DATABASE_URL environment variable with the following content:

    mysql://${MYSQL_USER}:${MYSQL_PASSWORD}@${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}

    The meanings of ${MYSQL_USER} and other variables can be found in the MySQL documentation’s environment variable section.

Automatic Optimization

When building a Laravel service, Zeabur runs these optimization commands 1:

php artisan optimize
php artisan config:cache
php artisan event:cache
php artisan route:cache
php artisan view:cache

You can disable these automatic optimization commands by setting the PHP_OPTIMIZE=false variable. Then, use custom build commands to fine-tune your optimization process.

Vite / Laravel Mix Support

Zeabur automatically runs npm install and npm run build if a build script exists in your package.json. Since both Vite and Laravel Mix define a build script in package.json by default, Zeabur automatically compiles these assets when building a Laravel application.

Footnotes

  1. https://laravel.com/docs/11.x/deployment#optimization