<?xml version="1.0" encoding="UTF-8"?>
<response><items><item><id>1</id><site>bestyii</site><type>topic</type><post_meta_id>8</post_meta_id><user_id>3</user_id><title>Yii2 composer安装慢的解决办法</title><author></author><excerpt></excerpt><image></image><content>在yii中引用php的开源项目用composer已经很方便了，引用前端的开源项目也有composer的插件如：&#13;
&#13;
1. [fxp-asset](https://github.com/fxpio/composer-asset-plugin "fxp-asset")&#13;
2. [Asset Packagist](https://github.com/hiqdev/asset-packagist "Asset Packagist")&#13;
&#13;
以前yii默认采用前者，现在新的yii2模版默认采用后者，后者的作者就很厉害了，貌似是个重度yii用户，看来是被fxp-asset的执行缓慢给弄急眼了，所以自己搞了个更新的方法。&#13;
&#13;
**言归正传：**&#13;
所以更快速的安装方式就是 Asset Packagist https://asset-packagist.org&#13;
&#13;
其实就是2步：&#13;
1. 在config中关闭fxp-asset的调用&#13;
2. 在源列表中加入asset-packagist库的配置&#13;
&#13;
```&#13;
"config": {&#13;
        "process-timeout": 1800,&#13;
        "fxp-asset": {&#13;
            "enabled": false &#13;
        }&#13;
    },&#13;
...&#13;
 "repositories": [&#13;
    {&#13;
        "type": "composer",&#13;
        "url": "https://asset-packagist.org"&#13;
    }&#13;
]&#13;
```&#13;
&#13;
如果composer的源采用阿里云镜像，完整写法如下：&#13;
&#13;
```&#13;
"repositories": {&#13;
        "0": {&#13;
            "type": "composer",&#13;
            "url": "https://asset-packagist.org"&#13;
        },&#13;
        "packagist": {&#13;
            "type": "composer",&#13;
            "url": "https://mirrors.aliyun.com/composer/"&#13;
        }&#13;
    }&#13;
```&#13;
&#13;
需要注意的是，yii在yii\\base\\Application 中定义vendor路径的时候也定义了bower和npm路径：&#13;
&#13;
```php&#13;
&#13;
    /**&#13;
     * Sets the directory that stores vendor files.&#13;
     * @param string $path the directory that stores vendor files.&#13;
     */&#13;
    public function setVendorPath($path)&#13;
    {&#13;
        $this-&gt;_vendorPath = Yii::getAlias($path);&#13;
        Yii::setAlias('@vendor', $this-&gt;_vendorPath);&#13;
        Yii::setAlias('@bower', $this-&gt;_vendorPath . DIRECTORY_SEPARATOR . 'bower');&#13;
        Yii::setAlias('@npm', $this-&gt;_vendorPath . DIRECTORY_SEPARATOR . 'npm');&#13;
    }&#13;
```&#13;
&#13;
这就和asset-packagist的默认安装路径有了差别解决办法：&#13;
重新定义yii中的bower和npm路径&#13;
&#13;
```php&#13;
&#13;
    $config = [&#13;
        ...&#13;
        'aliases' =&gt; [&#13;
            '@bower' =&gt; '@vendor/bower-asset',&#13;
            '@npm'   =&gt; '@vendor/npm-asset',&#13;
        ],&#13;
        ...&#13;
    ];&#13;
```</content><tags>composer,yii2</tags><last_comment_time>1574129131</last_comment_time><last_comment_username></last_comment_username><view_count>5480</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574129131</created_at><updated_at>1574141225</updated_at></item><item><id>2</id><site>bestyii</site><type>topic</type><post_meta_id>8</post_meta_id><user_id>3</user_id><title>Yii2 的安全最佳实践（Best Practices Of Security）</title><author></author><excerpt></excerpt><image></image><content>下面，我们将会回顾常见的安全原则，并介绍在使用 Yii 开发应用程序时，如何避免潜在安全威胁。 大多数这些原则并非您独有，而是适用于网站或软件开发， 因此，您还可以找到有关这些背后的一般概念的进一步阅读的链接。&#13;
&#13;
基本准则&#13;
=======&#13;
无论是开发何种应用程序，我们都有两条基本的安全准则：&#13;
&#13;
- 过滤输入&#13;
- 转义输出&#13;
&#13;
过滤输入&#13;
-------&#13;
&#13;
过滤输入的意思是，用户输入不应该认为是安全的，你需要总是验证你获得的输入值是在允许范围内。 比如，我们假设可以通过三个字段完成排序` title，created_at 和 status`，然后，这个值是由用户输入提供的， 那么，最好在我们接收参数的时候，检查一下这个值是否是指定的范围。 对于基本的 PHP 而言，上述做法类似如下：&#13;
&#13;
```php&#13;
$sortBy = $_GET['sort'];&#13;
if (!in_array($sortBy, ['title', 'created_at', 'status'])) {&#13;
	throw new Exception('Invalid sort value.');&#13;
}&#13;
```&#13;
&#13;
在 Yii 中，很大可能性，你会使用 `表单校验器` 来执行类似的检查。&#13;
&#13;
**进一步阅读该主题：**&#13;
&#13;
https://www.owasp.org/index.php/Data_Validation&#13;
https://www.owasp.org/index.php/Input_Validation_Cheat_Sheet&#13;
&#13;
转义输出&#13;
--------&#13;
转义输出的意思是，根据我们使用数据的上下文环境，数据需要被转义。比如：在 HTML 上下文， 你需要转义` &lt;，&gt; `之类的特殊字符。在 JavaScript 或者 SQL 中，也有其他的特殊含义的字符串需要被转义。 由于手动的给所用的输出转义容易出错， Yii 提供了大量的工具来在不同的上下文执行转义。&#13;
&#13;
**进一步阅读该话题：**&#13;
&#13;
https://www.owasp.org/index.php/Command_Injection&#13;
https://www.owasp.org/index.php/Code_Injection&#13;
https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)&#13;
&#13;
避免 SQL 注入&#13;
=======&#13;
&#13;
SQL 注入发生在查询语句是由连接未转义的字符串生成的场景，比如：&#13;
&#13;
```php&#13;
$username = $_GET['username'];&#13;
$sql = "SELECT * FROM user WHERE username = '$username'";&#13;
```&#13;
除了提供正确的用户名外，攻击者可以给你的应用程序输入类似` '; DROP TABLE user; -- `的语句。 这将会导致生成如下的 SQL：&#13;
&#13;
```&#13;
SELECT * FROM user WHERE username = ''; DROP TABLE user; --'&#13;
```&#13;
&#13;
这是一个合法的查询语句，并将会执行以空的用户名搜索用户操作，然后，删除 user 表。 这极有可能导致网站出错，数据丢失。（你是否进行了规律的数据备份？）&#13;
&#13;
在 Yii 中，大部分的数据查询是通过 `Active Record` 进行的， 而其是完全使用 PDO 预处理语句执行 SQL 查询的。在预处理语句中，上述示例中，构造 SQL 查询的场景是不可能发生的。&#13;
&#13;
有时，你仍需要使用 `raw queries` 或者 `query builder`。 在这种情况下，你应该使用安全的方式传递参数。如果数据是提供给表列的值，最好使用预处理语句：&#13;
```php&#13;
// query builder&#13;
$userIDs = (new Query())&#13;
    -&gt;select('id')&#13;
    -&gt;from('user')&#13;
    -&gt;where('status=:status', [':status' =&gt; $status])&#13;
    -&gt;all();&#13;
&#13;
// DAO&#13;
$userIDs = $connection&#13;
    -&gt;createCommand('SELECT id FROM user where status=:status')&#13;
    -&gt;bindValues([':status' =&gt; $status])&#13;
    -&gt;queryColumn();&#13;
```&#13;
&#13;
如果数据是用于指定列的名字，或者表的名字，最好的方式是只允许预定义的枚举值。&#13;
&#13;
```php&#13;
function actionList($orderBy = null)&#13;
{&#13;
    if (!in_array($orderBy, ['name', 'status'])) {&#13;
        throw new BadRequestHttpException('Only name and status are allowed to order by.')&#13;
    }&#13;
&#13;
    // ...&#13;
}&#13;
```&#13;
如果上述方法不行，表名或者列名应该被转义。Yii 针对这种转义提供了一个特殊的语法， 这样可以在所有支持的数据库都使用一套方案。&#13;
&#13;
```php&#13;
$sql = "SELECT COUNT([[$column]]) FROM {{table}}";&#13;
$rowCount = $connection-&gt;createCommand($sql)-&gt;queryScalar();&#13;
```&#13;
你可以在 Quoting Table and Column Names 中获取更多的语法细节。&#13;
&#13;
**进一步阅读该话题：**&#13;
&#13;
https://www.owasp.org/index.php/SQL_Injection&#13;
防止 XSS 攻击&#13;
============&#13;
XSS 或者跨站脚本发生在输出 HTML 到浏览器时，输出内容没有正确的转义。 例如，如果用户可以输入其名称，那么他输入 `&lt;script&gt;alert('Hello!');&lt;/script&gt;` 而非其名字 `Alexander`， 所有输出没有转义直接输出用户名的页面都会执行 JavaScript 代码 `alert('Hello!');`， 这会导致浏览器页面上出现一个警告弹出框。就具体的站点而言，除了这种无意义的警告输出外， 这样的脚本可以以你的名义发送一些消息到后台，甚至执行一些银行交易行为。&#13;
&#13;
避免 XSS 攻击在 Yii 中非常简单，有如下两种一般情况：&#13;
&#13;
- 你希望数据以纯文本输出。&#13;
- 你希望数据以 HTML 形式输出。&#13;
&#13;
如果你需要的是纯文本，你可以如下简单的转义：&#13;
&#13;
```php&#13;
&lt;?= \yii\helpers\Html::encode($username) ?&gt;&#13;
```&#13;
如果是 HTML，我们可以用 `HtmlPurifier` 帮助类来执行：&#13;
```php&#13;
&lt;?= \yii\helpers\HtmlPurifier::process($description) ?&gt;&#13;
```&#13;
&#13;
注意 HtmlPurifier 帮助类的处理过程较为费时，建议增加缓存。&#13;
&#13;
**进一步阅读该话题：**&#13;
&#13;
https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)&#13;
&#13;
防止 CSRF 攻击&#13;
============&#13;
CSRF 是跨站请求伪造的缩写。这个攻击思想源自许多应用程序假设来自用户的浏览器请求是由用户自己产生的， 而事实并非如此。&#13;
&#13;
例如，网站 an.example.com 有一个 /logout 网址，当使用简单的 GET 请求访问时, 记录用户退出。 只要用户的请求一切正常，但是有一天坏人们故意在用户经常访问的论坛上放上 `&lt;img src="http://an.example.com/logout"&gt;`。 浏览器在请求图像或请求页面之间没有任何区别， 所以当用户打开一个带有这样一个被操作过的 &lt;img&gt; 标签的页面时， 浏览器将 GET 请求发送到该 URL，用户将从 an.example.com 注销。&#13;
&#13;
这是 CSRF 攻击如何运作的基本思路。可以说用户退出并不是一件严重的事情， 然而这仅仅是一个例子，使用这种方法可以做更多的事情，例如触发付款或者是改变数据。 想象一下如果某个网站有一个这样的 `http://an.example.com/purse/transfer?to=anotherUser&amp;amount=2000` 网址。 使用 GET 请求访问它会导致从授权用户账户转账 $2000 给 anotherUser。 我们知道，浏览器将始终发送 GET 请求来加载图像， 所以我们可以修改代码以仅接受该 URL 上的 POST 请求。 不幸的是，这并不会拯救我们，因为攻击者可以放置一些 JavaScript 代码而不是 &lt;img&gt; 标签，这样就可以向该 URL 发送 POST 请求。&#13;
&#13;
出于这个原因，Yii 应用其他机制来防止 CSRF 攻击。&#13;
&#13;
**为了避免 CSRF 攻击，你总是需要：**&#13;
&#13;
1. 遵循 HTTP 准则，比如 GET 不应该改变应用的状态。 有关详细信息，请参阅 [RFC2616][1]。&#13;
2. 保证 Yii CSRF 保护开启。&#13;
有的时候你需要对每个控制器和/或方法使用禁用 CSRF。可以通过设置其属性来实现：&#13;
&#13;
```php&#13;
namespace app\controllers;&#13;
&#13;
use yii\web\Controller;&#13;
&#13;
class SiteController extends Controller&#13;
{&#13;
    public $enableCsrfValidation = false;&#13;
&#13;
    public function actionIndex()&#13;
    {&#13;
        // CSRF validation will not be applied to this and other actions&#13;
    }&#13;
&#13;
}&#13;
```&#13;
要对每个自定义方法禁用 CSRF 验证，您可以使用：&#13;
&#13;
```php&#13;
namespace app\controllers;&#13;
&#13;
use yii\web\Controller;&#13;
&#13;
class SiteController extends Controller&#13;
{&#13;
    public function beforeAction($action)&#13;
    {&#13;
        // ...set `$this-&gt;enableCsrfValidation` here based on some conditions...&#13;
        // call parent method that will check CSRF if such property is true.&#13;
        return parent::beforeAction($action);&#13;
    }&#13;
}&#13;
```&#13;
&#13;
在 standalone actions 禁用 CSRF 必须在 `init()` 方法中设置。 不要把这段代码放在 beforeRun() 方法中，因为它不会起任何作用。&#13;
```php&#13;
&lt;?php&#13;
&#13;
namespace app\components;&#13;
&#13;
use yii\base\Action;&#13;
&#13;
class ContactAction extends Action&#13;
{&#13;
    public function init()&#13;
    {&#13;
        parent::init();&#13;
        $this-&gt;controller-&gt;enableCsrfValidation = false;&#13;
    }&#13;
&#13;
    public function run()&#13;
    {&#13;
          $model = new ContactForm();&#13;
          $request = Yii::$app-&gt;request;&#13;
          if ($request-&gt;referrer === 'yiipowered.com'&#13;
              &amp;&amp; $model-&gt;load($request-&gt;post())&#13;
              &amp;&amp; $model-&gt;validate()&#13;
          ) {&#13;
              $model-&gt;sendEmail();&#13;
          }&#13;
    }&#13;
}&#13;
```&#13;
&#13;
&gt; `警告：` 禁用 CSRF 将允许任何站点向您的站点发送 POST 请求。在这种情况下，实施额外验证非常重要，例如检查 IP 地址或秘密令牌。&#13;
&#13;
**进一步阅读该话题：**&#13;
&#13;
https://www.owasp.org/index.php/CSRF&#13;
&#13;
防止文件暴露&#13;
============&#13;
默认的服务器 webroot 目录指向包含有 `index.php` 的 `web` 目录。在共享托管环境下，这样是不可能的， 这样导致了所有的代码，配置，日志都在webroot目录。&#13;
&#13;
如果是这样，别忘了拒绝除了 web 目录以外的目录的访问权限。 如果没法这样做，考虑将你的应用程序托管在其他地方。&#13;
&#13;
在生产环境关闭调试信息和工具&#13;
在调试模式下， Yii 展示了大量的错误信息，这样是对开发有用的。 同样，这些调试信息对于攻击者而言也是方便其用于破解数据结构，配置值，以及你的部分代码。 永远不要在生产模式下将你的 `index.php` 中的 `YII_DEBUG` 设置为 `true`。&#13;
&#13;
你同样也不应该在生产模式下开启 Gii。它可以被用于获取数据结构信息， 代码，以及简单的用 Gii 生成的代码覆盖你的代码。&#13;
&#13;
调试工具栏同样也应该避免在生产环境出现，除非非常有必要。它将会暴露所有的应用和配置的详情信息。 如果你确定需要，反复确认其访问权限限定在你自己的 IP。&#13;
&#13;
**进一步阅读该话题：**&#13;
&#13;
https://www.owasp.org/index.php/Exception_Handling&#13;
https://www.owasp.org/index.php/Top_10_2007-Information_Leakage&#13;
&#13;
使用 TLS 上的安全连接（HTTPS的启用）&#13;
===================&#13;
Yii 提供依赖 cookie 和/或 PHP 会话的功能。如果您的连接受到威胁，这些可能会很容易受到攻击。 如果应用程序通过 TLS 使用安全连接，则风险会降低。&#13;
有关如何配置它的说明，请参阅您的 Web 服务器文档。 &#13;
&#13;
&#13;
现在各大云平台都提供免费的单域名SSL证书，如阿里云，[腾讯云][2]。&#13;
&#13;
以阿里云部署Nginx下的SSL证书为例：&#13;
修改nginx.conf文件如下：&#13;
```&#13;
# 以下属性中以ssl开头的属性代表与证书配置有关，其他属性请根据自己的需要进行配置。&#13;
server {&#13;
    listen 443 ssl;&#13;
    server_name localhost;  # localhost修改为您证书绑定的域名。&#13;
    &#13;
    root html;&#13;
    index index.html index.htm;&#13;
    ssl_certificate cert/domain name.pem;   #将domain name.pem替换成您证书的文件名。&#13;
    ssl_certificate_key cert/domain name.key;   #将domain name.key替换成您证书的密钥文件名。&#13;
    ssl_session_timeout 5m;&#13;
    #使用此加密套件。&#13;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; &#13;
    #使用该协议进行配置。 &#13;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   &#13;
    ssl_prefer_server_ciphers on;   &#13;
    location / {&#13;
        root html;   #站点目录。&#13;
        index index.html index.htm;   &#13;
    }&#13;
}&#13;
	&#13;
```&#13;
设置http请求自动跳转https。（一般都这么做）&#13;
```&#13;
server {&#13;
  listen 80;&#13;
  server_name www.example.com;&#13;
  return 301 https://$server_name$request_uri;&#13;
}&#13;
```&#13;
&#13;
&gt; 参考官方说明，写的能不能配置成功自己脑补&#13;
&gt; 腾讯云 [Nginx 服务器证书安装][3]&#13;
&gt; 阿里云 [在Nginx/Tengine服务器上安装证书][4]&#13;
&#13;
&#13;
安全服务器配置&#13;
============&#13;
本节的目的是强调在为基于 Yii 的网站提供服务配置时需要考虑的风险。 除了这里涉及的要点之外， 可能还有其他与安全相关的配置选项， 所以不要认为这部分是完整的。&#13;
&#13;
避免 Host-header 攻击&#13;
--------------------&#13;
像 yii\web\UrlManager 和 yii\helpers\Url 这样的类会使用 currently requested host name 来生成链接。 如果 Web 服务器配置为独立于 `Host` 标头的值提供相同的站点，这个信息并不可靠， 并且 [可能由发送HTTP请求的用户伪造][5]。 在这种情况下，您应该修复您的 Web 服务器配置以便仅为指定的主机名提供站点服务 或者通过设置 `request` 应用程序组件的 hostInfo 属性来显式设置或过滤该值。&#13;
&#13;
&gt; `注意：` 您应该始更倾向于使用 web 服务器配置 'host header attack' 保护而不是使用过滤器。 仅当服务器配置设置不可用时 yii\filters\HostControl 才应该被使用。&#13;
&#13;
**以Nginx为例：**&#13;
&#13;
方法一：&#13;
&#13;
修改nginx.conf&#13;
&#13;
添加一个默认server，当host头被修改匹配不到server时会跳到该默认server，该默认server直接返回403错误。&#13;
&#13;
例子如下：&#13;
```config&#13;
server {&#13;
&#13;
       listen 80 default;&#13;
&#13;
       server_name _;&#13;
       server_name_in_redirect off;&#13;
       location / {&#13;
&#13;
            return 403;&#13;
&#13;
       }&#13;
&#13;
       }&#13;
```&#13;
重启nginx即可。&#13;
&#13;
方法二：&#13;
&#13;
修改nginx.conf&#13;
&#13;
在目标server添加检测规则，参考以下标红配置：&#13;
&#13;
   &#13;
```config&#13;
server {&#13;
&#13;
       server_name  abc.com;&#13;
&#13;
       listen       80;&#13;
&#13;
        if ($http_Host !~*^abc.com:80$)&#13;
&#13;
        {&#13;
            return 403;&#13;
        }&#13;
&#13;
       ...&#13;
&#13;
       }&#13;
```&#13;
重启nginx即可。&#13;
&#13;
有关于服务器配置的更多信息，请参阅您的 web 服务器的文档：&#13;
- [在Web服务器防止Host头攻击][6]&#13;
- Apache 2：http://httpd.apache.org/docs/trunk/vhosts/examples.html#defaultallports&#13;
- Nginx：https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/&#13;
&#13;
**`如果您无权访问服务器配置，您可以在应用程序级别设置 yii\filters\HostControl 过滤器， 以防此类的攻击。`**&#13;
&#13;
```php&#13;
// Web Application configuration file&#13;
return [&#13;
    'as hostControl' =&gt; [&#13;
        'class' =&gt; 'yii\filters\HostControl',&#13;
        'allowedHosts' =&gt; [&#13;
            'example.com',&#13;
            '*.example.com',&#13;
        ],&#13;
        'fallbackHostInfo' =&gt; 'https://example.com',&#13;
    ],&#13;
    // ...&#13;
];&#13;
```&#13;
&#13;
&#13;
&#13;
补充阅读&#13;
=======&#13;
- [web安全攻防思维导图][7]&#13;
![web安全攻防思维导图][8]&#13;
&#13;
- web攻击及防御技术 （来源网络）&#13;
![web攻击及防御技术][9]&#13;
&#13;
&#13;
  [1]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html&#13;
  [2]: https://cloud.tencent.com/document/product/400/8422&#13;
  [3]: https://cloud.tencent.com/document/product/400/35244&#13;
  [4]: https://help.aliyun.com/document_detail/98728.html&#13;
  [5]: https://www.acunetix.com/vulnerabilities/web/host-header-attack&#13;
  [6]: https://www.freebuf.com/articles/web/178315.html&#13;
  [7]: https://blog.csdn.net/qq_42636435/article/details/89222372&#13;
  [8]: /img/bVbwZh6&#13;
  [9]: /img/bVbwZlh
&gt; 本文由 [monster](https://www.bestyii.com/member/monster) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>yii2</tags><last_comment_time>1574142784</last_comment_time><last_comment_username></last_comment_username><view_count>3682</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574142784</created_at><updated_at>1574142784</updated_at></item><item><id>3</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>3</user_id><title>CentOS7 supervisord进程管理服务</title><author></author><excerpt></excerpt><image></image><content>简介&#13;
===&#13;
官网 http://supervisord.org/&#13;
supervisor是一个允许用户监视和控制在linux操作系统的进程数量的客户端/服务器系统。由python语言编写，用以监控服务器的运行，发现问题能立即自动预警及自动重启等功能。supervisor还要求管理的程序是非daemon程序，supervisord会帮你把它转成daemon程序。&#13;
组件&#13;
===&#13;
&#13;
- supervisord&#13;
服务端，他负责在他自己的进程下起一个子进程，相应来自客户端的命令，重启崩溃或异常退出的子进程，输出相关日志，针对于子进程活跃期间的时间进行生成和管理&#13;
配置文件位置： /etc/supervisord.conf 注意配置合理权限&#13;
- supervisorctl&#13;
命令行客户端，有supervisord提供的一个shell-like接口，通过它，用户可以连接到不同的supervisorctl进程，查看，起停子进程，列出正在运行的子进程。通过TCP进行交互，提供认证，在[supervisorctl]段进行统一的配置&#13;
- web server&#13;
supervisorctl的web管理界面，通过访问http://localhost:9001/来管理子进程状态，[inet_http_server]这段进行配置&#13;
&#13;
安装&#13;
===&#13;
前提条件是要有python环境，linux一般自带python环境，这里以centOS为例。&#13;
&#13;
yum安装&#13;
```&#13;
yum install -y supervisor&#13;
```&#13;
启动&#13;
===&#13;
```&#13;
systemctl start supervisord&#13;
```&#13;
开机启动&#13;
```&#13;
systemctl enable supervisord&#13;
```&#13;
配置文件解析&#13;
===&#13;
&#13;
生成配置文件：&#13;
echo_supervisord_conf &gt; /tmp/supervisord.conf&#13;
一般yum安装后配置文件默认位置是/etc/supervisor/supervisord.conf。其中注释是以分号开头&#13;
```&#13;
[unix_http_server]   #这段是通过socket文件启动的web server，这个要有，因为命令行supervisorctl是通过这个实现的。&#13;
file = /tmp/supervisor.sock&#13;
chmod = 0777&#13;
chown= nobody:nogroup&#13;
username = user&#13;
password = 123&#13;
[inet_http_server] #通过网络端口启动的web server&#13;
port = 127.0.0.1:9001&#13;
username = user&#13;
password = 123&#13;
[supervisord]   #这块是服务配置&#13;
logfile = /tmp/supervisord.log   日志文件&#13;
logfile_maxbytes = 50MB   日志文件最大size&#13;
logfile_backups=10  日志轮询下备份数&#13;
loglevel = info 日志级别&#13;
pidfile = /tmp/supervisord.pid  pid文件位置&#13;
nodaemon = false  如果是true，supervisor将在前端启动&#13;
minfds = 1024   supervisord启动成功的最小文件描述符数&#13;
minprocs = 200 supervisord启动成功的最小进程描述符数&#13;
umask = 022&#13;
user = chrism   启动用户，这块要注意，这个用户要有相应的目录权限&#13;
identifier = supervisor supervisor进程的 identifier字符串，用户RPC协议接口&#13;
directory = /tmp  当supervisord服务daemonizes时，切换到这个目录，可用这个%(here)s变量来扩展到整个配置文件&#13;
nocleanup = true  禁止supervisord在启动时间清空任何存在的AUTO子日志文件&#13;
childlogdir = /tmp AUTO自日志文件目录&#13;
strip_ansi = false 除去在子日志文件中所有的 ANSI转义序列&#13;
environment = KEY1="value1",KEY2="value2" 一个键/值的列表，一个环境变量吧？&#13;
[supervisorctl]&#13;
serverurl = unix:///tmp/supervisor.sock&#13;
username = chris&#13;
password = 123&#13;
prompt = mysupervisor String used as supervisorctl prompt.作为supervisorctl提示字符串。&#13;
```&#13;
管理进程配置&#13;
---&#13;
配置文件位置：/etc/supervisor/conf.d/&#13;
一般有如下配置项：&#13;
&#13;
```&#13;
process_name=%(program_name)s ＃进程名称，默认是程序名称&#13;
command 启动程序命令&#13;
numprocs=1 ＃进程数量&#13;
directory=/tmp ＃路径&#13;
umask=022 ＃掩码&#13;
priority=999 ＃优先级，越大被开起的越早&#13;
autorestart=true ＃自动重启&#13;
startsecs=10 ＃启动等待时间（秒）&#13;
startretries=3 ＃启动重试次数&#13;
stopsignal=TERM ＃关闭信号&#13;
stopwaitsecs=10 ＃关闭前等待时间&#13;
user=chrism ＃监控用户权限&#13;
redirect_stderr=false ＃重定向报错输出&#13;
stdout_logfile=/a/path ＃输入重定向为日志&#13;
stdout_logfile_maxbytes=1MB ＃日志大小&#13;
stdout_logfile_backups=10 ＃日志备份&#13;
stdout_capture_maxbytes=1MB&#13;
stderr_logfile=/a/path&#13;
stderr_logfile_maxbytes=1MB&#13;
stderr_logfile_backups=10&#13;
stderr_capture_maxbytes=1MB&#13;
environment=A=1,B=2 ＃预定义环境变量&#13;
serverurl=AUTO ＃系统URL&#13;
&#13;
```&#13;
&#13;
示例&#13;
---&#13;
为了更好的展示，我这边写了一个简单的服务。下面的具体的代码：&#13;
```&#13;
#!/usr/bin/env python&#13;
import socket&#13;
&#13;
HOST, PORT = '', 8080&#13;
&#13;
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)&#13;
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)&#13;
listen_socket.bind((HOST, PORT))&#13;
listen_socket.listen(1)&#13;
while True:&#13;
    client_connection, client_address = listen_socket.accept()&#13;
    request = client_connection.recv(1024)&#13;
    http_response = """\&#13;
HTTP/1.1 200 OK&#13;
   Hello World!&#13;
"""&#13;
    client_connection.sendall(http_response)&#13;
    client_connection.close()&#13;
```&#13;
这个脚本是一个简单的web服务器，8080端口，所有访问都会返回 Hello World!&#13;
下面我们看下配置文件/etc/supervisor/conf.d/webserver.conf&#13;
```&#13;
[program:webserver]&#13;
autostart=true&#13;
startretries=3&#13;
command=/opt/webserver.py&#13;
```&#13;
查看状态&#13;
```sh&#13;
root@0c1fc23d1398:~# supervisorctl status&#13;
webserver                        RUNNING   pid 1120, uptime 0:08:07&#13;
```&#13;
supervisorctl命令&#13;
&#13;
1.交互模式&#13;
&#13;
直接输入supervisorctl就进入交互模式。&#13;
```&#13;
root@0c1fc23d1398:~# supervisorctl &#13;
webserver                        RUNNING   pid 1120, uptime 0:09:40&#13;
supervisor&gt; &#13;
```&#13;
&#13;
2.命令行模式&#13;
&#13;
如上节所说的`supervisorctl status`&#13;
&#13;
3.具体命令项&#13;
&#13;
交互和命令行模式有一样。&#13;
```&#13;
reread 重新加载配置文件&#13;
update 将配置文件里新增的子进程加入进程组，如果设置了autostart=true则会启动新新增的子进程&#13;
status 查看所有进程状态&#13;
status 查看指定进程状态&#13;
start all 启动所有子进程&#13;
start  启动指定子进程&#13;
restart all 重启所有子进程&#13;
restart 重启指定子进程&#13;
stop all 停止所有子进程&#13;
stop  停止指定子进程&#13;
reload 重启supervisord&#13;
add  添加子进程到进程组&#13;
reomve  从进程组移除子进程，需要先stop。注意：移除后，需要使用reread和update才能重新运行该进程&#13;
```&#13;
web server界面&#13;
====&#13;
还记得“配置文件解析”一节关于web server段的配置吗？不记得可以回去看一眼。我们稍微展示下web界面。浏览器输入http://&lt;ip&gt;:&lt;port&gt;，会弹出输入用户名和密码的弹窗，输入信息后可以看到界面了。&#13;
&#13;
![图片描述][1]&#13;
&#13;
web界面&#13;
&#13;
从界面中可以看到，服务的状态，进程id和运行的时间。还有一些简单的操作，例如重启，停止，清除日志，监控日志等。&#13;
常见的错误&#13;
&#13;
1.多进程启动&#13;
&#13;
报错信息如下：&#13;
&#13;
```&#13;
Starting supervisor: Error: %(process_num) must be present within process_name when numprocs &gt; 1 in section 'program:nginx' (file: '/etc/supervisor/conf.d/nginx.conf')&#13;
For help, use /usr/bin/supervisord -h&#13;
```&#13;
这是配置文件格式不对，下面是格式简介&#13;
当numprocs=1时,process_name=%(program_name)s&#13;
当numprocs&gt;=2时,%(program_name)s_%(process_num)&#13;
类似下面的配置&#13;
&#13;
```&#13;
[program:sleeptime]&#13;
autostart=true&#13;
startretries=3&#13;
command=/opt/sleeptime.py&#13;
#主要是下面的2行&#13;
process_name=%(program_name)s%(process_num)s&#13;
numprocs=4&#13;
```&#13;
&#13;
这个时候查看进程状态，会发现有4个进程出现&#13;
```&#13;
root@0c1fc23d1398:/etc/supervisor/conf.d# supervisorctl status&#13;
sleeptime:sleeptime0             RUNNING   pid 1139, uptime 0:00:04&#13;
sleeptime:sleeptime1             RUNNING   pid 1140, uptime 0:00:04&#13;
sleeptime:sleeptime2             RUNNING   pid 1141, uptime 0:00:04&#13;
sleeptime:sleeptime3             RUNNING   pid 1142, uptime 0:00:03&#13;
webserver                        RUNNING   pid 1120, uptime 0:30:00&#13;
```&#13;
2.直接运行 supervisorctl status 报：&#13;
&#13;
```&#13;
Error: Server requires authentication&#13;
For help, use /usr/local/bin/supervisorctl -h&#13;
```&#13;
因为你设置访问账号密码，所以只能先supervisorctl进去，在status。&#13;
主要还是看日志信息，和程序本身的日志。&#13;
&#13;
&#13;
  [1]: /img/bVbwDes&#13;
&gt; 本文由 [systemofdown](https://www.bestyii.com/member/systemofdown) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。&#13;
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>centos7</tags><last_comment_time>1574212766</last_comment_time><last_comment_username></last_comment_username><view_count>4281</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574212766</created_at><updated_at>1599391579</updated_at></item><item><id>4</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>3</user_id><title>CentOS7 下 Docker 升级到最新版本</title><author></author><excerpt></excerpt><image></image><content>## 1、查看系统要求&#13;
&#13;
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看CentOS的内核版本。&#13;
```&#13;
uname -a&#13;
```&#13;
## 2、删除旧版本&#13;
```&#13;
yum remove docker  docker-common docker-selinux docker-engine&#13;
```&#13;
## 3、安装需要的软件包&#13;
yum-util 提供yum-config-manager功能，另外两个是devicemapper驱动依赖的&#13;
```&#13;
sudo yum install -y yum-utils device-mapper-persistent-data lvm2&#13;
```&#13;
## 4、设置Docker yum源&#13;
```&#13;
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo&#13;
```&#13;
&#13;
官方源有可能会很慢，不愿意等的把软件仓库地址替换腾讯云的镜像为:&#13;
```&#13;
sudo sed -i 's+download.docker.com+mirrors.cloud.tencent.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo&#13;
```&#13;
别忘了，刷新yum缓存&#13;
```&#13;
sudo yum makecache fast&#13;
```&#13;
&#13;
## 5、查看所有仓库中所有docker版本&#13;
可以查看所有仓库中所有docker版本,并选择特定的版本安装。&#13;
```&#13;
yum list docker-ce --showduplicates | sort -r&#13;
```&#13;
## 6、安装docker&#13;
```&#13;
sudo yum install docker-ce&#13;
```&#13;
由于repo中默认只开启stable仓库，故这里安装的是最新稳docker-ce-cli-19.03.1-3.el7.x86_64。&#13;
如果要安装特定版本：&#13;
```&#13;
sudo yum install docker-ce-18.06.1.ce  &#13;
```&#13;
## 7、启动&#13;
设置为开机启动&#13;
```&#13;
systemctl enable docker&#13;
```&#13;
启动&#13;
```&#13;
systemctl start docker&#13;
```&#13;
查看启动状态&#13;
```&#13;
systemctl status docker&#13;
```&#13;
查看版本&#13;
```&#13;
docker version&#13;
```</content><tags>docker,centos7</tags><last_comment_time>1574213625</last_comment_time><last_comment_username></last_comment_username><view_count>8001</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574213625</created_at><updated_at>1583307612</updated_at></item><item><id>5</id><site>bestyii</site><type>topic</type><post_meta_id>11</post_meta_id><user_id>3</user_id><title>关于PHP文件包含目录配置 open_basedir</title><author></author><excerpt></excerpt><image></image><content>open_basedir 官方介绍&#13;
=================&#13;
open_basedir string&#13;
&#13;
将 PHP 所能打开的文件限制在指定的目录树，包括文件本身。本指令不受安全模式打开或者关闭的影响。&#13;
当一个脚本试图用例如 fopen() 或者 gzopen() 打开一个文件时，该文件的位置将被检查。&#13;
当文件在指定的目录树之外时 PHP 将拒绝打开它。&#13;
所有的符号连接都会被解析，所以不可能通过符号连接来避开此限制。&#13;
特殊值 . 指明脚本的工作目录将被作为基准目录。但这有些危险，因为脚本的工作目录可以轻易被 chdir() 而改变。&#13;
&#13;
在 httpd.conf 文件中中，open_basedir 可以像其它任何配置选项一样用“php_admin_value open_basedir none”的方法关闭（例如某些虚拟主机中）。&#13;
&#13;
在 Windows 中，用分号分隔目录。在任何其它系统中用冒号分隔目录。作为 Apache 模块时，父目录中的 open_basedir 路径自动被继承。&#13;
&#13;
用 open_basedir 指定的限制实际上是前缀，不是目录名。&#13;
也就是说“open_basedir = /dir/incl”也会允许访问“/dir/include”和“/dir/incls”，如果它们存在的话。&#13;
如果要将访问限制在仅为指定的目录，用斜线结束路径名。例如：“open_basedir = /dir/incl/”。&#13;
&#13;
&gt; Note: 支持多个目录是 3.0.7 加入的。&#13;
默认是允许打开所有文件。&#13;
&#13;
设置限制包含目录&#13;
=============&#13;
接下来总结下， 可以有几种方式设置限制包含目录&#13;
1. php.ini  open_basedir = /home/wwwroot/&#13;
2. ini_set   注意：PHP &gt;5.2.3+ PHP_INI_ALL ，不建议使用，这么设置太随意了。&#13;
3. apache 的 httpd.conf 中Directory配置&#13;
```&#13;
"php_admin_value open_basedir none" #关闭&#13;
php_admin_value open_basedir "/home/wwwroot/:/tmp/:/var/tmp/:/proc/"&#13;
```&#13;
httpd.conf中VirtualHost&#13;
```&#13;
php_admin_value open_basedir "/home/wwwroot/:/tmp/:/var/tmp/:/proc/"&#13;
```&#13;
4. nginx fastcgi.conf&#13;
```&#13;
fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/";&#13;
```&#13;
5. .user.ini 文件&#13;
设置方法同 1 .&#13;
&#13;
&#13;
</content><tags>php,安全</tags><last_comment_time>1574213930</last_comment_time><last_comment_username></last_comment_username><view_count>3242</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574213930</created_at><updated_at>1574214051</updated_at></item><item><id>6</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>3</user_id><title>CentOS7下 yum 方式安装MySQL 5.7</title><author></author><excerpt></excerpt><image></image><content>&gt; 在CentOS中默认安装有MariaDB，这个是MySQL的分支，但为了需要，还是要在系统中安装MySQL，而且安装完成之后可以直接覆盖掉MariaDB。&#13;
&#13;
## 准备工作，卸载MariaDB&#13;
&#13;
```&#13;
yum remove mariadb&#13;
```&#13;
## 安装Mysql 5.7&#13;
### 1. 下载并安装MySQL官方的 Yum Repository&#13;
&#13;
&#13;
使用下面的命令就直接下载了安装用的Yum Repository&#13;
```&#13;
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm&#13;
```&#13;
最新版的参考mysql官方下载地址：[https://dev.mysql.com/downloads/repo/yum/](https://dev.mysql.com/downloads/repo/yum/ )&#13;
&#13;
### 2. 安装 Yum Repository&#13;
&#13;
```&#13;
yum -y install mysql57-community-release-el7-10.noarch.rpm&#13;
```&#13;
&#13;
### 3. 安装MySQL服务&#13;
这步可能会花些时间，安装完成后就会覆盖掉之前的mariadb。&#13;
```&#13;
yum -y install mysql-community-server&#13;
```&#13;
&#13;
### 4. 启动MySQL&#13;
启动mysql：&#13;
```&#13;
systemctl start  mysqld.service&#13;
```&#13;
设置开机启动：&#13;
```&#13;
systemctl enable  mysqld.service&#13;
```&#13;
查看MySQL运行状态：&#13;
&#13;
```&#13;
systemctl status mysqld.service&#13;
```&#13;
&#13;
## 初始化mysql&#13;
超级用户 `'root'@'localhost`被创建的时候，同时也设置了默认密码，这个密码明文方式存储在`error log`文件中. 是用下面命令可以快速的找到他.&#13;
```&#13;
shell&gt; sudo grep 'temporary password' /var/log/mysqld.log&#13;
```&#13;
需要修改密码之后才可以使用。登录mysql修改密码：&#13;
```&#13;
shell&gt; mysql -uroot -p&#13;
```&#13;
```&#13;
mysql&gt; ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';&#13;
```</content><tags>yum,centos7</tags><last_comment_time>1687678543</last_comment_time><last_comment_username>at1456328</last_comment_username><view_count>5249</view_count><follow_count>0</follow_count><comment_count>1</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574214391</created_at><updated_at>1687678543</updated_at></item><item><id>7</id><site>bestyii</site><type>topic</type><post_meta_id>8</post_meta_id><user_id>3</user_id><title>Yii2 queue的队列使用</title><author></author><excerpt></excerpt><image></image><content>少废话主要看文档&#13;
[官方文档 https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/README.md](https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/README.md)&#13;
&#13;
&#13;
yii2-queue 的使用&#13;
==============&#13;
&#13;
1.安装&#13;
-----&#13;
```&#13;
composer require --prefer-dist yiisoft/yii2-queue&#13;
```&#13;
&#13;
2.配置，在 common/config/main.php 中配置&#13;
----------------------------------&#13;
&#13;
redis作为驱动&#13;
&#13;
```php&#13;
    return [&#13;
        'bootstrap' =&gt; [&#13;
            'queue', // 把这个组件注册到控制台&#13;
        ],&#13;
        'components' =&gt; [&#13;
            'redis' =&gt; [&#13;
                'class' =&gt; \yii\redis\Connection::class,&#13;
                // ...&#13;
            ],&#13;
            'queue' =&gt; [&#13;
                'class' =&gt; \yii\queue\redis\Queue::class,&#13;
                'as log' =&gt; \yii\queue\LogBehavior::class,//错误日志 默认为 console/runtime/logs/app.log&#13;
                'redis' =&gt; 'redis', // 连接组件或它的配置&#13;
                'channel' =&gt; 'queue', // Queue channel key&#13;
            ],&#13;
        ],&#13;
    ];&#13;
```&#13;
&#13;
&#13;
File 作为驱动&#13;
&#13;
```php&#13;
    return [&#13;
        'bootstrap' =&gt; [&#13;
            'queue', // 把这个组件注册到控制台&#13;
        ],&#13;
        'components' =&gt; [&#13;
            'queue' =&gt; [&#13;
                'class' =&gt; \yii\queue\file\Queue::class,&#13;
                'as log' =&gt; \yii\queue\LogBehavior::class,//错误日志 默认为 console/runtime/logs/app.log&#13;
                'path' =&gt; '@runtime/queue',&#13;
            ],&#13;
        ],&#13;
    ];&#13;
```&#13;
3.新建 frontend/components/DownloadJob&#13;
-------------------------------------&#13;
```php&#13;
    class DownloadJob extends BaseObject implements \yii\queue\JobInterface&#13;
    {&#13;
        public $url;&#13;
        public $file;&#13;
&#13;
        public function execute($queue)&#13;
        {&#13;
            file_put_contents($this-&gt;file, file_get_contents($this-&gt;url));&#13;
        }&#13;
    }&#13;
```&#13;
4.控制台&#13;
------&#13;
&#13;
控制台用于监听和处理队列任务。&#13;
cmd 下 监听队列&#13;
```&#13;
    yii queue/listen&#13;
```&#13;
5.添加到队列&#13;
--------&#13;
&#13;
将任务添加到队列:&#13;
```php&#13;
    Yii::$app-&gt;queue-&gt;push(new frontend\components\DownloadJob([&#13;
        'url' =&gt; 'http://example.com/image.jpg',&#13;
        'file' =&gt; '/tmp/image.jpg',&#13;
    ]));&#13;
```&#13;
&#13;
将任务推送到队列中延时5分钟运行:&#13;
```php&#13;
    Yii::$app-&gt;queue-&gt;delay(5 * 60)-&gt;push(new frontend\components\DownloadJob([&#13;
        'url' =&gt; 'http://example.com/image.jpg',&#13;
        'file' =&gt; '/tmp/image.jpg',&#13;
    ]));&#13;
```&#13;
6.测试&#13;
-----&#13;
&#13;
执行 5 中的程序，控制台监听到，便会后台自动 下载http://example.com/image.jpg到本地为/tmp/image.jpg&#13;
&#13;
启动worker&#13;
========&#13;
&#13;
可以使用Supervisor或Systemd 来启动多进程worker，也可以使用 Cron，我们这里主要说一下Supervisor&#13;
&#13;
centos7 supervisor的使用&#13;
=====================&#13;
&#13;
1.安装supervisor&#13;
--------------&#13;
```&#13;
    yum update&#13;
    yum install epel-release&#13;
    yum install -y supervisor&#13;
    #开机启动&#13;
    systemctl enable supervisord&#13;
    #启动&#13;
    systemctl start supervisord&#13;
```&#13;
2.supervisor 命令&#13;
---------------&#13;
```&#13;
    supervisorctl status 查看进程状态&#13;
    supervisorctl reload 重启supervisord&#13;
    supervisorctl start|stop|restart 启动关闭重启进程&#13;
```&#13;
3.添加配置文件&#13;
--------&#13;
&#13;
Supervisor 配置文件通常在 /etc/supervisord.d 目录下. 你可以创建一些配置文件在这里.&#13;
**注：文件名是.ini结尾**&#13;
&#13;
下面就是个例子:&#13;
```&#13;
    [program:yii-queue-worker]&#13;
    process_name=%(program_name)s_%(process_num)02d&#13;
    command=/usr/bin/php /var/www/my_project/yii queue/listen --verbose=1 --color=0&#13;
    autostart=true&#13;
    autorestart=true&#13;
    user=www-data&#13;
    numprocs=4&#13;
    redirect_stderr=true&#13;
    stdout_logfile=/var/www/my_project/log/yii-queue-worker.log&#13;
```&#13;
&#13;

&gt; 本文由 [systemofdown](https://www.bestyii.com/member/systemofdown) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>yii2,centos7</tags><last_comment_time>1574214782</last_comment_time><last_comment_username></last_comment_username><view_count>5372</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574214782</created_at><updated_at>1574214782</updated_at></item><item><id>8</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>3</user_id><title>CentOS7下GitLab跨大版本升级</title><author></author><excerpt></excerpt><image></image><content>备份&amp;升级&#13;
=====&#13;
&#13;
1.在升级前一定要做好备份，记录自己当前gitlab-ce的版本号。&#13;
&#13;
查看当前gitlab版本号&#13;
&#13;
```&#13;
yum list | grep gitlab-ce&#13;
```&#13;
2.备份文件&#13;
&#13;
```&#13;
gitlab-rake gitlab:backup:create&#13;
```&#13;
&#13;
&gt; 在目录/var/opt/gitlab/backups/下会生成一个备份文件如：1552552057_gitlab_backup.tar，其中1552552057即为此次备份都版本号。&#13;
**还原备份（失败）**&#13;
命令：*gitlab-rake gitlab:backup:restore BACKUP=备份版本号*&#13;
&#13;
3.配置gitlab-yum 本地源&#13;
如清华的镜像：&#13;
[https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/](https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/)&#13;
&#13;
```&#13;
[root@localhost ~]# cat &lt;&lt; EOF &gt; /etc/yum.repos.d/gitlab-ce.repo&#13;
&#13;
&gt; [gitlab-ce]&#13;
&gt; name=gitlab-ce&#13;
&gt; baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/&#13;
&gt; repo_gpgcheck=0&#13;
&gt; gpgcheck=0&#13;
&gt; enable=1&#13;
&gt; gpgkey=https://packages.gitlab.com/gpg.key&#13;
&gt; EOF&#13;
```&#13;
4.yum install安装&#13;
&gt; **升级Gitlab（注意：由于升级不能跨越大版本号，因此只能升级到当前大版本号到最高版本，方可升级到下一个大版本号）**&#13;
&#13;
依次执行下面指令逐步升级，在每一步安装成功后如果发现界面500，不可访问，那么执行gitlab-ctl reconfigure指令刷新配置文件。（一定保证数据可以正常访问方可执行下一步升级指令）&#13;
&#13;
&gt; 升级过程中有可能会升级PostgreSQL，命令：sudo gitlab-ctl pg-upgrade&#13;
&#13;
查看所有可用的版本&#13;
```&#13;
yum list gitlab-ce --showduplicate  | sort -r&#13;
```&#13;
然后手动更新&#13;
```&#13;
yum install gitlab-ce-10.8.7-ce.0.el7&#13;
yum install gitlab-ce-11.0.0-ce.0.el7&#13;
yum install gitlab-ce-11.11.5-ce.0.el7&#13;
yum install gitlab-ce-12.0.0-ce.0.el7&#13;
...&#13;
yum update&#13;
```&#13;
查看当前版本号&#13;
&#13;
```&#13;
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION&#13;
```&#13;
&gt; 本文由 [systemofdown](https://www.bestyii.com/member/systemofdown) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。&#13;
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>gitlab,yum,centos7</tags><last_comment_time>1574214939</last_comment_time><last_comment_username></last_comment_username><view_count>7547</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574214939</created_at><updated_at>1656141251</updated_at></item><item><id>9</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>3</user_id><title>CentOS7下gitlab邮件服务设置</title><author></author><excerpt></excerpt><image></image><content>1.配置文件位置&#13;
--------&#13;
&#13;
```&#13;
vim /etc/gitlab/gitlab.rb&#13;
```&#13;
&#13;
以腾讯企业邮箱为例其它邮箱大同小异&#13;
```&#13;
gitlab_rails['smtp_enable'] = true&#13;
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"&#13;
gitlab_rails['smtp_port'] = 465&#13;
gitlab_rails['smtp_user_name'] = "邮箱地址"&#13;
gitlab_rails['smtp_password'] = "password"&#13;
gitlab_rails['smtp_authentication'] = "login"&#13;
gitlab_rails['smtp_enable_starttls_auto'] = true&#13;
gitlab_rails['smtp_tls'] = true&#13;
gitlab_rails['smtp_domain'] = "exmail.qq.com"&#13;
gitlab_rails['gitlab_email_from'] = '邮箱地址'&#13;
```&#13;
&#13;
2.更新配置&#13;
------&#13;
&#13;
```&#13;
    gitlab-ctl reconfigure&#13;
```&#13;
&#13;
3.重启服务&#13;
------&#13;
&#13;
```&#13;
gitlab-ctl restart&#13;
```&#13;
&#13;
4.非必需步骤进入控制台&#13;
------------&#13;
&#13;
测试邮件服务是否正常&#13;
```&#13;
gitlab-rails console&#13;
等到出现 “&gt;”再执行下面命令&#13;
Notify.test_email("XXX@XXX.XX","title","gitlab").deliver_now&#13;
```
&gt; 本文由 [systemofdown](https://www.bestyii.com/member/systemofdown) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>gitlab,yum,centos7</tags><last_comment_time>1574215086</last_comment_time><last_comment_username></last_comment_username><view_count>3562</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574215086</created_at><updated_at>1574215086</updated_at></item><item><id>10</id><site>bestyii</site><type>topic</type><post_meta_id>11</post_meta_id><user_id>3</user_id><title>国内 PHP Composer 镜像列表</title><author></author><excerpt></excerpt><image></image><content>Composer 是什么？&#13;
==============&#13;
Composer 是一个 PHP 包管理的系统，现在越来越多的 PHP 使用 Composer 来管理包。比如 FastAdmin、 ThinkPHP、Laravel 等都是用 Composer 进行 php 包的管理。&#13;
&#13;
镜像列表&#13;
=======&#13;
国内也很多开发者使用 Composer，但由于不可控因素，官方的服务器常常连接不上。所以这里收集了一下国内镜像列表。（先后次序会不定期调整）&#13;
&#13;
镜像名 | 地址 | 赞助商 | 更新频率 | 备注&#13;
- | - | - | - | -&#13;
阿里云 Composer 镜像 | https://mirrors.aliyun.com/composer/ | 阿里云 | 96 秒 | 推荐&#13;
腾讯云 Composer 镜像 | https://mirrors.cloud.tencent.com/composer/ | 腾讯云 | 24 小时 | -&#13;
华为云 Composer 镜像 | https://repo.huaweicloud.com/repository/php/ | 华为云 | 未知 | 未知&#13;
&#13;
如何使用&#13;
=======&#13;
&#13;
全局配置（推荐）&#13;
--------------------&#13;
所有项目都会使用该镜像地址：&#13;
1. 阿里云&#13;
```&#13;
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/&#13;
```&#13;
2. 腾讯云&#13;
```&#13;
composer config -g repos.packagist composer https://mirrors.cloud.tencent.com/composer/&#13;
```&#13;
3. 华为云&#13;
```&#13;
composer config -g repos.packagist composer https://repo.huaweicloud.com/repository/php/&#13;
```&#13;
&#13;
取消配置：&#13;
```&#13;
composer config -g --unset repos.packagist&#13;
```&#13;
项目配置&#13;
----------&#13;
仅修改当前工程配置，仅当前工程可使用该镜像地址：&#13;
```&#13;
composer config repo.packagist composer https://mirrors.aliyun.com/composer/&#13;
```&#13;
取消配置：&#13;
```&#13;
composer config --unset repos.packagist&#13;
````&#13;
调试&#13;
-----&#13;
composer 命令增加 -vvv 可输出详细的信息，命令如下：&#13;
```&#13;
composer -vvv require alibabacloud/sdk&#13;
```&#13;
</content><tags>composer,php</tags><last_comment_time>1574230513</last_comment_time><last_comment_username></last_comment_username><view_count>4431</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>1</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574230513</created_at><updated_at>1574254684</updated_at></item><item><id>11</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>2</user_id><title>CentOS7 Docker部署ELK(Elasticsearch Logstash Kibana)</title><author></author><excerpt></excerpt><image></image><content>需求背景&#13;
=======&#13;
&#13;
业务发展越来越庞大，服务器越来越多&#13;
各种访问日志、应用日志、错误日志量越来越多，导致运维人员无法很好的去管理日志&#13;
开发人员排查问题，需要到服务器上查日志，不方便&#13;
运营人员需要一些数据，需要我们运维到服务器上分析日志&#13;
&#13;
ELK vs. Elastic Stack&#13;
================&#13;
&#13;
ELK是三个开源软件的缩写，分别为：Elasticsearch 、 Logstash以及Kibana , 它们都是开源软件。&#13;
目前由于原本的ELK Stack成员中加入了 Beats 工具所以已改名为Elastic Stack。&#13;
Beats，它是一个轻量级的日志收集处理工具(Agent)，占用资源少，适合于在各个服务器上搜集日志后传输给Logstash，官方也推荐此工具.&#13;
&#13;
![Elastic Stack](https://statics.bestyii.com/IlruqcCVqgKZSdwB.png "Elastic Stack")&#13;
&#13;
Elastic Stack包含&#13;
---------------------&#13;
&#13;
Elasticsearch是个开源分布式搜索引擎，提供搜集、分析、存储数据三大功能。它的特点有：分布式，零配置，自动发现，索引自动分片，索引副本机制，restful风格接口，多数据源，自动搜索负载等。详细可参考[Elasticsearch权威指南](https://www.elastic.co/guide/en/elastic-stack/current/index.html "Elasticsearch权威指南")&#13;
&#13;
Logstash 主要是用来日志的搜集、分析、过滤日志的工具，支持大量的数据获取方式。一般工作方式为c/s架构，client端安装在需要收集日志的主机上，server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。&#13;
&#13;
Kibana 也是一个开源和免费的工具，Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面，可以帮助汇总、分析和搜索重要数据日志。&#13;
&#13;
Beats在这里是一个轻量级日志采集器，其实Beats家族有6个成员，Beats所占系统的CPU和内存几乎可以忽略不计。&#13;
&#13;
- Packetbeat： 网络数据（收集网络流量数据）&#13;
- Metricbeat： 指标 （收集系统、进程和文件系统级别的 CPU 和内存使用情况等数据）&#13;
- Filebeat： 日志文件（收集文件数据）&#13;
- Winlogbeat： windows事件日志（收集 Windows 事件日志数据）&#13;
- Auditbeat：审计数据 （收集审计日志）&#13;
- Heartbeat：运行时间监控 （收集系统运行时的数据）&#13;
准备工作&#13;
=======&#13;
Docker 安装 请参考[CentOS7 下 Docker 升级到最新版本](https://www.bestyii.com/topic/4 "CentOS7 下 Docker 升级到最新版本")&#13;
&#13;
准备镜像&#13;
=======&#13;
6.0之后官方开始自己维护镜像版本:https://www.docker.elastic.co/ 。找到需要的ELK镜像地址，pull下来就好了。&#13;
```&#13;
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.4.2&#13;
docker pull docker.elastic.co/logstash/logstash:7.4.2&#13;
docker pull docker.elastic.co/kibana/kibana:7.4.2&#13;
```&#13;
官方pull下来之后镜像名太长了，所以我将镜像全部重新打了tag&#13;
```&#13;
docker tag docker.elastic.co/elasticsearch/elasticsearch:7.4.2 elasticsearch:latest&#13;
docker tag docker.elastic.co/logstash/logstash:7.4.2 logstash:latest&#13;
docker tag docker.elastic.co/kibana/kibana:7.4.2 kibana:latest&#13;
```&#13;
&#13;
使用`docker images`查看,确认是否成功&#13;
```&#13;
# docker images&#13;
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE&#13;
docker.elastic.co/logstash/logstash             7.4.2               642b82780655        3 weeks ago         889MB&#13;
logstash                                        latest              642b82780655        3 weeks ago         889MB&#13;
kibana                                          latest              230d3ded1abc        3 weeks ago         1.1GB&#13;
docker.elastic.co/kibana/kibana                 7.4.2               230d3ded1abc        3 weeks ago         1.1GB&#13;
docker.elastic.co/elasticsearch/elasticsearch   7.4.2               b1179d41a7b4        3 weeks ago         855MB&#13;
elasticsearch                                   latest              b1179d41a7b4        3 weeks ago         855MB&#13;
```&#13;
安装docker版本ElasticSearch&#13;
======================&#13;
在elasticsearch的docker版本文档中，官方提到了vm.max_map_count的值在生产环境最少要设置成262144。设置的方式有两种&#13;
1. 永久性的修改,在/etc/sysctl.conf文件中添加一行：&#13;
```&#13;
grep vm.max_map_count /etc/sysctl.conf # 查找当前的值。&#13;
vm.max_map_count=262144 # 修改或者新增&#13;
```&#13;
2. 正在运行的机器&#13;
```&#13;
sysctl -w vm.max_map_count=262144&#13;
```&#13;
之后我们执行命令，暴露容器的9200，9300端口，方便我们在其它机器上可以通过类似head插件去做es索引的操作等。执行命令为：&#13;
```&#13;
docker run -p 127.0.0.1:9200:9200 -p 9300:9300 --name elasticsearch -e "discovery.type=single-node" elasticsearch&#13;
```&#13;
&gt; 如果实际使用中，可能需要设置集群等操作。因实际情况而定。如果你需要存储历史数据，那么就可能需要将data目录保存到本地，使用-v，或者mount参数挂载本地一个目录。如果实际使用中，可能需要设置集群等操作。因实际情况而定。如果你需要存储历史数据，那么就可能需要将data目录保存到本地，使用-v，或者mount参数挂载本地一个目录。&#13;
&#13;
设置外部目录&#13;
--&#13;
在本文实例中采用的就是存储到外部目录，文件系统必须是elasticsearch用户可读。&#13;
&gt; 默认情况下, Elasticsearch 运行的容器内部用户 elasticsearch 的 uid:gid 是 1000:1000.&#13;
&#13;
```&#13;
mkdir esdatadir&#13;
chmod g+rwx esdatadir&#13;
chgrp 1000 esdatadir&#13;
```&#13;
在启动命令中添加参数&#13;
```&#13;
docker run  -e ES_JAVA_OPTS="-Xms8g -Xmx8g"  -d -v /srv/esdatadir/logs:/usr/share/elasticsearch/logs   -v /srv/esdatadir/data:/usr/share/elasticsearch/data -p 127.0.0.1:9200:9200 -p 9300:9300  --restart=always --name elasticsearch -e "discovery.type=single-node" elasticsearch&#13;
```&#13;
&gt; 设置JVM堆的大小&#13;
使用环境变量 `ES_JAVA_OPTS` 去定义堆的大小. 例如, 设置为 16GB, 在docker run 时加入参数` -e ES_JAVA_OPTS="-Xms16g -Xmx16g"`.&#13;
- Elasticsearch 在jvm.options中指定了Xms(最小)和Xmx(最大)的堆的设置。所设置的值取决于你的服务器的可用内存大小。&#13;
- 最小堆的大小和最大堆的大小应该相等。&#13;
- 设置最大堆的值不能超过你物理内存的50%，要确保有足够多的物理内存来保证内核文件缓存。&#13;
&#13;
安装docker版本kibana&#13;
=================&#13;
启动kibana&#13;
-------------&#13;
kibana的作用主要是帮助我们将日志文件可视化。便于我们操作，统计等。它需要ES服务，所以我们将部署好的es和kibana关联起来，主要用到的参数是`--link`:&#13;
&#13;
```&#13;
docker run -d -p 5601:5601  --restart=always --link elasticsearch -e ELASTICSEARCH_URL=http://elasticsearch:9200 kibana&#13;
```&#13;
使用link参数，会在kibana容器hosts文件中加入elasticsearch ip地址，这样我们就直接通过定义的name来访问es服务了。&#13;
&#13;
使用Nginx作为反向代理访问kibana&#13;
------------------------------------------&#13;
安装httpd-tools&#13;
```&#13;
yum install -y httpd-tools&#13;
```&#13;
安装nginx，采用nginx官方源安装,详细参考[Nginx官方手册](http://nginx.org/en/linux_packages.html#RHEL-CentOS "Nginx官方手册")&#13;
```&#13;
sudo yum install yum-utils&#13;
```&#13;
创建源配置文件`/etc/yum.repos.d/nginx.repo`,并在文件中写入以下信息&#13;
```&#13;
[nginx-stable]&#13;
name=nginx stable repo&#13;
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/&#13;
gpgcheck=1&#13;
enabled=1&#13;
gpgkey=https://nginx.org/keys/nginx_signing.key&#13;
module_hotfixes=true&#13;
&#13;
[nginx-mainline]&#13;
name=nginx mainline repo&#13;
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/&#13;
gpgcheck=1&#13;
enabled=0&#13;
gpgkey=https://nginx.org/keys/nginx_signing.key&#13;
module_hotfixes=true&#13;
```&#13;
安装nginx&#13;
```&#13;
sudo yum install nginx&#13;
```&#13;
创建nginx对应的kibana配置文件&#13;
```&#13;
cd /etc/nginx/conf.d/&#13;
vim kibana.conf&#13;
```&#13;
加入下列参数&#13;
```&#13;
server {&#13;
    listen 80;&#13;
&#13;
    server_name elk.bestyii.com; #别忘了改成自己的&#13;
&#13;
    auth_basic "Restricted Access";&#13;
    auth_basic_user_file /etc/nginx/.kibana-user;&#13;
&#13;
    location / {&#13;
        proxy_pass http://127.0.0.1:5601;&#13;
        proxy_http_version 1.1;&#13;
        proxy_set_header Upgrade $http_upgrade;&#13;
        proxy_set_header Connection 'upgrade';&#13;
        proxy_set_header Host $host;&#13;
        proxy_cache_bypass $http_upgrade;&#13;
    }&#13;
}&#13;
```&#13;
接下来我们为这个访问地址加上个登录授权的账号&#13;
```&#13;
sudo htpasswd -c /etc/nginx/.kibana-user bestyii&#13;
#输入两边密码就可以了&#13;
```&#13;
测试以下配置文件是否正确&#13;
```&#13;
nginx -t&#13;
```&#13;
确认争取无误后，设置nignx开机启动并启动&#13;
```&#13;
systemctl enable nginx&#13;
systemctl start nginx&#13;
```&#13;
现在我们就可以打开浏览器访问到kibana了。&#13;
安装logstash&#13;
==========&#13;
前面的kibana和ES的安装，如果我们在开发环境中并不需要太多的关注他们的详细配置。但是logstash和filebeat我们需要注意下它的配置，因为这两者是我们完成需求的重要点。&#13;
&#13;
logstash我们只让它进行日志处理，处理完之后将其输出到elasticsearch。&#13;
例如我们需要收集系统日志，我们先定义一些设置，保存在外部目录`/srv/logstashdir/logstash.conf`中，运行docker的时候连接过去方便修改。&#13;
```&#13;
input {&#13;
  beats {&#13;
    port =&gt; 5044&#13;
  }&#13;
}&#13;
filter {&#13;
  if [type] == "syslog" {&#13;
    grok {&#13;
      match =&gt; { "message" =&gt; "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }&#13;
      add_field =&gt; [ "received_at", "%{@timestamp}" ]&#13;
      add_field =&gt; [ "received_from", "%{host}" ]&#13;
    }&#13;
    date {&#13;
      match =&gt; [ "syslog_timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]&#13;
    }&#13;
  }&#13;
}&#13;
output {&#13;
  elasticsearch {&#13;
    hosts =&gt; ["elasticsearch:9200"]&#13;
    manage_template =&gt; false&#13;
    index =&gt; "%{[@metadata][beat]}-%{+YYYY.MM.dd}"&#13;
    document_type =&gt; "%{[@metadata][type]}"&#13;
  }&#13;
}&#13;
```&#13;
&gt; 注意：hosts的地址，我们用的是`elasticsearch:9200`,是因为下面的启动命令是用了` --link elasticsearch `,前面提到link的作用，所以docker内部会设置hosts。&#13;
&#13;
启动docker版logstash&#13;
```&#13;
docker run  -d -p 5044:5044 -p 9600:9600 --rm -it  --name logstash --link elasticsearch -v /srv/logstashdir/config:/usr/share/logstash/config -v /srv/logstashdir/pipeline:/usr/share/logstash/pipeline logstash&#13;
```&#13;
至此服务端已经算是搭建完成了。&#13;
&#13;
在客户端中安装filebeat&#13;
==================&#13;
客户端安装，可以是docker版本也可以是直接yum安装，这就无所谓了。&#13;
本文就采用是yum安装为例。&#13;
&#13;
安装Filebeat&#13;
---------------&#13;
导入elasticsearch key&#13;
```&#13;
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch&#13;
```&#13;
创建源配置文件`vim /etc/yum.repos.d/elasticsearch.repo`并添加官方源信息&#13;
```&#13;
[elasticsearch-7.x]&#13;
name=Elasticsearch repository for 7.x packages&#13;
baseurl=https://artifacts.elastic.co/packages/7.x/yum&#13;
gpgcheck=1&#13;
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch&#13;
enabled=1&#13;
autorefresh=1&#13;
type=rpm-md&#13;
```&#13;
安装&#13;
```&#13;
yum -y install filebeat&#13;
```&#13;
配置 Filebeat&#13;
----------------&#13;
配置文件在本地目录`/etc/filebeat`中，编辑`filebeat.yml`&#13;
```&#13;
vim /etc/filebeat/filebeat.yml&#13;
```&#13;
修改输出的ip地址&#13;
```&#13;
output.logstash:&#13;
  # The Logstash hosts&#13;
  hosts: ["10.5.5.25:5044"]&#13;
```&#13;
接下来, 我们需要启用filebeat modules. 运行 filebeat 命令获取可用 filebeat modules 清单.&#13;
```&#13;
filebeat modules list&#13;
```&#13;
启用 `system` module&#13;
```&#13;
filebeat modules enable system&#13;
```&#13;
 filebeat system module 是配合着配置文件`modules.d/system.yml`使用的 .&#13;
我们用的是CentOS系统，所以需要调整一下&#13;
```&#13;
vim /etc/filebeat/modules.d/system.yml&#13;
```&#13;
我们要监控登录的日志和系统日志&#13;
```&#13;
# Syslog&#13;
  syslog:&#13;
    enabled: true&#13;
    var.paths: ["/var/log/messages"]&#13;
&#13;
  # Authorization logs&#13;
  auth:&#13;
    enabled: true&#13;
    var.paths: ["/var/log/secure"]&#13;
```&#13;
现在就配置好了，启动，并设置开机自动启动。&#13;
```&#13;
systemctl enable filebeat&#13;
systemctl start filebeat&#13;
```&#13;
看一下运行的状态&#13;
```&#13;
systemctl status filebeat&#13;
```&#13;
在Kibana中测试&#13;
============&#13;
登录到 elk.bestyii.com，输入用户名密码，进入到 Kibana Dashboard。&#13;
![](https://statics.bestyii.com/L9sdFAphS2HUZ8QQ.png)&#13;
&#13;
点击 'Connect to your Elasticsearch index'，创建索引。&#13;
&#13;
创建 'filebeat-*' 索引规则，并点击'Next step' 按钮。&#13;
![](https://statics.bestyii.com/CLO78pxeUqGhy5TG.png)&#13;
&#13;
 filter name, 选择 '@timestamp' 并点击 'Create index pattern'。&#13;
![](https://statics.bestyii.com/KQhji-i1qUrVGoWn.png)&#13;
&#13;
此时 'filebeat-*' 索引规则已经创建完成, 点击 'Discover' 菜单离开。&#13;
![](https://statics.bestyii.com/n7q74QS-4WSkx1sr.png)&#13;
&#13;
你就可以看到从filebeat发出来的log 数据。&#13;
CentOS 7 system Logs&#13;
![](https://statics.bestyii.com/VUqAM3qUBt3nQyTl.png)&#13;
&#13;
&#13;
&gt; 本文由 [ez](https://www.bestyii.com/member/ez) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。&#13;
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>ELK,Elasticsearch,Logstash,Kibana,docker,centos7</tags><last_comment_time>1574649668</last_comment_time><last_comment_username></last_comment_username><view_count>6248</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574649668</created_at><updated_at>1581614271</updated_at></item><item><id>12</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>2</user_id><title>CentOS7 挂载新硬盘</title><author></author><excerpt></excerpt><image></image><content>查看系统是否检测到新的硬盘设备&#13;
=========================&#13;
hd, sd, vd 都试试&#13;
```&#13;
ls /dev/ |grep sd&#13;
```&#13;
我的服务器的结果&#13;
```&#13;
# ls /dev/ |grep vd&#13;
vda&#13;
vda1&#13;
vdb&#13;
```&#13;
就是他vdb，没有分区&#13;
&#13;
查看分区大小&#13;
----------------&#13;
```&#13;
fdisk -l&#13;
```&#13;
&#13;
新的硬盘分区&#13;
===========&#13;
```&#13;
fdisk /dev/vdb&#13;
```&#13;
开始&#13;
```&#13;
]# fdisk /dev/vdb&#13;
Welcome to fdisk (util-linux 2.23.2).&#13;
&#13;
Changes will remain in memory only, until you decide to write them.&#13;
Be careful before using the write command.&#13;
&#13;
Device does not contain a recognized partition table&#13;
Building a new DOS disklabel with disk identifier 0xf39a3343.&#13;
&#13;
&#13;
Command (m for help): m # 先输入个m看看菜单&#13;
Command action&#13;
   a   toggle a bootable flag&#13;
   b   edit bsd disklabel&#13;
   c   toggle the dos compatibility flag&#13;
   d   delete a partition&#13;
   g   create a new empty GPT partition table&#13;
   G   create an IRIX (SGI) partition table&#13;
   l   list known partition types&#13;
   m   print this menu&#13;
   n   add a new partition&#13;
   o   create a new empty DOS partition table&#13;
   p   print the partition table&#13;
   q   quit without saving changes&#13;
   s   create a new empty Sun disklabel&#13;
   t   change a partition's system id&#13;
   u   change display/entry units&#13;
   v   verify the partition table&#13;
   w   write table to disk and exit&#13;
   x   extra functionality (experts only)&#13;
```&#13;
&#13;
一般新建一个分区的输入 n ，分区的类型选 p 然后选分区起始扇区和结尾扇区。&#13;
分配完成后，输入w 保存&#13;
```&#13;
Command (m for help): n&#13;
Partition type:&#13;
   p   primary (0 primary, 0 extended, 4 free)&#13;
   e   extended&#13;
Select (default p): p&#13;
Partition number (1-4, default 1): 1&#13;
First sector (2048-2097151999, default 2048): &#13;
Using default value 2048&#13;
Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): &#13;
Using default value 2097151999&#13;
Partition 1 of type Linux and of size 1000 GiB is set&#13;
&#13;
Command (m for help): w&#13;
The partition table has been altered!&#13;
&#13;
Calling ioctl() to re-read partition table.&#13;
Syncing disks.&#13;
```&#13;
&#13;
设置分区格式&#13;
==========&#13;
&#13;
给分区设置xfs格式,可能时间会久，不要慌张，等一下就好了&#13;
&#13;
```&#13;
mkfs.xfs -f /dev/vdb1&#13;
```&#13;
&#13;
挂载&#13;
====&#13;
&#13;
临时挂载&#13;
-----------&#13;
&#13;
先创建目录，再将分区挂载到目录上。临时挂载重启后需要重新挂载&#13;
&#13;
```&#13;
mkdir /data1&#13;
mount -t xfs /dev/vdb1 /data1&#13;
```&#13;
或挂载已有目录中，我习惯用srv作为扩展，所以&#13;
```&#13;
mount -t xfs /dev/vdb1 /srv&#13;
```&#13;
&#13;
永久挂载&#13;
-----------&#13;
&#13;
修改系统挂载硬盘的文件，其中0 0 表示在在开机时不对分区进行检查&#13;
&#13;
```&#13;
vim /etc/fstab&#13;
#添加以下配置&#13;
/dev/vdb1       /srv      xfs   defaults   0 0&#13;
```&#13;
&#13;
补充命令&#13;
=======&#13;
```&#13;
#查看已经挂载的分区和文件系统类型&#13;
df -T&#13;
&#13;
#显示出所有挂载和未挂载的分区，但不显示文件系统类型&#13;
fdisk -l&#13;
&#13;
#查看未挂载的文件系统类型，以及哪些分区尚未格式化&#13;
parted -l&#13;
&#13;
#查看未挂载的文件系统类型&#13;
lsblk -f&#13;
```&#13;
&gt; 本文由 [ez](https://www.bestyii.com/member/ez) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。&#13;
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>文件系统,centos7</tags><last_comment_time>1574651278</last_comment_time><last_comment_username></last_comment_username><view_count>4316</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574651278</created_at><updated_at>1581614335</updated_at></item><item><id>13</id><site>bestyii</site><type>topic</type><post_meta_id>11</post_meta_id><user_id>3</user_id><title>解决 composer proc_open(): Cannot allocate memory</title><author></author><excerpt></excerpt><image></image><content>问题描述&#13;
=======&#13;
在linux服务器使用composer部署yii项目时，出现“proc_open(): fork failed - Cannot allocate memory”&#13;
&#13;
也就是提示“提示内存不足”，我们可以通过创建swap分区解决这个问题。&#13;
&#13;
解决方法&#13;
=======&#13;
&#13;
- 先运行 free -m 看下空间是多少&#13;
- 在命令行环境依次运行以下三条命令&#13;
&#13;
```&#13;
dd if=/dev/zero of=/var/swap.1 bs=1M count=1024&#13;
mkswap /var/swap.1&#13;
swapon /var/swap.1&#13;
```&#13;
&#13;
解释&#13;
&#13;
dd 从/dev/zero设备复制出一个1G大小的文件/var/swap.1&#13;
&#13;
mkswap 格式化/var/swap.1&#13;
&#13;
swapon 将swap分区挂在到文件系统&#13;
&#13;
然后输入free -m 查看内存使用量信息</content><tags>composer,centos7</tags><last_comment_time>1574654716</last_comment_time><last_comment_username></last_comment_username><view_count>3503</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574654716</created_at><updated_at>1574654796</updated_at></item><item><id>14</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>2</user_id><title>CentOS 7 下 Nginx 安全加固配置规范</title><author></author><excerpt></excerpt><image></image><content>#目标&#13;
安全，安全，安全&#13;
&#13;
#WEB服务器搭建&#13;
在做yii开发的时候，离不开nginx+php-fpm组合。所以我们是在CentOS 7 中使用Nginx作为web服务&#13;
##一. Nginx 安装&#13;
安装nginx，采用nginx官方源安装,详细参考[Nginx官方手册](http://nginx.org/en/linux_packages.html#RHEL-CentOS "Nginx官方手册")&#13;
###1. 准备工作&#13;
开始安装前，需要一些前置工具的安装&#13;
```&#13;
sudo yum install yum-utils&#13;
```&#13;
###2. 官方源设置&#13;
创建源配置文件`/etc/yum.repos.d/nginx.repo`,并在文件中写入以下信息&#13;
```&#13;
[nginx-stable]&#13;
name=nginx stable repo&#13;
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/&#13;
gpgcheck=1&#13;
enabled=1&#13;
gpgkey=https://nginx.org/keys/nginx_signing.key&#13;
module_hotfixes=true&#13;
&#13;
[nginx-mainline]&#13;
name=nginx mainline repo&#13;
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/&#13;
gpgcheck=1&#13;
enabled=0&#13;
gpgkey=https://nginx.org/keys/nginx_signing.key&#13;
module_hotfixes=true&#13;
```&#13;
###3. 安装&#13;
安装nginx&#13;
```&#13;
sudo yum install nginx&#13;
```&#13;
&#13;
设置nignx开机启动并启动&#13;
```&#13;
systemctl enable nginx&#13;
systemctl start nginx&#13;
```&#13;
##二. 配置Yii网站&#13;
创建nginx对应的yii配置文件&#13;
```&#13;
vim /etc/nginx/conf.d/yii.conf&#13;
```&#13;
加入下列参数，参考：[推荐使用的 Nginx 配置](https://www.yiiframework.com/doc/guide/2.0/zh-cn/start-installation#recommended-nginx-configuration "推荐使用的 Nginx 配置")&#13;
```&#13;
server {&#13;
    charset utf-8;&#13;
    client_max_body_size 128M;&#13;
&#13;
    listen 80; ## listen for ipv4&#13;
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6&#13;
&#13;
    server_name www.bestyii.com;&#13;
    root        /path/to/basic/web;&#13;
    index       index.php;&#13;
&#13;
    access_log  /path/to/basic/log/access.log;&#13;
    error_log   /path/to/basic/log/error.log;&#13;
&#13;
    location / {&#13;
        # Redirect everything that isn't a real file to index.php&#13;
        try_files $uri $uri/ /index.php$is_args$args;&#13;
    }&#13;
&#13;
    # uncomment to avoid processing of calls to non-existing static files by Yii&#13;
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {&#13;
    #    try_files $uri =404;&#13;
    #}&#13;
    #error_page 404 /404.html;&#13;
&#13;
    # deny accessing php files for the /assets directory&#13;
    location ~ ^/assets/.*\.php$ {&#13;
        deny all;&#13;
    }&#13;
&#13;
    location ~ \.php$ {&#13;
        include fastcgi_params;&#13;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;&#13;
        fastcgi_pass 127.0.0.1:9000;&#13;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;&#13;
        try_files $uri =404;&#13;
    }&#13;
&#13;
    location ~* /\. {&#13;
        deny all;&#13;
    }&#13;
}&#13;
```&#13;
&gt; 使用该配置时，你还应该在 `php.ini` 文件中设置 `cgi.fix_pathinfo=0` ， 能避免掉很多不必要的 stat() 系统调用。&#13;
&#13;
&gt;还要注意当运行一个 HTTPS 服务器时，需要添加 `fastcgi_param HTTPS on;` 一行， 这样 Yii 才能正确地判断连接是否安全。&#13;
&#13;
测试以下配置文件是否正确&#13;
```&#13;
nginx -t&#13;
```&#13;
确认无误后，重启Nignx 使之生效。&#13;
```&#13;
systemctl restart nginx&#13;
```&#13;
&#13;
#安全加固&#13;
nginx的多年发展，自身的安全漏洞比较少，一般利用软件包管理器（yum）升级一下就好了。&#13;
&#13;
现在侧重讲述如何利用nginx来加固web应用，干一些应用防火墙（WAF）干的活。&#13;
##默认参数重置&#13;
&#13;
软件本身会有一些默认参数，往往这些参数会暴露更多的信息，或者留有安全隐患。所以我们要合理的调整系统参数。&#13;
###防止Host头攻击&#13;
访问网站时如果访问路径中缺少/，大多数中间件都会自动将路径补全，返回302或301跳转如下图，Location位置的域名会使用Host头的值。&#13;
&#13;
&gt; 这种情况实际上风险较低，难以构成Host头攻击。但是由于大多漏洞扫描器会将这种情况检测为Host头攻击，为了通过上级检查或各种审核，大多数甲方单位会要求修复漏洞，彻底解决问题。&#13;
&#13;
添加一个默认server，当host头被修改匹配不到server时会跳到该默认server，该默认server直接返回403错误。&#13;
```&#13;
server {&#13;
&#13;
       listen 80 default;&#13;
&#13;
       server_name _;&#13;
&#13;
       location / {&#13;
&#13;
            return 403;&#13;
&#13;
       }&#13;
&#13;
}&#13;
```&#13;
&#13;
###禁用autoindex&#13;
确保nginx.conf配置文件上禁用autoindex，即autoindex off或者没有配置autoindex。&#13;
&#13;
###关闭服务器标记&#13;
`server_tokens off;`建议加载全局配置中。如：&#13;
```&#13;
http{&#13;
    include       naxsi_core.rules;&#13;
    include      mime.types;&#13;
    default_type  application/octet-stream;&#13;
    sendfile        on;&#13;
    server_tokens off;#关闭服务器标记&#13;
    ... ...&#13;
```&#13;
&#13;
###自定义缓存&#13;
设置自定义缓存以限制缓冲区溢出攻击。nginx.conf配置如下：&#13;
```&#13;
http{&#13;
    ... ...&#13;
    server{&#13;
        ... ...&#13;
        client_body_buffer_size  16K;&#13;
       client_header_buffer_size  1k;&#13;
        client_max_body_size  1m;&#13;
       large_client_header_buffers  4  8k;&#13;
        ... ...&#13;
```&#13;
&gt;  注：上述的参数不是最优参数，仅供参考。&#13;
&#13;
设置timeout设置timeout设低来防御DOS攻击，nginx.conf配置如下：&#13;
```&#13;
http {&#13;
    ... ...&#13;
       client_body_timeout   10;&#13;
       client_header_timeout  30;&#13;
       keepalive_timeout     30  30;&#13;
       send_timeout          10;&#13;
```&#13;
&#13;
###限制访问的方法&#13;
在目前的应用系统中值使用到POST和GET方法，所以除了它们之外，其他方式的请求均可拒绝。Nginx.conf配置如下：&#13;
```&#13;
server{&#13;
       ... ...&#13;
       if($request_method !~ ^(GET|HEAD|POST)$) {        &#13;
                     return404;&#13;
              }&#13;
       ... ...&#13;
```&#13;
&#13;
###封杀各种user-agent&#13;
user-agent 也即浏览器标识，每个正常的web请求都包含用户的浏览器信息，除非经过伪装，恶意扫描工具一般都会在user-agent里留下某些特征字眼，比如scan，nmap等。我们可以用正则匹配这些字眼，从而达到过滤的目的，请根据需要调整。&#13;
```&#13;
if ($http_user_agent ~* "java|python|perl|ruby|curl|bash|echo|uname|base64|decode|md5sum|select|concat|httprequest|httpclient|nmap|scan" ) {&#13;
    return 403;&#13;
}&#13;
```&#13;
这里分析得不够细致，具体的非法user-agent还得慢慢从日志中逐个提取。&#13;
&#13;
##封杀特定的url&#13;
特定的文件扩展名，比如.bak&#13;
```&#13;
location ~* \.(bak|save|sh|sql|mdb|svn|git|old)$ {&#13;
    rewrite ^/(.*)$  $host  permanent;&#13;
}&#13;
```&#13;
知名程序,比如phpmyadmin&#13;
```&#13;
location /(admin|phpadmin|status)  {&#13;
    deny all;&#13;
}&#13;
```&#13;
##强制网站使用域名访问&#13;
可以逃过IP扫描，比如&#13;
```&#13;
if ( $host !~* 'abc.com' ) {&#13;
    return 403;&#13;
}&#13;
```&#13;
##url 参数过滤敏感字&#13;
```&#13;
if ($query_string ~* "union.*select.*\(") {&#13;
    rewrite ^/(.*)$  $host  permanent;&#13;
}&#13;
&#13;
if ($query_string ~* "concat.*\(") {&#13;
    rewrite ^/(.*)$  $host  permanent;&#13;
}&#13;
```&#13;
在头信息中定义安全参数&#13;
```&#13;
 # Security headers&#13;
    add_header X-Frame-Options "SAMEORIGIN" always;&#13;
    add_header X-XSS-Protection "1; mode=block" always;&#13;
    add_header X-Content-Type-Options "nosniff" always;&#13;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;&#13;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;&#13;
   add_header Content-Security-Policy "default-src 'self';  style-src  'unsafe-inline'  'self';  script-src  'unsafe-inline' 'self' *.bestyii.com analysis.bestyii.com *.baidu.com *.google-analytics.com;  img-src 'self' data: oss.bestyii.com *.baidu.com *.google-analytics.com;  connect-src  'self'  *.baidu.com ;"; #按需配置&#13;
&#13;
```&#13;
&gt; 本文由 [ez](https://www.bestyii.com/member/ez) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。&#13;
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>安全,nginx,yii2</tags><last_comment_time>1574849483</last_comment_time><last_comment_username></last_comment_username><view_count>8055</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574849483</created_at><updated_at>1575877885</updated_at></item><item><id>15</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>4</user_id><title>CentOS 7 下搭建LNMP运行环境(nginx,php,mariadb)</title><author></author><excerpt></excerpt><image></image><content># 环境搭建步骤&#13;
&#13;
## 初步步骤&#13;
#### 1.检查centos版本&#13;
执行命令&#13;
```&#13;
cat /etc/centos-release&#13;
```&#13;
执行结果&#13;
&#13;
```&#13;
# CentOS Linux release 7.6.1810 (Core)&#13;
```&#13;
#### 2. 设置时区&#13;
```&#13;
timedatectl list-timezones&#13;
sudo timedatectl set-timezone 'Asia/Shanghai'&#13;
```&#13;
date 命令查看时区&#13;
```&#13;
date&#13;
```&#13;
#### 3. 更新操作系统软件包&#13;
```&#13;
sudo yum update -y&#13;
```&#13;
若执行失败 可参考网站(https://mirrors.tuna.tsinghua.edu.cn/help/centos/)&#13;
&#13;
#### 4. 安装CentOS操作系统基本管理所需的一些基本软件包&#13;
&#13;
```&#13;
sudo yum install -y curl wget vim git unzip socat bash-completion epel-release&#13;
```&#13;
&#13;
## 第1步 - 安装PHP和必要的PHP扩展&#13;
&#13;
#### 1. 设置Webtatic YUM源：&#13;
```&#13;
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm&#13;
```&#13;
&#13;
#### 2. 安装PHP，以及必要的PHP扩展：&#13;
```&#13;
sudo yum install -y php72w php72w-cli php72w-fpm php72w-common php72w-mbstring php72w-zip php72w-mysql php72w-sqlite3 php72w-curl php72w-xml php72-gd  php72w-intl&#13;
```&#13;
要显示在模块中编译的PHP，您可以运行：&#13;
&#13;
```&#13;
php -m&#13;
&#13;
```&#13;
运行结果&#13;
&#13;
```&#13;
[PHP Modules]&#13;
bz2&#13;
calendar&#13;
Core&#13;
ctype&#13;
curl&#13;
date&#13;
dom&#13;
&#13;
```&#13;
检查PHP版本：&#13;
&#13;
```&#13;
php --version&#13;
```&#13;
运行结果&#13;
&#13;
```&#13;
# PHP 7.2.14 (cli) (built: Jan 12 2019 12:47:33) ( NTS )&#13;
# Copyright (c) 1997-2018 The PHP Group&#13;
# Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies&#13;
#     with Zend OPcache v7.2.14, Copyright (c) 1999-2018, by Zend Technologies&#13;
```&#13;
#### 3. 启动并启用PHP-FPM服务：&#13;
```&#13;
sudo systemctl start php-fpm.service&#13;
sudo systemctl enable php-fpm.service&#13;
```&#13;
继续下一步，即数据库安装和设置&#13;
&#13;
## 第2步 - 安装数据库和设置&#13;
#### 1.安装数据库服务器&#13;
```&#13;
sudo yum install -y mariadb-server&#13;
```&#13;
检查数据库版本&#13;
&#13;
```&#13;
mysql --version&#13;
```&#13;
运行结果&#13;
&#13;
```&#13;
# mysql  Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1&#13;
&#13;
```&#13;
如果是远程环境，不需要设置服务启动和密码(进行2,3步骤)&#13;
&#13;
#### 2.启动服务&#13;
&#13;
```&#13;
sudo systemctl start mariadb.service&#13;
sudo systemctl enable mariadb.service&#13;
```&#13;
#### 3. 数据库密码设置&#13;
&#13;
```&#13;
sudo mysql_secure_installation&#13;
```&#13;
回答每个问题：&#13;
&#13;
```&#13;
Would you like to setup VALIDATE PASSWORD plugin? N&#13;
New password: your_secure_password&#13;
Re-enter new password: your_secure_password&#13;
Remove anonymous users? [Y/n] Y&#13;
Disallow root login remotely? [Y/n] Y&#13;
Remove test database and access to it? [Y/n] Y&#13;
Reload privilege tables now? [Y/n] Y&#13;
```&#13;
以root用户身份连接到  mysql  shell：&#13;
&#13;
```&#13;
sudo mysql -u root -p&#13;
# Enter password&#13;
```&#13;
为Bolt CMS 创建一个空的  MariaDB 数据库和用户并记住凭据：&#13;
&#13;
```&#13;
MariaDB&gt; CREATE DATABASE dbname;&#13;
MariaDB&gt; GRANT ALL ON dbname.* TO 'username' IDENTIFIED BY 'password';&#13;
MariaDB&gt; FLUSH PRIVILEGES;&#13;
```&#13;
&#13;
退出  MariaDB：&#13;
&#13;
```&#13;
MariaDB&gt; exit&#13;
```&#13;
替换，并用您自己的名字。 dbname username  password &#13;
&#13;
&#13;
## 第三步 - 安装nginx&#13;
nginx 安装参考文档&#13;
http://nginx.org/en/linux_packages.html#RHEL-CentOS&#13;
#### 1. 安装 yum-utils&#13;
```&#13;
sudo yum install yum-utils&#13;
```&#13;
#### 创建一个文件， /etc/yum.repos.d/nginx.repo 使用如下内容&#13;
```&#13;
[nginx-stable]&#13;
name=nginx stable repo&#13;
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/&#13;
gpgcheck=1&#13;
enabled=1&#13;
gpgkey=https://nginx.org/keys/nginx_signing.key&#13;
&#13;
[nginx-mainline]&#13;
name=nginx mainline repo&#13;
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/&#13;
gpgcheck=1&#13;
enabled=0&#13;
gpgkey=https://nginx.org/keys/nginx_signing.key&#13;
```&#13;
安装nginx&#13;
```&#13;
sudo yum install nginx&#13;
```&#13;
&#13;
检查nginx的版本&#13;
&#13;
```&#13;
nginx -v&#13;
```&#13;
运行结果&#13;
&#13;
```&#13;
# nginx version: nginx/1.12.2&#13;
```&#13;
#### 2.启动并启用Nginx服务：&#13;
&#13;
```&#13;
sudo systemctl start nginx.service&#13;
sudo systemctl enable nginx.service&#13;
```&#13;
#### 3.通过运行以下命令 为Pagekit 配置  NGINX：&#13;
```&#13;
sudo vim /etc/nginx/conf.d/pagekit.conf&#13;
```&#13;
并使用以下配置填充文件：&#13;
```&#13;
server {&#13;
    listen [::]:443 ssl http2;&#13;
    listen 443 ssl http2;&#13;
    listen [::]:80;&#13;
    listen 80;&#13;
    # 配置自己的域名&#13;
    server_name example.com;&#13;
&#13;
    index index.php index.html;&#13;
    #配置项目目录&#13;
    root /var/www/pagekit;&#13;
&#13;
    ssl_certificate /etc/letsencrypt/example.com/fullchain.pem;&#13;
    ssl_certificate_key /etc/letsencrypt/example.com/private.key;&#13;
    ssl_certificate /etc/letsencrypt/example.com_ecc/fullchain.pem;&#13;
    ssl_certificate_key /etc/letsencrypt/example.com_ecc/private.key;&#13;
&#13;
    location / {&#13;
        try_files $uri $uri/ /index.php?$query_string;&#13;
    }&#13;
&#13;
    location ~ \.php$ {&#13;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;&#13;
        try_files $fastcgi_script_name =404;&#13;
        set $path_info $fastcgi_path_info;&#13;
        fastcgi_param PATH_INFO $path_info;&#13;
        fastcgi_index index.php; include fastcgi.conf;&#13;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;&#13;
        fastcgi_pass 127.0.0.1:9000;&#13;
    }&#13;
&#13;
}&#13;
```&#13;
如果是yii框架,参考网站 https://www.yiichina.com/doc/guide/2.0/start-installation&#13;
&#13;
检查  NGINX 配置是否存在语法错误：&#13;
&#13;
```&#13;
sudo nginx -t&#13;
```&#13;
#### 4.重新启动NGINX 服务：&#13;
&#13;
```&#13;
sudo systemctl reload nginx.service&#13;
```&#13;
&#13;
参考网站&#13;
https://www.howtoforge.com/how-to-install-pagekit-cms-on-centos-7/</content><tags>php,yum,nginx</tags><last_comment_time>1574924845</last_comment_time><last_comment_username></last_comment_username><view_count>4887</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1574924845</created_at><updated_at>1574929770</updated_at></item><item><id>16</id><site>bestyii</site><type>topic</type><post_meta_id>5</post_meta_id><user_id>3</user_id><title>CentOS 7 安装最新版NodeJS</title><author></author><excerpt></excerpt><image></image><content>1. 从官网下下载最新的nodejs&#13;
==&#13;
下载地址：https://nodejs.org/en/download/&#13;
&#13;
这里选择的是Linux Binaries (x64)&#13;
&#13;
![](https://statics.bestyii.com/rZH0Hj_Z4J6_aGmO.png)&#13;
&#13;
```&#13;
cd /usr/local/src&#13;
wget https://nodejs.org/dist/v12.13.1/node-v12.13.1-linux-x64.tar.xz&#13;
```&#13;
&#13;
2. 解压&#13;
==&#13;
```&#13;
tar -xvf node-v12.13.1-linux-x64.tar.xz&#13;
```&#13;
&#13;
3. 移动并改名文件夹&#13;
==&#13;
```&#13;
mv node-v12.13.1-linux-x64 ../nodejs&#13;
```&#13;
&#13;
4. 让npm和node命令全局生效&#13;
==&#13;
```&#13;
ln -s /usr/local/nodejs/bin/npm /usr/local/bin/&#13;
ln -s /usr/local/nodejs/bin/node /usr/local/bin/&#13;
```&#13;
&#13;
5. 查看nodejs是否安装成功&#13;
==&#13;
```&#13;
# node -v&#13;
v12.13.1&#13;
# npm -v&#13;
6.12.1&#13;
```&#13;
大功告成！</content><tags>centos7</tags><last_comment_time>1575122726</last_comment_time><last_comment_username></last_comment_username><view_count>9385</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1575122726</created_at><updated_at>1575123154</updated_at></item><item><id>17</id><site>bestyii</site><type>topic</type><post_meta_id>8</post_meta_id><user_id>3</user_id><title>Yii2下composer 安装本地开发Extensions(扩展包)</title><author></author><excerpt></excerpt><image></image><content>在开发当中，我们总是想复用代码，降低重复开发的工作。&#13;
在yii2开发的过程中，用composer管理依赖，在开发的时候，官方给出的办法是用`aliases`定义命名空间的。&#13;
&#13;
假设在 vendor/mycompany/myext 目录中安装了一个扩展，并且扩展类的命名空间为 myext ， 那么你可以在应用配置文件中包含如下代码：&#13;
```&#13;
[&#13;
    'aliases' =&gt; [&#13;
        '@myext' =&gt; '@vendor/mycompany/myext',&#13;
    ],&#13;
]&#13;
```&#13;
这种办法很有局限性，如果我们扩展本身也想引用依赖，就很麻烦，需要在项目中单独安装，并不是和这个扩展进行关联。&#13;
&#13;
使用Gii创建扩展的时候，默认生成了composer.json，那我们可以把需要的依赖定义到这个配置文件里。&#13;
&#13;
问题来了，这么定义如何载入并使这个配置生效。&#13;
其实很简单，我们在定义repositories（仓库）的时候会有多种模式（如：composer，git，artifact，path等）。&#13;
&#13;
我们只需要在项目的composer.json配置文件中增加对应本地扩展路径的定义即可&#13;
```&#13;
{&#13;
...&#13;
 "repositories": {&#13;
        "0": {&#13;
            "type": "composer",&#13;
            "url": "https://asset-packagist.org"&#13;
        },&#13;
        "local": {&#13;
            "type": "path",&#13;
            "url": "./app/extensions/*/*",&#13;
            "options": {&#13;
                "symlink": true&#13;
            }&#13;
        },&#13;
        "packagist": {&#13;
            "type": "composer",&#13;
            "url": "https://repo.huaweicloud.com/repository/php/"&#13;
        }&#13;
    }&#13;
}&#13;
```&#13;
&gt; 为了开发方便，我们安装的时候采用软连接的形式。避免开发的目录与实际运行的目录文件不能同步。&#13;
&#13;
安装需要的扩展&#13;
这里需要注意的是，安装的时候版本参数使用`dev-master`。&#13;
```&#13;
composer require  grazio/yii2-adminui:dev-master -vvv&#13;
```&#13;
&#13;
&#13;
&gt; 本文由 [systemofdown](https://www.bestyii.com/member/systemofdown) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。&#13;
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>composer,yii2</tags><last_comment_time>1575134438</last_comment_time><last_comment_username></last_comment_username><view_count>4357</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1575134438</created_at><updated_at>1576112590</updated_at></item><item><id>18</id><site>bestyii</site><type>topic</type><post_meta_id>8</post_meta_id><user_id>2</user_id><title>Nginx下Yii2程序开启http2及http2_push提速</title><author></author><excerpt></excerpt><image></image><content>网上有人说，网站用HTTP2推送接口请求极大的加快的网站访问速度。&#13;
其实也没有什么极大的提速，也就是5%左右。不过积少成多也得干。&#13;
&#13;
三步搞定&#13;
===&#13;
1. 配置http2&#13;
---&#13;
HTTP2 和 HTTPS&#13;
目前所有支持 HTTP/2 的浏览器都是基于 TLS 1.2 协议之上构建 HTTP/2 的，所以要使用 HTTP/2 `必须开启 HTTPS`.&#13;
&#13;
开启 HTTP2,只需要在 SSL 后面加上就能开启 HTTP2.&#13;
```&#13;
listen 443 ssl http2;&#13;
```&#13;
&#13;
2.安装扩展 yii2-http2-server-push&#13;
---&#13;
[yii2-http2-server-push](https://github.com/DevGroup-ru/yii2-http2-server-push "yii2-http2-server-push")，其功能是自动解析页面中可以被推送的文件，并把LINK信息加入到响应头中。&#13;
&#13;
```&#13;
php composer.phar require devgroup/yii2-http2-server-push&#13;
```&#13;
&#13;
3.nginx中开启http2_push_preload 指令&#13;
---&#13;
```&#13;
server{&#13;
    http2_push_preload on;&#13;
}&#13;
```&#13;
&#13;
用chrome确认过眼神，就是他没错的。&#13;
&#13;
![添加头信息](https://statics.bestyii.com/aJKxHkhRWb_WZvLF.png "添加头信息")&#13;
&#13;
![PUSH传输](https://statics.bestyii.com/T5LvNe6frDpZYg-v.png)&#13;
&#13;
百因必有果&#13;
====&#13;
原理很简单，减少请求次数，利用Push这个机制。&#13;
Nginx 1.13.9 就增加了 HTTP2_Push 支持。Nginx 开启 HTTP2 推送有两种方法。&#13;
&#13;
1.http2_push 指令&#13;
---&#13;
强制推送某 URL。&#13;
```&#13;
location / {&#13;
        index  index.html;&#13;
        http2_push /css/allinone.min.css;&#13;
    }&#13;
```&#13;
这个方法太硬了，我们需要灵活一点。所以这个方法不是今天的讨论重点。&#13;
2.http2_push_preload 指令&#13;
---&#13;
Nginx 会解析预加载 Link 头动态的推送某 URL。&#13;
```&#13;
server{&#13;
    http2_push_preload on;&#13;
    location / {&#13;
        index  index.html;&#13;
    }&#13;
}&#13;
```&#13;
&gt; 本文由 [ez](https://www.bestyii.com/member/ez) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。&#13;
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>nginx,yii2</tags><last_comment_time>1575342327</last_comment_time><last_comment_username></last_comment_username><view_count>4119</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1575342327</created_at><updated_at>1575342463</updated_at></item><item><id>19</id><site>bestyii</site><type>topic</type><post_meta_id>9</post_meta_id><user_id>1</user_id><title>Html5中图片利用img标签srcset属性做retina屏幕适配</title><author></author><excerpt></excerpt><image></image><content>`srcset`属性&#13;
===&#13;
下面是图片（img）元素，同时包含了src和srcset属性.&#13;
&#13;
当浏览器不支持`srcset`属性的时候`src`属性的值作为默认图片.&#13;
&#13;
标准分辨率的时候, srcset属性中， 1x 变量对应的图片将作为[1倍图片]. 当屏幕显示每个CSS像素使用2倍设备像素时, 2x v变量对应的图片将作为[2倍图片]. 同理, 这里还设置了3x 和 4x的图片.&#13;
&#13;
```html&#13;
&lt;img src="image-src.png" srcset="image-1x.png 1x, image-2x.png 2x, image-3x.png 3x, image-4x.png 4x"&gt;&#13;
```&#13;
参考文档：&#13;
1. [webkit: The srcset Attribute.](https://webkit.org/demos/srcset/)&#13;
2. [What Img Srcset Does In HTML5: A Quick &amp; Simple Guide](https://html.com/attributes/img-srcset/)</content><tags>html5,responsive</tags><last_comment_time>1575513892</last_comment_time><last_comment_username></last_comment_username><view_count>3911</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1575513892</created_at><updated_at>1575513908</updated_at></item><item><id>20</id><site>bestyii</site><type>topic</type><post_meta_id>8</post_meta_id><user_id>2</user_id><title>Yii2如何使用MinIO做为对象存储</title><author></author><excerpt></excerpt><image></image><content>MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口，非常适合于存储大容量非结构化的数据，例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等，而一个对象文件可以是任意大小，从几kb到最大5T不等。&#13;
&#13;
安装MinIO&#13;
==&#13;
详细步骤参考：[CentOS 7中使用Docker运行MinIO作为对象存储服务器](https://www.bestyii.com/topic/21 "CentOS 7中使用Docker运行MinIO作为对象存储服务器")&#13;
&#13;
安装好以后，我们要使用`亚马逊S3云存储服务接口`来调用这个服务。&#13;
&#13;
不得不说的是关于文件系统php有个一统天下的神器 [Flysystem](https://github.com/thephpleague/flysystem "Flysystem")，yii2也有其相应的扩展可以用。&#13;
&#13;
安装 Flysystem Extension for Yii 2&#13;
==&#13;
我们用的是[creocoder/yii2-flysystem](https://github.com/creocoder/yii2-flysystem)&#13;
&#13;
还是composer 安装，安装慢的时候别忘了改[composer国内源](https://www.bestyii.com/topic/10 "composer国内源")&#13;
&#13;
```&#13;
composer require creocoder/yii2-flysystem&#13;
```&#13;
&#13;
安装 AWS S3 filesystem 的扩展&#13;
==&#13;
还得装这么个扩展&#13;
```&#13;
composer require league/flysystem-aws-s3-v3&#13;
```&#13;
&#13;
配置Yii项目&#13;
==&#13;
```&#13;
return [&#13;
    //...&#13;
    'components' =&gt; [&#13;
        //...&#13;
       'fs' =&gt; [&#13;
            'class' =&gt; 'creocoder\flysystem\AwsS3Filesystem',&#13;
            'key' =&gt; 'keykeykeykeykeykeykeykey',&#13;
            'secret' =&gt; 'secretsecretsecretsecretsecret',&#13;
            'bucket' =&gt; 'bestyii',&#13;
            'region' =&gt; 'beijing',&#13;
            'version' =&gt; 'latest',&#13;
            // 'baseUrl' =&gt; 'your-base-url',&#13;
            //'prefix' =&gt; 'webapp',&#13;
            // 'options' =&gt; [],&#13;
            'endpoint' =&gt; 'http://oss.bestyii.com',&#13;
            'pathStyleEndpoint' =&gt; true&#13;
        ],&#13;
    ],&#13;
];&#13;
```&#13;
&#13;
如何使用&#13;
==&#13;
&#13;
写入文件&#13;
--&#13;
写入字符串到文件&#13;
```&#13;
Yii::$app-&gt;fs-&gt;write('filename.ext', 'contents');&#13;
```&#13;
&#13;
使用文档流方式写入文件&#13;
```&#13;
$stream = fopen('/path/to/somefile.ext', 'r+');&#13;
Yii::$app-&gt;fs-&gt;writeStream('filename.ext', $stream);&#13;
```&#13;
更新文件&#13;
--&#13;
To update file&#13;
```&#13;
Yii::$app-&gt;fs-&gt;update('filename.ext', 'contents');&#13;
```&#13;
To update file using stream contents&#13;
```&#13;
$stream = fopen('/path/to/somefile.ext', 'r+');&#13;
Yii::$app-&gt;fs-&gt;updateStream('filename.ext', $stream);&#13;
```&#13;
写入或更新&#13;
---&#13;
To write or update file&#13;
```&#13;
Yii::$app-&gt;fs-&gt;put('filename.ext', 'contents');&#13;
```&#13;
To write or update file using stream contents&#13;
```&#13;
$stream = fopen('/path/to/somefile.ext', 'r+');&#13;
Yii::$app-&gt;fs-&gt;putStream('filename.ext', $stream);&#13;
```&#13;
读取文件&#13;
--&#13;
To read file&#13;
```&#13;
$contents = Yii::$app-&gt;fs-&gt;read('filename.ext');&#13;
```&#13;
To retrieve a read-stream&#13;
```&#13;
$stream = Yii::$app-&gt;fs-&gt;readStream('filename.ext');&#13;
$contents = stream_get_contents($stream);&#13;
fclose($stream);&#13;
```&#13;
检查文件是否存在&#13;
--&#13;
To check if a file exists&#13;
```&#13;
$exists = Yii::$app-&gt;fs-&gt;has('filename.ext');&#13;
```&#13;
删除文件&#13;
--&#13;
To delete file&#13;
```&#13;
Yii::$app-&gt;fs-&gt;delete('filename.ext');&#13;
```&#13;
读取后并删除文件&#13;
--&#13;
To read and delete file&#13;
```&#13;
$contents = Yii::$app-&gt;fs-&gt;readAndDelete('filename.ext');&#13;
```&#13;
文件重命名&#13;
--&#13;
To rename file&#13;
```&#13;
Yii::$app-&gt;fs-&gt;rename('filename.ext', 'newname.ext');&#13;
```&#13;
&#13;
还有更多请参考文档吧 https://github.com/creocoder/yii2-flysystem&#13;
&#13;
&#13;
&gt; 本文由 [ez](https://www.bestyii.com/member/ez) 创作，采用 [知识共享署名 3.0 中国大陆许可协议](http://creativecommons.org/licenses/by/3.0/cn) 进行许可。&#13;
可自由转载、引用，但需署名作者且注明文章出处。</content><tags>对象存储,MinIO,Flysystem,yii2</tags><last_comment_time>1575879519</last_comment_time><last_comment_username></last_comment_username><view_count>4535</view_count><follow_count>0</follow_count><comment_count>0</comment_count><favorite_count>0</favorite_count><like_count>0</like_count><thanks_count>0</thanks_count><hate_count>0</hate_count><status>1</status><order>999</order><created_at>1575879519</created_at><updated_at>1581614178</updated_at></item></items><_links><self><href>https://www.miinno.com/?page=1</href></self><first><href>https://www.miinno.com/?page=1</href></first><last><href>https://www.miinno.com/?page=11</href></last><next><href>https://www.miinno.com/?page=2</href></next></_links><_meta><totalCount>201</totalCount><pageCount>11</pageCount><currentPage>1</currentPage><perPage>20</perPage></_meta></response>
