Question

Laravel issue with envvars

Hello at all,

I’m deploying a Laravel application with DO Apps Platform. Deployment works but I have an issue with the App environment variables.

I did set in the DO Console

DB_HOST=${db-mysql-fra1.HOSTNAME}
DB_PORT=${db-mysql-fra1.PORT}
DB_DATABASE=${db-mysql-fra1.DATABASE}
DB_USERNAME=${db-mysql-fra1.USERNAME}
DB_PASSWORD=${db-mysql-fra1.PASSWORD}

In the build process, I added a php artisan migrate --force but it fails with: php_network_getaddresses: getaddrinfo for ${db-mysql-fra1.HOSTNAME} failed. The envvar is not translated. Once launched, I have the same issue with php-fpm. If I do:

$ php artisan tinker
> echo env('DB_HOST'); // db-mysql-fra1-user.b.db.ondigitalocean.com
> echo config('database.connections.mysql.host'); // ${db-mysql-fra1.HOSTNAME}
> exit
$ php artisan config:clear
$ php artisan tinker
> echo config('database.connections.mysql.host'); // db-mysql-fra1-user-0.b.db.ondigitalocean.com

So I must manually clear the config cache to make it work. Of course, I added the php artisan config:clear in the build phase commands but same:

php artisan cache:clear
php artisan config:clear
php artisan migrate --force
npm run build

I must add that I previously set a php artisan optimize in the build commands but I removed it as I thought this was the problem.

Thanks in advance,

Axel


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 18, 2023
Accepted Answer

Hi there Axel,

Happy to hear that you’ve managed to figure out most of the issues.

Regarding the database migrations error that you are getting, you need to move the migrations step out of the build stage to the run stage.

Here is an example of a Laravel app with the same setup where the migrations step is executed right before the run command itself:

https://github.com/thedevdojo/wave/blob/main/.do/deploy.template.yaml#L9

In general it is always a good practice to run your database migrations during the deploy stage rather than the build stage, as if you make some database schema changes that are not backwords compatible during the build stage, you might end up in a situation where your running app is still using the old code base but with the new database schema causing an outage.

Let me know how it goes!

Best,

Bobby

I’m starting to uncover the answer: I set FILESYSTEM_DISK=s3 for testing and forgot to revert it. Rollbacking to FILESYSTEM_DISK=local fixed the php-fpm environment. But not the build phase with php-cli.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel