|
1 | | -## 高并发系统内核优化 |
2 | | -+ [Socket优化](#Socket) |
3 | | - + Nginx |
4 | | - + 系统内核 |
5 | | -+ [文件优化](#file) |
6 | | - + Nginx |
7 | | - + 系统内核 |
8 | | -+ [配置文件优化](#config-file) |
9 | | - + Nginx配置文件 |
10 | | - + 内核配置文件 |
11 | | - + PHP7配置文件 |
12 | | - + PHP-FPM配置文件 |
13 | | -### <a name="Socket"/> Socket优化 |
14 | | -#### Nginx |
15 | | -+ 子进程允许打开的连接数:`worker_connections` |
16 | | -#### 系统内核 |
17 | | -+ [内核参数的优化](http://blog.csdn.net/moxiaomomo/article/details/19442737) |
| 1 | +# 高并发系统内核优化 |
| 2 | +--- |
| 3 | +## [Socket优化](#Socket) |
| 4 | + |
| 5 | +* Nginx |
| 6 | + |
| 7 | +* 系统内核 |
| 8 | + |
| 9 | +## [文件优化](#file) |
| 10 | + |
| 11 | +* Nginx |
| 12 | + |
| 13 | +* 系统内核 |
| 14 | + |
| 15 | +## [配置文件优化](#config-file) |
| 16 | + |
| 17 | +* Nginx配置文件 |
| 18 | + |
| 19 | +* 内核配置文件 |
| 20 | + |
| 21 | +* PHP7配置文件 |
| 22 | + |
| 23 | +* PHP-FPM配置文件 |
| 24 | + |
| 25 | +# <a name="Socket"/> Socket优化 |
| 26 | +## Nginx |
| 27 | + |
| 28 | +#### 子进程允许打开的连接数: |
| 29 | + |
| 30 | +```bash |
| 31 | +worker_connections |
| 32 | +``` |
| 33 | +## 系统内核 |
| 34 | +#### [内核参数的优化](http://blog.csdn.net/moxiaomomo/article/details/19442737) |
| 35 | + |
18 | 36 | + 实践优化配置 |
19 | 37 | + 编辑: `vim /etc/sysctl.conf` |
20 | 38 | + 配置结果 |
|
34 | 52 | net.ipv4.tcp_keepalive_time = 30 |
35 | 53 | ``` |
36 | 54 | + 执行命令使之生效:`/sbin/sysctl -p` |
37 | | -### <a name="file"/> 文件优化 |
38 | | -#### Nginx |
39 | | -+ 指当一个nginx进程打开的最多文件描述符数目:`worker_rlimit_nofile 100000;` |
40 | | -#### 系统内核 |
| 55 | +# <a name="file"/> 文件优化 |
| 56 | + |
| 57 | +## Nginx |
| 58 | + |
| 59 | +* 指当一个nginx进程打开的最多文件描述符数目:`worker_rlimit_nofile 100000;` |
| 60 | + |
| 61 | +## 系统内核 |
| 62 | + |
41 | 63 | + 系统限制其最大进程数:`ulimit -n` |
| 64 | + |
42 | 65 | + 编辑文件:`/etc/security/limits.conf` |
43 | 66 |
|
44 | 67 | ```conf |
|
48 | 71 | * soft nofile 65535 |
49 | 72 | * hard nofile 65535 |
50 | 73 | ``` |
51 | | -### <a name="config-file"/> 配置文件优化 |
| 74 | +## <a name="config-file"/> 配置文件优化 |
| 75 | + |
52 | 76 | + Nginx配置文件 |
53 | 77 |
|
54 | 78 | ```lua |
|
215 | 239 | net.ipv4.tcp_keepalive_time = 30 |
216 | 240 | net.ipv4.ip_local_port_range = 1024 65000 |
217 | 241 | ``` |
218 | | -#### PHP.ini配置文件优化(PHP7) |
| 242 | +## PHP.ini配置文件优化(PHP7) |
| 243 | + |
219 | 244 | + 启用Zend Opcache,php.ini配置文件中加入 |
220 | 245 |
|
221 | 246 | ```bash |
|
248 | 273 | │ └── vendor |
249 | 274 | ``` |
250 | 275 | + 使用新的编译器,使用新一点的编译器, 推荐GCC 4.8以上, 因为只有GCC 4.8以上PHP才会开启Global Register for opline and execute_data支持, 这个会带来5%左右的性能提升 |
| 276 | + |
251 | 277 | + 开启HugePages,然后开启Opcache的huge_code_pages |
| 278 | + |
252 | 279 | + 系统中开启HugePages |
253 | 280 |
|
254 | 281 | ```bash |
|
266 | 293 | Hugepagesize: 2048 kB |
267 | 294 | ``` |
268 | 295 | + 然后在php.ini中加入,`opcache.huge_code_pages=1` |
269 | | -+ 开启Opcache File Cache,`opcache.file_cache=/tmp` |
270 | | -+ 启用Zend Opcache |
271 | | -#### PHP-FPM优化 |
| 296 | + |
| 297 | + + 开启Opcache File Cache,`opcache.file_cache=/tmp` |
| 298 | + |
| 299 | + + 启用Zend Opcache |
| 300 | + + |
| 301 | +## PHP-FPM优化 |
| 302 | + |
272 | 303 | + 结构 |
273 | 304 |
|
274 | 305 | ```bash |
|
378 | 409 | ``` |
379 | 410 |
|
380 | 411 | + 以下命令将帮助我们确定每个(PHP-FPM)子进程使用的内存: |
381 | | - > RSS列显示PHP-FPM进程的未交换的物理内存使用量,单位为千字节 |
| 412 | + |
| 413 | + > RSS列显示PHP-FPM进程的未交换的物理内存使用量,单位为千字节 |
382 | 414 | > 平均每个PHP-FPM进程在我的机器上占用大约75MB的RAM |
383 | 415 |
|
384 | 416 | ```php |
|
392 | 424 | ``` |
393 | 425 |
|
394 | 426 | + 在我的情况下是56MB,服务器有16GB的RAM,所以: |
| 427 | + |
395 | 428 | >我留下了一些记忆,让系统呼吸。在计算内存使用情况时,您需要考虑计算机上运行的任何其他服务。 |
396 | 429 |
|
397 | 430 | ```php |
|
417 | 450 | ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }' |
418 | 451 | ``` |
419 | 452 |
|
420 | | -+ HELP |
421 | | - + [php-fpm - 启动参数及重要配置详解](http://www.4wei.cn/archives/1002061) |
422 | | - + [php-fpm backlog参数潜在问题](http://blog.csdn.net/willas/article/details/11634825) |
423 | | - + [Adjusting child processes for PHP-FPM (Nginx)](https://myshell.co.uk/blog/2012/07/adjusting-child-processes-for-php-fpm-nginx/) |
424 | | - + [Nginx的worker_processes优化](http://blog.chinaunix.net/uid-26000296-id-3987521.html) |
| 453 | +## HELP |
| 454 | + |
| 455 | ++ [php-fpm - 启动参数及重要配置详解](http://www.4wei.cn/archives/1002061) |
| 456 | ++ [php-fpm backlog参数潜在问题](http://blog.csdn.net/willas/article/details/11634825) |
| 457 | ++ [Adjusting child processes for PHP-FPM (Nginx)](https://myshell.co.uk/blog/2012/07/adjusting-child-processes-for-php-fpm-nginx/) |
| 458 | + |
| 459 | ++ [Nginx的worker_processes优化](http://blog.chinaunix.net/uid-26000296-id-3987521.html) |
425 | 460 |
|
0 commit comments