# PHP优化基础
PHP优化基础书:
https://www.sitepoint.com/premium/books/performance-project
## 1,用Blackfire监控PHP性能问题
PHP-level Performance Optimization with Blackfire:
红色排前面的就是性能问题所在:
![](https://img.kancloud.cn/b3/0c/b30c93187aa791cdc41531b08731dbb9_400x500.png)
*****
## 2,检查Mysql性能
MySQL Performance Boosting with Indexes and Explain:
### show profiles看执行情况:
As we can see, the `show profiles;` command gives us times not only for the original query but also for all the other queries that are made. This way we can accurately profile our queries.
![](https://img.kancloud.cn/76/5c/765c0f6b0472f76b59e8522137e0f50b_903x757.png)
### explain慢查询:
![](https://img.kancloud.cn/2c/b7/2cb7d43ca6ad957c2e4a0cab875bd193_1344x688.png)
*****
*****
## 3,用GTmetrix工具检查存在问题
Improving Performance Perception with Pingdom and GTmetrix:
例如:
### On-demand Image Resizing 图片加载方面:用srcset
![](https://img.kancloud.cn/94/8e/948efe9e8780b2cbd04ca4cb980330db_1024x148.png)
*****
## 4,后台任务异步化
Using Background Processing to Speed Up Page Load Times
![](https://img.kancloud.cn/cb/db/cbdb533855f301d5242e26d3f4a76fb8_1024x397.png)
We're using the [Beanstalkd](http://kr.github.io/beanstalkd/) job queue to store jobs, the Symfony Console component to implement workers as console commands and [Supervisor](http://supervisord.org/) to take care of worker processes.
![](https://img.kancloud.cn/0a/75/0a75570a99c1ca5859062adbb21fd130_989x311.png)
异步的另一个好处:多任务多worker并行处理:
The best part about background processing is the ease of parallel processing. We can set a `numprocs=5` directive and Supervisor will spawn five instances of our workers.
### 利用supervisor:
![](https://img.kancloud.cn/8c/df/8cdf0455a1da1d3986942ec0adca33cf_790x266.png)
*****
## 6,Nginx 和 PHP-FPM方面的优化
Server-side Optimization with Nginx and pm-static
php-fpm :`dynamic`, `ondemand` and `static`, 尝试 使用:**static**
https://www.sitepoint.com/server-side-optimization-with-nginx-and-pm-static/
tried the `pm = static` setting, allowing our PHP processes to take over the maximum of the server's resources, short of swapping, or driving the CPU to a halt. This setting means we're forcing the maximum out of our system at all times. It also means that — within our server's constraints — there won't be any spawning overhead time cost.
*****
## 7,页面静态化
Nginx and **fastcgi Caching** 动态php变成页面静态化
![](https://img.kancloud.cn/91/84/918432147437d9ed0aa54b4613d72da2_1115x521.png)
![](https://img.kancloud.cn/ca/d8/cad81e8039e42cbfbd27cdcd9cbc7711_1013x978.png)
同时也可以使用**Cloudflare** 和 **varnish**。。
### 有没有其他方案?
Cloudflare APO,[How to Set up Cloudflare APO for WordPress (kinsta.com)](https://kinsta.com/blog/cloudflare-apo-wordpress/)