Running Laravel apps on CapRover 🐳

@jackbrycesmith

Jack Bryce-Smith

Β· 3 min read

CapRover is an impressive open source tool that makes it easy to run various backend services on a rented VPS.

We can use it to help run Laravel apps confidently in production, rivalling benefits provided by services such as Laravel Forge.

Here’s the setup I’ve been using to successfully deploy to my own server:

jackbrycesmith/laravel-caprover-template

DockerfileView on GitHub β†’

Prepping CapRover

Assuming you’ve got CapRover up and running, setup a database server of your choosing.

This is a case of a few clicks in the interface with one of their β€˜One-Click apps’.

CapRover one-click database setup

You could also add a database in one of these servers by installing Adminer directly from the dashboard. But do remember to enable Basic Auth in the 'HTTP Settings' of the app.

Then keep a note of the credentials for the DB server; you'll be entering them alongside other .env contents into the dashboard of the 'app' that will hold your Laravel code.

DB_CONNECTION=mysql
DB_HOST=srv-captain--mysql
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=root
DB_PASSWORD=reallysecurepassword

Create your app inside CapRover

The only thing you need to consider is whether to make it have persistent directories or not.

Both choices are fine. If you want to have filesystem changes persisted between deploys in this Laravel context you would add /srv/app/storage as the 'Path in App' in the 'App Configs' section of the dashboard.

πŸš€ Future Scaling Consideration...

The transition to 'Serverless' hosting with something like Laravel Vapor or Google Cloud Run will be smoother if you don't rely on the local filesystem for your app.

Whilst you're inside the 'App Configs' section, enter all the relevant environment variables & hit 'Save & Update'.

Using the template

/.deploy/Dockerfile

  • Install any other required PHP extensions
  • Copy any other required environment variables as Docker ARGs.

/.deploy/entrypoint.sh

  • Run any php artisan commands here

Need Laravel Horizon? Easy as...

  1. Create a Redis 'One-Click app'
  2. Add appropriate environment variables in dashboard
  3. Uncomment this section in .deploy/config/supervisor.conf

How about Websockets?

  1. Install the fantastic Laravel Websockets package
  2. Add appropriate environment variables in dashboard
  3. Uncomment this section in .deploy/config/supervisor.conf
  4. Enable 'Websocket Support' in CapRover dashboard

    Enable NGINX proxy header upgrade for websockets in CapRover app dashboard

  5. βœ… Websockets will be available on port 80/443 of your configured domains!

This is easier than what is involved for setting up in Laravel Forge.

Final tips

  • πŸ” Avoid mixed content errors caused by asset() usage, by forcing HTTPS in your AppServiceProvider.php...
if (config('app.env') === 'production') {
    \URL::forceScheme('https');
}

Cleaning up old docker images in the CapRover dashboard