DEV Community

Lucas Lima
Lucas Lima

Posted on

Deploying a Laravel Breeze App on Render: Step-by-Step Guide (Part 1)

On this tutorial I'll explain how to deploy a laravel app using Breeze starter kit on Render.

Render is a cloud application platform that allows users to perform automated deploys of their projects hosted on any git repository with many features like logs and metrics monitoring, scaling, automating and much more, check out all of their features on this link.

1. Configuring The docker environment

In order to deploy a Laravel app on our render workspace we'll first need to configure a docker environment with a Dockerfile, Nginx conf and a deploy bash script.

2. .Dockerignore

First, we'll need to create our .dockerignore file on the project root folder in order to specify wich files will not be included on the build ṕrocess, so our docker image become lighter.

/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
Enter fullscreen mode Exit fullscreen mode

3. Creating the Dockerfile

On this step we'll create our dockerfile on the prioject root folder, specifying all the resources that we need to run our laravel app with breeze starter kit

FROM richarvey/nginx-php-fpm:latest
# Certifique-se de que o sistema está pronto para instalar pacotes
USER root
RUN apk update && \
    apk add --no-cache curl nodejs npm && \
    npm install -g npm@latest
COPY . .
# Image config
ENV SKIP_COMPOSER 1
ENV WEBROOT /var/www/html/public
ENV PHP_ERRORS_STDERR 1
ENV RUN_SCRIPTS 1
ENV REAL_IP_HEADER 1

# Laravel config
ENV APP_ENV production
ENV APP_DEBUG false
ENV LOG_CHANNEL stderr

# Allow composer to run as root
ENV COMPOSER_ALLOW_SUPERUSER 1
CMD ["/start.sh"]
Enter fullscreen mode Exit fullscreen mode

On this file, we added this specific instruction:

RUN apk update && \
    apk add --no-cache curl nodejs npm && \
    npm install -g npm@latest
Enter fullscreen mode Exit fullscreen mode

That will allow us to install nodejs on our image in order to run vite, which is included in laravel breeze.

4. Nginx configuration

Now we'll create a directory on our project root folder named conf/nginx and place the following ngix-site.conf file:

server {
  # Render provisions and terminates SSL
  listen 80;
  # Make site accessible from http://localhost/
  server_name _;
  root /var/www/html/public;
  index index.html index.htm index.php;
  # Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
  sendfile off;
  # Add stdout logging
  error_log /dev/stdout info;
  access_log /dev/stdout;
  # block access to sensitive information about git
  location /.git {
    deny all;
    return 403;
  }
  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-XSS-Protection "1; mode=block";
  add_header X-Content-Type-Options "nosniff";
  charset utf-8;
  location / {
      try_files $uri $uri/ /index.php?$query_string;
  }
  location = /favicon.ico { access_log off; log_not_found off; }
  location = /robots.txt  { access_log off; log_not_found off; }
  error_page 404 /index.php;
  location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp|tiff|ttf|svg)$ {
    expires 5d;
  }
  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    include fastcgi_params;
  }
  # deny access to . files
  location ~ /\. {
    log_not_found off;
    deny all;
  }
  location ~ /\.(?!well-known).* {
    deny all;
  }
}
Enter fullscreen mode Exit fullscreen mode

5. Build sh script

Finally, on our project root folder we'll create a scripts directory and inside a sh build script named 00-laravel-deploy.sh used by render to run bootstrap laravel commands like migrations, composer install and also ou npm commands, needed by vite.

#!/usr/bin/env bash
echo "Running composer"
composer global require hirak/prestissimo
composer install --no-dev --working-dir=/var/www/html
echo "Caching config..."
php artisan config:cache
echo "Caching routes..."
php artisan route:cache
echo "Running npm install..."
npm install
echo "Running npm build..."
npm run build
echo "Running migrations..."
php artisan migrate --force
Enter fullscreen mode Exit fullscreen mode

6. Oops, I almost forgot

There's a configuration in Laravel app Service provider so our app can run using https, let's add in to our app/Providers/AppServiceProvider.php file:

namespace App\Providers;

use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    // ...

    public function boot(UrlGenerator $url)
    {
        if (env('APP_ENV') == 'production') {
            $url->forceScheme('https');
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Push to a github repository

Now, we have to push this project to a github repository, and in the next part of this tutorial we'll access Render admin panel and run our application on the cloud!

Part 2

Top comments (0)