3 cách set biến môi trường trong CLI in Centos 7/ Linux/Debian … – 3 ways to set environment variables in Command Line Interface

Way 1 :

The easiest way, but it’s only available in this session, when you logout , or login for new session , the environment variable is not existed.

Run below command :

export APPLICATION_ENV='staging'

And check value of variable APPLICATION_ENV

echo $APPLICATION_ENV

And login for the new session . The environment isn’t staging, it’s dev ( i had set it before , that way in Way 3 )

Check this environment variable in PHP CODE  in old session

Result :

 

Way 2 :

  • When you use this way, the environment variables will set for your user, not for all users in your OS. So if your have more than one , you should use this way to set environment variables. It’s will not conflict with another user.
  • If you logout, and login with your user, it’s will be existed.

You can use vim, nano, vi …etc

vim ~/.bash_profile

Add those lines and save

APPLICATION_ENV=production

export APPLICATION_ENV

Then logout and login. And test.

Test with PHP CODE i wrote in way 1

 

Way 3 :

This way will set environment variables for all users in your OS.

I choose this way, because i’m root =))

vim /etc/environment

Then add this line

APPLICATION_ENV=dev

Then save & close the editor.

You don’t need to logout and login, it effect after you save .

 

Then this with PHP CODE

Note : bài này mình tự viết bằng tiếng anh nên có sai sót gì thì chỉnh giúp mình nhé. Thanks

5 (100%) 1 vote