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

时间:2020-8-22 作者:admin

安装:

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>

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

  /**
     * 获取环境模式
     *
     * @return string
     */
    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;
    }

 

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。