ubuntu清理磁盘空间

  1. df -h:显示磁盘占用

  2. apt-get autoclean:清理旧版本缓存

  3. apt-get clean:清理所有缓存

  4. apt-get autoremove:移除孤立软件(可以清理出500M+的空间)

  5. rm -rf /var/log/*:删除所有log(删了后last命令提示找不到一个/var/log下的文件。。。)

  6. find / -size +100M:列出所有大于100M的文件运行一遍后,把快照大小从3G+降到了2.5G,但相比我刚装好所有软件时的2.1G的快照还是大了蛮多

How to Read the PHP Slow Request Log

The PHP slow request log is where PHP records information about any request that takes more than five seconds to execute.

Having slow PHP code not only creates a bad experience for your site’s visitors, but it is also expensive for you. An app with fast PHP code only needs a single PHP process to handle many requests each second. However, since each PHP process can only execute one request at a time, when a PHP script is slow, the app needs additional PHP processes to handle the requests. Each additional process uses more memory, so having slow code means your server needs a lot more memory.

If your server has high memory usage or requests that are timing out, you may have slow requests that you can debug by reading the PHP slow request log.

Location of the PHP Slow Request Log

On the ServerPilot Business plan, you can view the PHP slow request log from each app’s Logs tab in ServerPilot.

If you’re not on the Business plan, you can SSH or SFTP into your server to view the PHP slow request log.

Each app has its own PHP slow request log. The log file is located under the app’s system user’s home directory at:

log/APPNAME/APPNAME_phpX.Y.slow.log

where phpX.Y is the PHP version the app is using, for example php7.0.

Understanding the PHP Slow Request Log

The PHP slow request log records stack traces (also known as tracebacks) of each slowly executing script at the moment the request passed five seconds in execution.

For example, an entry in the slow request log might look like this:

[06-Oct-2015 12:20:34]  [pool example] pid 1852
script_filename = /srv/users/serverpilot/apps/example/public/index.php
[0x00007f7e475ba138] curl_exec() /srv/users/serverpilot/apps/example/public/wp-content/plugins/wp-piwik/classes/WP_Piwik/Request/Rest.php:44
[0x00007f7e475b9f10] curl() /srv/users/serverpilot/apps/example/public/wp-content/plugins/wp-piwik/classes/WP_Piwik/Request/Rest.php:20
[0x00007f7e475b9d48] request() /srv/users/serverpilot/apps/example/public/wp-content/plugins/wp-piwik/classes/WP_Piwik/Request.php:63
[0x00007f7e475b9be0] perform() /srv/users/serverpilot/apps/example/public/wp-content/plugins/wp-piwik/classes/WP_Piwik.php:941
[0x00007f7e475b99f8] request() /srv/users/serverpilot/apps/example/public/wp-content/plugins/wp-piwik/classes/WP_Piwik.php:1098
[0x00007f7e475b98b8] updateTrackingCode() /srv/users/serverpilot/apps/example/public/wp-content/plugins/wp-piwik/classes/WP_Piwik/TrackingCode.php:16
[0x00007f7e475b9720] __construct() /srv/users/serverpilot/apps/example/public/wp-content/plugins/wp-piwik/classes/WP_Piwik.php:288
[0x00007fffa8cdcac0] addJavascriptCode() unknown:0
[0x00007f7e475b9540] call_user_func_array() /srv/users/serverpilot/apps/example/public/wp-includes/plugin.php:503
[0x00007f7e475b9428] do_action() /srv/users/serverpilot/apps/example/public/wp-includes/general-template.php:2284
[0x00007f7e475b9358] wp_footer() /srv/users/serverpilot/apps/example/public/wp-content/themes/k2/footer.php:17
[0x00007f7e475b9358] +++ dump failed

The first two lines tell you the time of the slow request and which script was initially requested. In the above example, these first two lines are:

[06-Oct-2015 12:20:34]  [pool example] pid 1852
script_filename = /srv/users/serverpilot/apps/example/public/index.php

The third line (the line immediately below the first two lines) is the top of the stack trace. This tells you the currently executing function at the time the script passed five seconds in execution. In the example above, the top of the stack trace is:

[0x00007f7e475ba138] curl_exec() /srv/users/serverpilot/apps/example/public/wp-content/plugins/wp-piwik/classes/WP_Piwik/Request/Rest.php:44

The three parts of this line are a unique identifier for the request ([0x00007f7e475ba138]), the name of the executing function (curl_exec()), and the file name and line number of that function (/srv/users/.../Request/Rest.php:44).

The top of the stack trace is generally the cause of your script’s slowness. The rest of the lines below that tell you each function call that led up to that point.

Note that you will sometimes see +++ dump failed at the bottom of a stack trace. That indicates the end of the stack trace and does not indicate a problem.

If you see a WordPress plugin name in the slow request log, you may be able to resolve your app’s slow request problems by disabling the plugin.

Common Causes of Slow Requests

There are a few common causes of slow requests.

Slow Requests Due to curl_exec()

If you see the curl_exec() function in your PHP slow request log, the problem is not a bug in cURL but rather that the request is taking a long time to complete.

When your scripts rely on making requests to external services and other websites, your own site can become slow and unreliable due to problems with the other website. For example, if their website is slow or is rate-limiting your requests, your own app will become very slow. These issues can be resolved by disabling plugins that make external requests and not writing your own PHP code to perform HTTP requests to external websites or services.

Slow Requests Due to mysqli_query()

If you see the mysqli_query() function in your PHP slow request log, the problem is not a bug in MySQL or any MySQL settings but rather that your PHP code is executing slow SQL queries.

You can have slow SQL queries due to, for example, lack of proper indexes in your database tables or PHP code that is inefficiently using the databases. The solution in this case is to track down the particular queries that are slow and fix them.

Acceptable Cases of Slow Requests

There is one acceptable case of slow requests where you can safely ignore entries in the PHP slow request log: slow requests that are not being performed when answering a website visitor’s request but rather are scheduled requests being performed in the background. Common example of such requests are requests for wp-cron.php or any requests you may have scheduled using cron.

pureComponent vs Component vs Stateless Functional component的区别

1,createClass,没有使用es6语法的时候,定义组件,就使用React.createClass来定义组件

var React = require("react");
var Greeting = React.createClass({
  
  propTypes: {
    name: React.PropTypes.string //属性校验
  },

  getDefaultProps: function() {
    return {
      name: 'Mary' //默认属性值
    };
  },
  
  getInitialState: function() {
    return {count: this.props.initialCount}; //初始化state
  },
  
  handleClick: function() {
    //用户点击事件的处理函数
  },

  render: function() {
    return <h1>Hello, {this.props.name}</h1>;
  }
});
module.exports = Greeting;

在createClass中,React对属性中的所有函数都进行了this绑定,也就是上面的handleClick其实相当于handleClick.bind(this);

2, component,因为es6对类和继承有语法级别的支持,所以用es6创建组件的方式更为优雅

import React from 'react';
class Greeting extends React.Component {

  constructor(props) {
    super(props); //调用父类的构造函数
    this.state = {count: props.initialCount};
    this.handleClick = this.handleClick.bind(this);
  }
  
  //static defaultProps = {
  //  name: 'Mary'  //定义defaultprops的另一种方式
  //}
  
  //static propTypes = {
    //name: React.PropTypes.string
  //}
  
  handleClick() {
    //点击事件的处理函数
  }
  
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

Greeting.propTypes = {
  name: React.PropTypes.string
};

Greeting.defaultProps = {
  name: 'Mary'
};
export default Greating;

3,pureComponent
我们知道,当组件的props或者state发生变化时,React就会对当前的props和state分别与nextProps和nextState进行比较,当发生变化时,就会对当前组件或子组件进行重新渲染,否则就不渲染,有时候为了避免组件进行不必要的重新渲染,我们通过shouldComponentUpdate来优化性能

class CounterButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = {count: 1};
  }

  shouldComponentUpdate(nextProps, nextState) {
    if (this.props.color !== nextProps.color) {
      return true;
    }
    if (this.state.count !== nextState.count) {
      return true;
    }
    return false;
  }

  render() {
    return (
      <button
        color={this.props.color}
        onClick={() => this.setState(state => ({count: state.count + 1}))}>
        Count: {this.state.count}
      </button>
    );
  }
}

shouldComponentUpdate通过判断props.color和state.count是否发生变化来重新渲染组件,React提供了PureComponent来自动帮我们做这件事,

class CounterButton extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {count: 1};
  }

  render() {
    return (
      <button
        color={this.props.color}
        onClick={() => this.setState(state => ({count: state.count + 1}))}>
        Count: {this.state.count}
      </button>
    );
  }
}

4,Stateless Functional Component
上面提到的是用来创建包含状态和用户交互的复杂组件,当组件本身只是用来展示,所有数据都是通过props传入的时候,我们便可以使用Stateless Functional Component来快速创建组件

import React from 'react';
const Button = ({
  day,
  increment
}) => {
  return (
    <div>
      <button onClick={increment}>Today is {day}</button>
    </div>
  )
}

Button.propTypes = {
  day: PropTypes.string.isRequired,
  increment: PropTypes.func.isRequired,
}

选哪个
createClass, 除非你确实对ES6的语法一窍不通,不然的话就不要再使用这种方式定义组件。

Stateless Functional Component, 对于不需要内部状态,且用不到生命周期函数的组件,我们可以使用这种方式定义组件,比如展示性的列表组件,可以将列表项定义为Stateless Functional Component。

PureComponent/Component,对于拥有内部state,使用生命周期的函数的组件,我们可以使用二者之一,但是大部分情况下,我更推荐使用PureComponent,因为它提供了更好的性能,同时强制你使用不可变的对象,保持良好的编程习惯。