add new env file to laravel for new server environment - laravel

i'm taking over a project from a client to add functionalities. I quite new in the development of Laravel 5.5
There are already 3 .env files in my project:
.env
.env.example
.env.live
.env.live is for production (APP/ENV=production) and runs on aws, the other 2 are for local use.
Now I have a new AWS EC2 server, database and so on and need a new env file to use these new instances. Let's say I create a new .env file called .env.dev
How how can I switch between the .env.dev and .env.live? Or how do I use this .env.dev?

If you're using Elastic Beanstalk, go to your environment, then click the Configuration menu item. From there click "Modify" in the "Software" section. A page will appear where you can add Environment Properties as key->value pairs.
You can then delete the .env files. These properties will be read by your Laravel app just like it reads the .env files.

You can also use the symbolic links
Just Delete .env from both of the server and create a symbolic link like.
ln -s .env.live .env // for live
ln -s .env.dev .env // for dev server
You can also keep this command inside your deployment script if you are using any CI/CD.

On every server you only need .env file to execute. If you have three files
.env
.env.live
.env.example
.env.other
you can create multiple files with .env.*. But when you will execute your application then your .env file will execute.
Now if you want to add new additional settings to your environment then you just need to add in .env file. and also specify all other .env.* files and change values according to server.
When you will deploy your application on live server. Then you just need to rename your .env.live file to .env and set configurations to that file.
cp .env.live .env
Note: if you already have .env file on your server(as you said your application is already on production server.) then you just need to add your environment settings to that file.

Related

How to avoid git sync of prisma .env files

When I set up prisma in my project, by following this article, I can set up .env and set database url and password.
After that,DB connection was established and some tables were migrated. But I am wondering that Are there any problem to add .env to .gitignore ?
password is hardcoded and this credentials are not pushed.
If someone has experienced this problems or know other options, will you please let me know.
It is strongly recommended that the env files are added to .gitignore.
Executing npx prisma init should create a .gitignore file and a .env file along with creating prisma folder. The default behaviour is that the env file gets added to the .gitignore file so that the credentials are not accidentally pushed to source control.
Here is a reference for using multiple env variables or using prisma without any env variables.
There should be no issues on adding .env to .gitignore file.

Laravel - Why laravel ask to rename .env.example to .env while .env already exist?

In Laravel 6 Documentation second paragraph of Application Key written like this
Typically, this string should be 32 characters long. The key can be
set in the .env environment file. If you have not renamed the
.env.example file to .env, you should do that now. If the application
key is not set, your user sessions and other encrypted data will not
be secure!
Why did they ask to rename it? I also found this paragraph in the Laravel older version. Is there any difference between those since they have the same content but different name?
If you've install Laravel using composer command.
composer create-project laravel/laravel projectname
you don't need renamed the .env.example file to .env. If you installed Laravel via Composer or the Laravel installer, this key has already been set for you by the php artisan key:generate command.
If you clone project using git clone some folder is ignored by git so you might not get env file as well as vendor folder. Therefore, they will have to manually enter php artisan key:generate for their app to function correctly.
More info SO answer
Laravel need to use .env file to define a database connection, some general setting like application key as well. So if you have no .env file, your Laravel has not a setting for now.
Like they said, If the application key is not set, your user sessions and other encrypted data will not be secure! You need to create / copy /remove the .env.example to the new .env for this reason. for letting our Laravel knows about general config.
By the way, do not use the .env.example like copy-and-paste because it's an example. you need to change the value config to your own.
The simplest way is move it on your server with Filezilla or another FTP program. Rename file, and re-download it on your computer. It works for me last time :)
The .env.example the file is just an example of the .env file. It is not used by the app. It is used to serve as a base for you to edit and rename.
In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.

DigitalOcean: I want to download the .env file from server where laravel application installed

I am working on Laravel application on a DigitalOcean server. To speedup my programming I am going to clone my project to my local machine. But I did not find the .env file on the server.
Anyone, please help me getting the .env file from the DigitalOcean server.
The .env file is not tracked by git on the server, so when you git clone the repository from the server, it will not be included. Depending on the complexity of your project on the server, the .env file might include custom variables that are required for the project to function properly. So your best bet is to get a copy of the .env file from the server.
To manually copy the .env file from the server to your local machine using scp or similar tool:
$ scp username#server:/repo/.env /some/local/directory
Or, if you are using Laravel Forge to deploy to the DigiitalOcean server, you could log in to Forge and copy the contents of the .env from the interface.
But don't forget to customize the .env afterwards for your local environment (database credentials, etc).
In laravel project , .env file exit in the project root file, that does not matter if it is local or live.
So if you can not find the .evn file in your digital ocean server it is likely that you dont have it in your application .
What you can do is make a copy of .env.example and rename it to .env file.

where the .env file goes on the web server in laravel 5.6

I am confused about .env file as when I hosted the laravel application the .env file is missing on the web server. If anybody knows the way how can we find .env file on the server please answer.
Try ls -la to list all files in your web root.
As other have said, it sounds like the file exists but you cannot see it because it is a hidden file (denoted by the . in .env)
For future reference, whilst .env is likely to be excluded in a .gitignore there is often a .env.example which is include that you can copy and modify for your environment.
By default a Laravel app's Git repo will ignore the .env file - it's because you will use this on your development machine to configure the app to work on that environment.
You should have another .env file on your server which provides the configuration required for your app to run in that environment. This file should be maintained between deployments - and most importantly, the credentials inside of it should not be tracked in a repo.
Your .env file is there! It is a hidden file and you can't see it with gui unless if you somehow reveal hidden files! I recommend you to use putty over windows or ssh over linux terminal and edit it if you know how to do that.
By default some files like .env, .htaccess are hidden on the server therefore to see these files we need to set as show hidden files on the server settings.

Laravel - .env vs database.php

I am confused by Laravels .env file VS other settings
I have a .env file which has my sql database settings inside, but I also have a file in config/database.php that has these settings inside.
When I deploy my application to an Elastic Beanstalk instance, which of these files is it using for the database settings?
.env is short for environment and thus that is your environment configurations.
The database.php configuration contains non-critical information.
You obviously won't have your database's username's password in your source control or available in the code.
In order to keep everything safe or to keep information saved that is environment-defined... you keep them in .env file
Laravel will prioritize .env variables.
A little more detailed answer:
The config Files are where you store all configurations. You shouldn't add any username, passwords or other secret informations in them, because they will be in your source control.
All secret informations and all environment dependant informations should be stored in your .env file. With this you can have different configuration values in local/testing/production with just a different .env file.
In your config files you access the information in you .env files, if necessary.
When use what from another answer
use env() only in config files
use App::environment() for checking the environment (APP_ENV in .env).
use config('app.var') for all other env variables, ex. config('app.debug')
create own config files for your own ENV variables. Example:
In your .env:
MY_VALUE=foo
example config app/myconfig.php
return [
'myvalue' => env('MY_VALUE', 'bar'), // 'bar' is default if MY_VALUE is missing in .env
];
Access in your code:
config('myconfig.myvalue') // will result in 'foo'
In your ".env" file you have your settings. in the ".php" files like your "database.php" file this is the default value for the property and normally, the corresponding value in the ".env" file is use here with this syntax : 'database' => env('database', 'default_value'),
The .env file is the first file it will use for configs. In case values are missing inside the .env file Laravel will check the config files. I primairly use those as backups.

Resources