Getting Started with Laravel on Render

Getting Started with Laravel on Render

Render is a unified cloud that enables you build and run all your apps, yes all your apps since it can use dockerized containers to serve your applications to the public.

Since Heroku discontinued their free tier services, Render has quickly become the de facto Heroku alternative offering exceptional services that allow you to easily scale your applications from hobby development to fully-fledged applications that can serve millions of users.

Render offers instant deployment for the following tech stacks:

  1. Web Services – Django & Flask, Rails & Express, GraphQL & REST
  2. Static Websites – Create React App, Gatsby and Hugo
  3. Background Workers – Celery, Sidekiq, RabbitMQ
  4. Cron Jobs – allows you to schedule and run any command, reliably and easily
  5. Dockerfiles – if you can Dockerize it you can run it on Render. You can run virtually anything with minimal setup using custom Dockerfiles.
  6. Private Services – you can run internal services on a private network with ease
  7. PostgreSQL – Spin up fully managed PostgreSQL databases with automated backups.
  8. Redis®️ – fully managed in-memory storage with live metrics

You can quickly see that Render is a powerful platform that reduces the time it takes to get from building to running your application. Getting started with Laravel on Render is pretty simple with some knowledge on Docker.

Pre-requisites

  1. Render account
  2. Github account
  3. Knowledge of Docker
  4. Knowledge of Laravel

Getting Started

  1. On your local machine create a new Laravel application by running the following command:

    laravel new laravel-on-render

  2. Next, we will create a repo on Github and push our Laravel application. You can find more details on how to do this on Github’s documentation here.

  3. Head over to Render and create a new account. You can use your Google, Github or GitLab accounts to quickly sign up.

  4. On the Render dashboard, click the blue action button and create a new PostgreSQL Database as shown below and enter your preferred db details.

    PostgreSQL Database on Render

  5. With the DB out of the way, create a new Web Service and give it permission to access the repo you had created earlier.
    Docker Web

  6. Select Docker as your runtime enviroment and update the Advanced section with the details below:

    KEY VALUE
    DATABASE_URL The internal DB connection URL. You can find this when you click Connect on your DB
    DB_CONNECTION pgsql
    APP_KEY The app key as generated by the Laravel command php artisan key:generate --show

    PostgreSQL Database URL

    PostgreSQL Internal Database URL

That’s all we need for the the Docker Web Service. Now we need to make a few changes to our code to allow Render to deploy our application.

Updating Laravel code for Render

  1. We need to force all requests on our application through HTTPS. Render automatically manages TLS for us but we need to tell Laravel to parse our assets in HTTPS. To do this open your app/Providers/AppServiceProvider.php and update the boot method as follows:
       public function boot(UrlGenerator $url)
        {
            if (env('APP_ENV') == 'production') {
                $url->forceScheme('https');
            }
        }
       
  2. Secondly, we need to configure our repository to deploy Laravel using Docker and NGINX. Our build will be based on the nginx-php-fpm Docker image.

    1. Create a .dockerignore file and add the following code

      		.ignore
      		/node_modules
      		/public/hot
      		/public/storage
      		/storage/*.key
      		/vendor
      		.env
      		.phpunit.result.cache
      		npm-debug.log
      		yarn-error.log
      		

    2. Create a Dockerfile and add the following code

              FROM richarvey/nginx-php-fpm:1.7.2
      
              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"]
              

    3. Under the folder conf/nginx/ create the file nginx-site.conf and add the code from this commit.
    4. Last but not least, we need a deploy script that will run after PHP starts. This include things like, running composer, caching and DB migration and seeding. Create this bash script under the folder scripts scripts/00-laravel-deploy.sh and add the code below:
      		#!/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 migrations...'
      		php artisan migrate --force
         

Awesome, we should now be able to deploy our Laravel application on Render. For any inquiries feel free to shoot me an email. Thank you for following along.


SNETTSCOM is an IT company. We excel at providing solutions in systems integration, consultancy, outsourcing, applications development, networking and security. Aside from this, SNETTSCOM also specializes in creative design and marketing.