lorisleiva / laravel-docker

🐳 Generic docker image for Laravel Applications
MIT License
927 stars 313 forks source link

Best way for database connections #59

Closed alexis-riot closed 3 years ago

alexis-riot commented 3 years ago

Hello,

What is the best way to link database connections using Laravel Deployer? At this time, it's only cloning the .env.example to .env and do not edit the DB credentials. If you can explain me the best way to do that safely ?

lorisleiva commented 3 years ago

Hi there 👋

I wouldn't have DB credentials in the repository. I simply deploy with .env as a shared_files (which is set by default) so that it is not affected by my releases and I manage environment variables directly on the server (using Laravel Forge in my case).

If this is not an option and you need a dynamic way to update your environment variables you can create your own custom tasks to achieve something that works best for you. For example, if you deploy using GitLab CI/CD you could create a custom task that takes secret variables from GitLab and updated the .env file accordingly.

alexis-riot commented 3 years ago

What file i'm supposed to modify using Laravel Deployer to change credentials ? I have try differents things like change .env, .env.example, etc... But I have already the same error:

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

(that is the default configuration env and don't take my changes)

lorisleiva commented 3 years ago

Yes because your .env file is specific to your local environment and should not be shared with your production server. Instead, once you've deployed you need to connect to your server via SSH and edit your.env file directly there. It should be in /your/deploy/path/shared/.env by default.

alexis-riot commented 3 years ago

Can you explain me why are you copying the .env.example in these two files, so ?

https://github.com/lorisleiva/laravel-docker/blob/master/gitlab/.gitlab-ci.deployments.yml#L28 https://github.com/lorisleiva/laravel-docker/blob/master/gitlab/.gitlab-ci.tests.yml#L18

The solution of the shared .env is really great for me but I don't understand why we are copying two others environments file.

alexis-riot commented 3 years ago

And an other thing (that is not directly lie to this question..)

The stream or file "/home/admin/web/path-file/public_html/releases/1/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied

Can you explain why after a deploy I got this error when I go on the site ?

Thanks a lot 💯

lorisleiva commented 3 years ago

Oh, I see why this would be confusing. This .env is not what will be sent to your production server. This is just the .env file that will be used within the GitLab pipeline. Otherwise, we'll get some errors from Laravel complaining that the .env file does not exist. If your test required a database, they will usually override some .env variable directly in the phpunit.xml file. This usually uses a sqlite database connection and a :memory: database so you don't actually require any extra set up.

If you need to provide some specific environment variables for your GitLab pipeline, you could inject them from GitLab's variable system or (simpler) copy from a specific file like cp .env .env.gitlab.


Regarding your other question, this seems to be a permission issue on your storage folder. This yaml job should normally fix that for you.

https://github.com/lorisleiva/laravel-docker/blob/1ef318747352014e00f4fa3cfbf46dbc1feb853a/gitlab/.gitlab-ci.deployments.yml#L16-L18

alexis-riot commented 3 years ago

Hey @lorisleiva. Thanks you.

I will take the last option:

or (simpler) copy from a specific file like cp .env .env.gitlab.

The problem is that the .env file is located inside a shared/ file that is generated on the prod/staging server by Laravel Deployer. Do you know how can I disable this behavior to make a simple copy of my environment file ? Or if it's not possible, an other solution ?

I'm a little bit confused with this shared/.env file that is generated automatically by Laravel Deployer and I have not have my hands on it.


It's fixed.

sudo chown -R admin:admin shared/storage fix the problem (using VestaCP).