Deploy a PHP Web App with Laravel and Docker


Laravel is one of the most popular web frameworks for PHP and for good reason. It comes bundled with most common web app needs, including authentication, authorization, localization, and support for multiple database backends including PostgreSQL.

In this guide, we’re going to deploy a simple Laravel 5.8 web app using Render’s native PostgreSQL and Docker support.

Let’s get started!

  1. Create a new PostgreSQL database on Render and copy the internal DB URL to use below.
  2. Fork render-examples/php-laravel-docker and create a new Web Service on Render, giving Render permission to access your forked repo.
  3. Select Docker for the environment, and add the following environment variables under the Advanced section:

    KeyValue
    DATABASE_URLThe internal connection string for the database you created above.
    DB_CONNECTIONpgsql
    APP_KEYCopy the output of php artisan key:generate --show

That’s it! Your Laravel web app will be live on your Render URL as soon as the build finishes. You can test it out by registering and logging in.

Modifying an Existing Laravel App for Render

The commit history of our sample repo is useful in understanding the modifications needed for an existing Laravel app.

If you haven’t already, make sure to run php artisan make:auth to generate authentication scaffolding for your app.

  1. Force HTTPS on all assets served by Laravel to avoid mixed content warnings in the browser. Render already manages and terminates SSL for you, but Laravel’s asset helper needs to be configured to serve everything over SSL. You can do this by following the changes in Force HTTPS for Laravel.

    In the end, the contents of app/Providers/AppServiceProvider.php should look something like this:

    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');
            }
        }
    }
  2. Configure your repo to deploy Laravel using Docker and NGINX. We’re building on the nginx-php-fpm Docker image as shown here, and adding php-fpm configuration for NGINX to tie everything together.

    Make sure to add the .dockerignore file to your repo to avoid adding unnecessary or confidential information to your Docker image.

  3. Finally, add a deploy script that will be run when your PHP app starts up.

    #!/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

You should now be able to deploy your existing Laravel app on Render. If you need help, feel free to email as at support@render.com.