Running Laravel apps on CapRover šŸ³

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

Template to deploy a Laravel app in docker for CapRover

Dockerfile

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

  1. āœ… 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