Getting Started with Laravel on Render
by snettscom
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:
- Web Services – Django & Flask, Rails & Express, GraphQL & REST
- Static Websites – Create React App, Gatsby and Hugo
- Background Workers – Celery, Sidekiq, RabbitMQ
- Cron Jobs – allows you to schedule and run any command, reliably and easily
- Dockerfiles – if you can Dockerize it you can run it on Render. You can run virtually anything with minimal setup using custom Dockerfiles.
- Private Services – you can run internal services on a private network with ease
- PostgreSQL – Spin up fully managed PostgreSQL databases with automated backups.
- 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
- Render account
- Github account
- Knowledge of Docker
- Knowledge of Laravel
Getting Started
-
On your local machine create a new Laravel application by running the following command:
laravel new laravel-on-render
-
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.
-
Head over to Render and create a new account. You can use your Google, Github or GitLab accounts to quickly sign up.
-
On the Render dashboard, click the blue action button and create a new PostgreSQL Database as shown below and enter your preferred db details.
-
With the DB out of the way, create a new Web Service and give it permission to access the repo you had created earlier.
-
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 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
- 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'); } }
-
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.
-
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
-
Create a
and add the following codeDockerfile
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"]
- Under the folder
conf/nginx/
create the filenginx-site.conf
and add the code from this commit. - 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 scriptsscripts/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.
Recommended Posts
GitHub Actions Deploy through SSH
22nd February 2023
How to Stay Organized with Microsoft Edge Vertical Tabs
9th March 2021
What is Growth Marketing? An Introductory Guide
19th February 2021