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

▼魔方 西西 提交于 2020-09-30 12:28:28

安装:

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;
    }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!