PHP Add $_env variable

Source: Internet
Author: User
Tags php cli phpinfo

In the daily development process, we often write some systems or modules in the configuration file. This facilitates the maintenance and modification of the program. The usual configuration files are. ini,. xml, and so on. The benefits of the configuration file are:
1, easy to manage.
2, high readability.
However, the use of configuration files can also be bad, namely:
1, poor security. If the encryption method is used, it will cost the decryption performance.
2, multi-server deployment, it is easy to cause file content differences, thus affecting the normal operation of the program.
To address the above security issues, it is common practice to set content in the system and configure it using environment variables. Let's take a look at how to configure environment variables in LNMP (Linux + Nginx + Mysql + php-fpm + PHP).

[Operation]:
Environment: Debian Linux OS
1, set the system environment variable:
#vi/etc/profile
If you add an environment variable:
Export my_redis_host= "localhost"

2, modify the php-fpm.conf, set the PHP environment variable:
#vi/etc/php5/fpm/php-fpm.conf
Increase:
env[my_redis_host]= $MY _redis_host

3. Modify the PHP-FPM startup script.
Because each time you want the system variable to take effect, it must be source/etc/profile, and it needs to be done when PHP-FPM starts. Therefore, the PHP-FPM startup script needs to be modified.
If installed with Apt-get, then:
#vi/ETC/INIT.D/PHP-FPM
After defining the environment variables such as path, add a row:
. /etc/profile
Note that there is a space between the dot and the path. In fact, it is the same as source, but here the source will make an error.
Also to modify the permissions of the/etc/profile, because PHP-FPM start the user is Www-data, and/etc/profile the rights of the user is root, here for the prospectus, the/etc/profile permission to set to "777", namely:
#chmod 777/etc/profile

4. Modify the php.ini configuration.
Because PHP.ini does not load the $_ENV variable definition by default, if you look at Phpinfo () at this point, you will notice that we set the environment variable to "no value".
#vi/etc/php5/fpm/php.ini
Modified: Variables_order for:
Variables_order= "Egpcs"

5, restart PHP-FPM
#service php5-fpm Restart

6. Test the setup results.
View Phpinfo (), in the "Enviroment variables" section and in the "PHP variables" section you may see the variable name and the variable value we just set.

[Supplemental]:
1,php The web can normally get $_env variables, PHP-CLI mode does not take $_env. That
Php-r ' Print_r ($_env); '
Get results: Array ()
This is because the PHP CLI uses a different php.ini, and its variables_order does not open the ENV variable. It is used:/etc/php5/cli/php.ini, press [4] instructions to open.
If you encounter this problem, you can use the specified load configuration file (. ini) to test for php.ini. That
Php-c '/etc/php5/fpm/php.ini '-R ' Print_r ($_env) '

PHP Add $_env variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.