Skip to content

Deployment

Guide for deploying the Backend

Deploy the Backend

We used Laravel Forge for managing the server because it’s easier for deploying Laravel without manually configuring the Server.

  1. Create an account on Laravel Forge.

  2. Connect your server to Laravel Forge. We used Hostinger because it’s affordable and offers plenty of RAM, allowing you to deploy your application for development and staging environment. However, you can use any server provider. See: Laravel Forge Supported Server Providers

  3. Add your PC’s SSH key to Your Server -> SSH Keys so you can access the server from your PC.

  4. Create a site for development and staging environment in Laravel Forge.
    Go to Server -> Sites -> Click new site

    1. Root Domain: You can ask the client to create you a dev.capturit.app and staging.capturit.app
    2. Project Type: Php/Laravel/Symphony (Default)

    Advance settings:

    1. PHP Version: In advance settings, select PHP 8.3.
    2. Database: You can put capturit_dev or capturit_staging

    Click the Add Site button

  5. Click the Environment tab

    Copy your .env and paste it in Site Environment section. You should change some of the environment variables:

    Site Environment
    APP_ENV=local
    APP_ENV=production
    APP_DEBUG=true
    APP_DEBUG=false
    APP_URL=http://localhost:8080
    APP_URL=https://<Root Domain>
    DB_DATABASE=capturit_local
    DB_DATABASE=capturit_dev
    DB_USERNAME=root
    DB_USERNAME=forge
    DB_PASSWORD=
    DB_PASSWORD=<Database Password>
    #For Staging Environment
    REDIS_DB=0
    REDIS_DB=1
    #For Staging Environment
    REDIS_CACHE_DB=1
    REDIS_CACHE_DB=2
    MAIL_FROM_ADDRESS=
    MAIL_FROM_ADDRESS=<Email address for dev/staging/production>
    CLOUDFLARE_R2_BUCKET=<local bucket>
    CLOUDFLARE_R2_BUCKET=<dev/staging/production bucket>
    SENTRY_LARAVEL_DSN=null
    SENTRY_LARAVEL_DSN=<Sentry Project DSN>

    Click the Save.

  6. Click Application tab, click Git Repository

    1. Configure the Repository and Branch
    2. Select the Database based on the name you assigned to it in Step 4
    3. Disable Run Migration

    Click the Install Repository and wait for it to install. After installing, enable this Laravel Services:

    • Laravel Scheduler
    • Laravel Horizon
    • Laravel Octane
    • Laravel Pulse

    Laravel Forge will automatically setup this services for you.

  7. Click the Deployments tab

    You can enable Quick Deploy if you want, but we usually disabled the Quick Deploy on staging and production environment.

    Add this to Deployment Script Section

    cd /home/forge/<Root Domain>
    if [ -f artisan ]; then
    $FORGE_PHP artisan down
    $FORGE_PHP artisan backup:run
    git pull origin $FORGE_SITE_BRANCH
    $FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader
    ( flock -w 10 9 || exit 1
    echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
    $FORGE_PHP artisan passport:keys #run only once
    $FORGE_PHP artisan migrate --seed --force #run only once
    #$FORGE_PHP artisan migrate --force #uncomment after running the command above
    $FORGE_PHP artisan pulse:purge --force
    $FORGE_PHP artisan optimize:clear
    $FORGE_PHP artisan optimize
    $FORGE_PHP artisan storage:link #run only once
    $FORGE_PHP artisan pulse:restart
    $FORGE_PHP artisan octane:reload
    $FORGE_PHP artisan horizon:terminate
    $FORGE_PHP artisan up;
    fi

    Click the Deploy Now button in the upper-right corner of the screen.

  8. Click SSL tab

    Click on Let’s Encrypt and then click on Obtain Certification. This will add an SSL certificate to your site.

  9. Click on the Root Domain above the Public IP. This will redirect you to the website.

  10. Add the Firebase JSON file to your project folder

    I prefer to use Visual Studio Code for accessing the project file using a remote connection. Requirements:

    • Visual Studio Code
    • Extension:
      • Remote - SSH
    1. Open Visual Studio Code.

    2. Open the Command Palette by running the following command:

      Ctrl + Shift + P
    3. Type Remote-SSH: Connect to Host and press Enter.

    4. Click the Add New SSH Host

    5. Enter ssh forge@server_public_ip and press Enter

    6. When prompted, enter the Sudo Password and press Enter.

      The connection to the server via SSH will begin.

    7. Once connected to the server, click Open Folder in the Explorer tab.

      A popup will appear. Select the folder named after your Root Domain.

    8. Add the Firebase JSON file to the following path:

      Storage -> app -> keys
  11. Setup Sentry

    Open a terminal and connect to the server via SSH

    Terminal window
    ssh forge@server_public_ip

    Install the Excimer extension via PECL:

    Terminal window
    sudo pecl install excimer

    More info: Sentry for Laravel

This should get the Capturit backend up and running.