php linux yaml 的安装和使用、环境区分(测试、预发布、生产)

   日期:2020-08-22     浏览:139    评论:0    
核心提示:安装:1 下载yaml包wget http://pyyaml.org/download/libyaml/yaml-0.2.2.tar.gztar -zxvf yaml-0.2.2.tar.gzcd yaml-0.2.2/./configuremakemake install2,下载php的yaml扩展包点击官网下载:https://pecl.php.net/package/yamltar -zvxf yaml-2.0.4.tgz cd yaml-2.0.4//usr/

安装:

1 下载yaml包

wget http://pyyaml.org/download/libyaml/yaml-0.2.2.tar.gz
tar -zxvf yaml-0.2.2.tar.gz
cd yaml-0.2.2/
./configure
make
make install

2,下载php的yaml扩展包

点击官网下载:https://pecl.php.net/package/yaml

tar -zvxf yaml-2.0.4.tgz 
cd yaml-2.0.4/
/usr/local/php7/bin/phpize  //这里要改成你PHP的安装目录
./configure --with-php-config=/usr/local/php7/bin/php-config  //这里也是一样
make
make install

php.ini 添加扩展yaml.so 

vi /usr/local/php7/lib/php.ini  //一样注意目录
extension=yaml.so


 4 重启PHP-FPM

//数组转yaml
$yaml = yaml_emit($invoice);

// yaml转数组
$parsed = yaml_parse($yaml);


//读取yaml文件地址

$configFile = dev.yaml

$defaultConfig = yaml_parse_file($configFile);

 5 设置环境变量,测试、预发布、灰度、生产

nginx配置

server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name domian.com;
    root        /opt/web;
    index       index.php;

    #access_log  /opt/log/access.log;
    #error_log   /opt/log/error.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
        
        # Project params
		fastcgi_param ENV_MOD DEVELOPMENT; # 开发环境
 		#fastcgi_param ENV_MOD TEST; # 测试环境
 		#fastcgi_param ENV_MOD PRODUCTION; # 生产环境
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }
}

Apache配置

<VirtualHost *:80>
   	ServerName domain.com
   	DocumentRoot "/opt/web/"

	<Directory "/opt/web/">
		# use mod_rewrite for pretty URL support
		RewriteEngine on
		# If a directory or a file exists, use the request directly
		RewriteCond %{REQUEST_FILENAME} !-f
		RewriteCond %{REQUEST_FILENAME} !-d
		# Otherwise forward the request to index.php
		RewriteRule . index.php

		# use index.php as index file
		DirectoryIndex index.php

		# ...other settings...
	</Directory>
	
	# 环境变量
	SetEnv ENV_MOD DEVELOPMENT #开发环境
	#SetEnv ENV_MOD TEST #测试环境
	#SetEnv ENV_MOD PRODUCTION #生产环境
	
</VirtualHost>

通过不同  环境变量,读取配置文件

  
    public function getMode()
    {
        // 从 webserver 配置获取环境模式信息
        $mode = getenv(self::ENV_NAME);

        // 尝试从config/mode.php读取环境配置
        if (empty($mode)) {
            $modeFile = $this->_configDir . '/mode.php';
            if (file_exists($modeFile)) {
                $mode = file_get_contents($modeFile);
            }
        } else {
            // 从配置文件获取环境模式信息
            //todo
        }

        $mode = trim(strtoupper($mode));
        return $mode;
    }

 

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服