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:
Template to deploy a Laravel app in docker for CapRover
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ā.
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ā¦
- Create a Redis āOne-Click appā
- Add appropriate environment variables in dashboard
- Uncomment this section in
.deploy/config/supervisor.conf
How about Websockets?
- Install the fantastic Laravel Websockets package
- Add appropriate environment variables in dashboard
- Uncomment this section in
.deploy/config/supervisor.conf
- Enable āWebsocket Supportā in CapRover dashboard
- ā 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 yourAppServiceProvider.php
ā¦
if (config('app.env') === 'production') {
\URL::forceScheme('https');
}
- š¤¦āāļø Reduce risk of running out of server disk space by setting up automatic cleanup of old images, or remember to do it manuallyā¦