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
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...