Chinaunix首页 | 论坛 | 博客
  • 博客访问: 404282
  • 博文数量: 80
  • 博客积分: 885
  • 博客等级: 准尉
  • 技术积分: 917
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-02 11:43
个人简介

人生就是一次旅行,生活就是艺术,人人都是手艺人.

文章分类

全部博文(80)

文章存档

2023年(1)

2022年(5)

2019年(3)

2018年(1)

2017年(6)

2016年(4)

2014年(8)

2013年(20)

2012年(9)

2010年(3)

2009年(17)

2008年(3)

我的朋友

分类: PHP

2018-12-05 15:15:49

环境:Centos linux os 
很多php同学经常会问到如何在我们的开发环境中使用php中的$_ENV 环境变量功能,以下步骤操作即可实现
1、在我们的php-fpm服务启动脚本(/etc/init.d/php-fpm)中添加
source /etc/profile
文件段如下:

点击(此处)折叠或打开

  1. #! /bin/sh

  2. ### BEGIN INIT INFO
  3. # Provides: php-fpm
  4. # Required-Start: $remote_fs $network
  5. # Required-Stop: $remote_fs $network
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: starts php-fpm
  9. # Description: starts the PHP FastCGI Process Manager daemon
  10. ### END INIT INFO

  11. prefix=@prefix@
  12. exec_prefix=@exec_prefix@

  13. #php_fpm_BIN=@sbindir@/php-fpm
  14. #php_fpm_CONF=@sysconfdir@/php-fpm.conf
  15. #php_fpm_PID=@localstatedir@/run/php-fpm.pid

  16. php_fpm_BIN=/usr/local/php/sbin/php-fpm
  17. php_fpm_CONF=/usr/local/php/etc/php-fpm.conf
  18. php_fpm_PID=/usr/local/php/var/run/php-fpm.pid

  19. php_opts="--fpm-config $php_fpm_CONF"

  20. source /etc/profile

  21. wait_for_pid () {
  22.         try=0

  23.         while test $try -lt 35 ; do

  24.                 case "$1" in
  25.                         'created')
  26.                         if [ -f "$2" ] ; then
  27.                                 try=''
  28.                                 break
  29.                         fi
  30.                         ;;

  31.                         'removed')
  32.                         if [ ! -f "$2" ] ; then
  33.                                 try=''
  34.                                 break
  35.                         fi
  36.                         ;;
  37.                 esac

  38.                 echo -n .
  39.                 try=`expr $try + 1`
  40.                 sleep 1

  41.         done

  42. }

  43. case "$1" in
  44.         start)
  45.                 echo -n "Starting php-fpm "

  46.                 $php_fpm_BIN $php_opts

  47.                 if [ "$?" != 0 ] ; then
  48.                         echo " failed"
  49.                         exit 1
  50.                 fi

  51.                 wait_for_pid created $php_fpm_PID

  52.                 if [ -n "$try" ] ; then
  53.                         echo " failed"
  54.                         exit 1
  55.                 else
  56.                         echo " done"
  57.                 fi
  58.         ;;

  59.         stop)
  60.                 echo -n "Gracefully shutting down php-fpm "

  61.                 if [ ! -r $php_fpm_PID ] ; then
  62.                         echo "warning, no pid file found - php-fpm is not running ?"
  63.                         exit 1
  64.                 fi

  65.                 kill -QUIT `cat $php_fpm_PID`

  66.                 wait_for_pid removed $php_fpm_PID

  67.                 if [ -n "$try" ] ; then
  68.                         echo " failed. Use force-quit"
  69.                         exit 1
  70.                 else
  71.                         echo " done"
  72.                 fi
  73.         ;;

  74.         force-quit)
  75.                 echo -n "Terminating php-fpm "

  76.                 if [ ! -r $php_fpm_PID ] ; then
  77.                         echo "warning, no pid file found - php-fpm is not running ?"
  78.                         exit 1
  79.                 fi

  80.                 kill -TERM `cat $php_fpm_PID`

  81.                 wait_for_pid removed $php_fpm_PID

  82.                 if [ -n "$try" ] ; then
  83.                         echo " failed"
  84.                         exit 1
  85.                 else
  86.                         echo " done"
  87.                 fi
  88.         ;;

  89.         restart)
  90.                 $0 stop
  91.                 $0 start
  92.         ;;

  93.         reload)

  94.                 echo -n "Reload service php-fpm "

  95.                 if [ ! -r $php_fpm_PID ] ; then
  96.                         echo "warning, no pid file found - php-fpm is not running ?"
  97.                         exit 1
  98.                 fi

  99.                 kill -USR2 `cat $php_fpm_PID`

  100.                 echo " done"
  101.         ;;

  102.         *)
  103.                 echo "Usage: $0 {start|stop|force-quit|restart|reload}"
  104.                 exit 1
  105.         ;;

  106. esac
2、在/etc/profile.d/php.sh文件中添加需加的环境变量KeyValue如下:
export MY_REDIS_HOST="localhost"
每一行一个。
3、修改php配置在php.ini中的
variables_order = "EGPCS"
4、修改php-fpm.conf配置添加如下:
include=/usr/local/php/etc/php-fpm.d/*.conf
5、添加PHP环境变量测试
]#echo “env[MY_REDIS_HOST]=$MY_REDIS_HOST” > /usr/local/php/etc/php-fpm.d/php-fpm.env.conf
6、重启php-fpm服务
service php-fpm restart


以上操作已经完成添加MY_REDIS_HOST 的值为localhost

可以通过以下test.php进行测试

点击(此处)折叠或打开

  1. <?php
  2. echo getenv(MY_REDIS_HOST );
  3. ?>







阅读(1116) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~