8244 发表于 2018-10-1 07:56:34

《Nginx1.13+MySQL 5.7+PHP7.2 优化》

  文档作者:清风
  文档版本:Version 1.0
  修改记录:2018-04-13
  系统环境:CentOS 6.5 64 bit
  Mysql:mysql-5.7.3
  PHP:php-7.2.4
  Nginx:nginx-1.13.12
  网站平台:wordpress-4.9.1
  配置过程
  LNMP&MySQL MASTER 主机配置

[*]配置主机名&关闭 Selinux 与 iptables 防火墙:  # hostname master
  # bash
  # echo "192.168.200.135    master" >>/etc/hosts
  # vim /etc/sysconfig/network
  NETWORKING=yes
  # /etc/init.d/iptables stop
  iptables:将链设置为政策 ACCEPT:filter                  [确定]
  iptables:清除防火墙规则:                                 [确定]
  iptables:正在卸载模块:                                 [确定]
  # setenforce 0
  setenforce: SELinux is disabled

[*]  配置 YUM 仓库:
  # cd /etc/yum.repos.d/
  # ls
  bakCentOS-Base.repoCentOS-Media.repoepel.repoepel-testing.repo
  # mv o bak/
  # mv bak/M ./
  # vim CentOS-Media.repo
  
  name=CentOS- - Media
  baseurl=file:///media/cdrom/
  gpgcheck=1
  enabled=1
  gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
  # mount /dev/sr0 /media/cdrom/
  mount: block device /dev/sr0 is write-protected, mounting read-only
  编译及安装 Nginx
  1.安装依赖软件并卸载 rpm 格式软件包
  # yum install -y pcre-devel
  # rpm -qa httpd mysql mysql-server php
  mysql-5.1.71-1.el6.x86_64
  httpd-2.2.15-29.el6.centos.x86_64
  # rpm -e mysql-5.1.71-1.el6.x86_64 --nodeps
  # rpm -e httpd-2.2.15-29.el6.centos.x86_64 --nodeps
  2.创建程序用户和程序组
  # useradd -M -s /sbin/nologin nginx
  3.编译安装 Nginx
  # tar xf nginx-1.13.12.tar.gz -C /usr/local/
  # cd /usr/local/nginx-1.13.12/
  # ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
  # ln -s /usr/local/nginx/sbin/ /usr/local/bin/
  4.Nginx 的运行控制
  a.检查配置文件
  # nginx -t
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  b.启动、停止 Nginx
  # nginx
  # netstat -anpt|grep nginx
  tcp      0      0 0.0.0.0:80                  0.0.0.0:                   LISTEN      4493/nginx   
  c.编写 nginx 服务器脚本
  # vim /etc/init.d/nginx
  #!/bin/bash
  #chkconfig: 235 99 20
  #desciption:Nginx server start script
  Nginx="/usr/local/nginx/sbin/nginx"
  Pid="/usr/local/nginx/logs/nginx.pid"
  . /etc/init.d/functions
  start(){
  [ ! -f $Pid ] && $Nginx && action "Nginx is OK" /bin/true || action "Nginx is running" /bin/false
  }
  stop(){
  [ ! -f $Pid ] && action "Nginx not running" /bin/false || kill $(cat "$Pid" 2>/dev/null) 2>/dev/null && action "Nginx is stoping" /bin/true
  }
  status(){
  [ -f $Pid ] && echo "Nginx is running" || echo "Nginx not running"
  }
  case $1 in
  start)
  start
  ;;
  stop)
  stop
  ;;
  status)
  status
  ;;
  restart)
  stop
  sleep 3
  start
  ;;
  reload)
  kill -1 $(cat "$Pid")
  ;;
  )
  echo "USAGE:$0 {start|stop|reload|status|restart}"
  ;;
  esac
  # chmod +X /etc/init.d/nginx
  # chkconfig --add nginx
  # chkconfig --list nginx
  nginx         0:关闭    1:关闭    2:启用    3:启用    4:关闭    5:启用    6:关闭
  5.配置文件 nginx.conf
  # cd /usr/local/nginx/conf/
  # vim nginx.conf
  events {
  worker_connections1024;
  use epoll;   #添加这行
  }
  #去掉下面几行注释:
  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  access_loglogs/access.logmain;
  # nginx -t
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  # /etc/init.d/nginx restart
  Nginx is stoping                                           [确定]
  Nginx is OK                                                [确定]
  6.nginx 的访问测试
  LEMP 架构及应用
  构建 LEMP 网站平台—MySql 数据库

[*]安装 MySQL 数据库  # tar xf cmake-2.8.6.tar.gz -C /usr/local/
  # tar xf mysql-5.7.3-m13.tar.gz -C /usr/local/
  # cd /usr/local/cmake-2.8.6/
  # ./configure && gmake && gmake install
  # cd ../mysql-5.7.3-m13/
  # cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all && make && make install
[*]安装后的调整和优化  # cp -f support-files/my-default.cnf /etc/my.cnf
  cp:是否覆盖"/etc/my.cnf"? y
  # cp -f support-files/mysql.server /etc/init.d/mysqld
  # chmod +x /etc/init.d/mysqld
  # chkconfig --add mysqld
  # chkconfig --list mysqld
  mysqld          0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
  # ln -s /usr/local/mysql/bin/* /usr/local/bin/
[*]初始化数据库  # useradd -M -s /sbin/nologin mysql
  # chown -R mysql.mysql /usr/local/mysql
  # /usr/local/mysql/scripts/mysql_install_db --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --user=mysql
  4.启动 Mysql 服务
  # /etc/init.d/mysqld start
  Starting MySQL..                                           [确定]
  # netstat -anpt|grep mysqld
  tcp      0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      73025/mysqld
  Mysql 主从同步
[*]安装时间同步 ntp 服务  MASTER:
  # rpm -qa ntp
  ntp-4.2.6p5-12.el6.centos.2.x86_64
  # vim /etc/ntp.conf
  #添加下面两行内容:
  server 127.127.1.0
  fudge 127.127.1.0 startum 8
  # /etc/init.d/ntpd start
  正在启动 ntpd:                                          [确定]
[*]修改主服务器配置文件:/etc/my.cnf  # vim /etc/my.cnf
  
  server_id = 1          #这行注释去掉,默认没有值,给它添加1
  log-bin=master-bin   #添加这行
  log-slave-updates=true#添加这行
  # /etc/init.d/mysqld restart
  Shutting down MySQL..                                    [确定]
  Starting MySQL.                                          [确定]
[*]登录 master 给 slave 服务器授权  # mysqladmin -u root password "123456"
  # mysql -u root -p
  Enter password:

  mysql> grant replication slave on . to 'myslave'@'192.168.200.%'>  Query OK, 0 rows affected (0.00 sec)
  mysql> flush privileges;
  Query OK, 0 rows affected (0.05 sec)
  mysql> show master status;
  +-------------------+----------+--------------+------------------+-------------------+
  | File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  +-------------------+----------+--------------+------------------+-------------------+
  | master-bin.000001 |      581 |            |                  |                   |
  +-------------------+----------+--------------+------------------+-------------------+
  1 row in set (0.00 sec)
  搭建 MySQL 从服务器

[*]MySQL Slave 主机安装前的准备  # vim /etc/sysconfig/network
  # /etc/init.d/iptables stop
  iptables:将链设置为政策 ACCEPT:filter                  [确定]
  iptables:清除防火墙规则:                                 [确定]
  iptables:正在卸载模块:                                 [确定]
  # setenforce 0
  setenforce: SELinux is disabled
  # hostname slave
  # bash
  # vim /etc/sysconfig/network
  NETWORKING=yes
  HOSTNAME=slave
[*]安装 MySQL 数据库  # rpm -qa mysql
  mysql-5.1.71-1.el6.x86_64
  # rpm -e mysql-5.1.71-1.el6.x86_64 –nodeps
  # tar xf cmake-2.8.6.tar.gz -C /usr/local/
  # tar xf mysql-5.7.3-m13.tar.gz -C /usr/local/
  # cd /usr/local/cmake-2.8.6/
  # ./configure && gmake && gmake install
  # cd ../mysql-5.7.3-m13/
  # cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc/ -DDEFAULT_CHARSET=utf8 -DEFAULT_COLLATION=utf8_general_ci -DWITH_EXTTRA_CHARSETS=all && make && make install
[*]安装后的调整和优化  # cp -f support-files/my-default.cnf /etc/my.cnf
  cp:是否覆盖"/etc/my.cnf"? y
  # cp support-files/mysql.server /etc/init.d/mysqld
  # chmod +x /etc/init.d/mysqld
  # chkconfig --add mysqld
  # chkconfig --list mysqld
  mysqld          0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
  # ln -s /usr/local/mysql/bin/* /usr/local/bin/
[*]初始化数据库  # useradd -M -s /sbin/nologin mysql
  # chown -R mysql.mysql /usr/local/mysql
  # /usr/local/mysql/scripts/mysql_install_db --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --user=mysql
  5.启动 Mysql 服务
  # /etc/init.d/mysqld start
  Starting MySQL..                                           [确定]
  # netstat -anpt|grep mysqld
  tcp      0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      34802/mysqld
  # mysqladmin-uroot password "123456"
[*]与主服务器同步时间  # rpm -q ntpdate
  ntpdate-4.2.6p5-12.el6.centos.2.x86_64
  # ntpdate 192.168.200.135
  14 Apr 09:23:07 ntpdate: adjust time server 192.168.200.135 offset 0.011442 sec
[*]配置从服务器 SLAVE 修改配置文件  # vim /etc/my.cnf
  
  server_id = 2                        #这行注释去掉,ID号唯一性,不和主相同就行
  relay-log=relay-log-bin               #添加这行
  relay-log-index=slave-relay-bin.index   #添加这行
  # /etc/init.d/mysqld restart
  Shutting down MySQL..                                    [确定]
  Starting MySQL.                                          [确定]
  # mysql -uroot -p123456
  mysql> change master to master_host='192.168.200.135',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=581;
  Query OK, 0 rows affected, 2 warnings (0.22 sec)
  mysql> start slave;
  Query OK, 0 rows affected (0.06 sec)
  mysql> show slave status\G
   1. row
  Slave_IO_State: Waiting for master to send event
  Master_Host: 192.168.200.135
  Master_User: myslave
  Master_Port: 3306
  Connect_Retry: 60
  Master_Log_File: master-bin.000001
  Read_Master_Log_Pos: 581

  Relay_Log_File:>  Relay_Log_Pos: 284
  Relay_Master_Log_File: master-bin.000001
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  Replicate_Do_DB:
  Replicate_Ignore_DB:
  Replicate_Do_Table:
  Replicate_Ignore_Table:
  Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
  Last_Errno: 0
  Last_Error:
  Skip_Counter: 0
  Exec_Master_Log_Pos: 581
  Relay_Log_Space: 455
  Until_Condition: None
  Until_Log_File:
  Until_Log_Pos: 0
  Master_SSL_Allowed: No
  Master_SSL_CA_File:
  Master_SSL_CA_Path:
  Master_SSL_Cert:
  Master_SSL_Cipher:
  Master_SSL_Key:
  Seconds_Behind_Master: 0
  Master_SSL_Verify_Server_Cert: No
  Last_IO_Errno: 0
  Last_IO_Error:
  Last_SQL_Errno: 0
  Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
  Master_Server_Id: 1
  Master_UUID: cfc819f2-3f79-11e8-bb70-000c29eda91d
  Master_Info_File: /usr/local/mysql/data/master.info
  SQL_Delay: 0
  SQL_Remaining_Delay: NULL

  Slave_SQL_Running_State: Slave has read all>  Master_Retry_Count: 86400
  Master_Bind:
  Last_IO_Error_Timestamp:
  Last_SQL_Error_Timestamp:
  Master_SSL_Crl:
  Master_SSL_Crlpath:
  Retrieved_Gtid_Set:
  Executed_Gtid_Set:
  Auto_Position: 0
  Replicate_Rewrite_DB:
  1 row in set (0.00 sec)

[*]验证主从复制效果:  在 MASTER 上创建数据库 crushlinux;
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | mysql            |
  | performance_schema |
  | test               |
  +--------------------+
  4 rows in set (0.01 sec)
  mysql> create database crushlinux;
  Query OK, 1 row affected (0.04 sec)
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | crushlinux         |
  | mysql            |
  | performance_schema |
  | test               |
  +--------------------+
  5 rows in set (0.00 sec)
  在 SLVAE 上都看到 crushlinux 的数据库表明同步成功
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | mysql            |
  | performance_schema |
  | test               |
  +--------------------+
  4 rows in set (0.05 sec)
  mysql> show databases;
  +--------------------+
  | Database         |
  +--------------------+
  | information_schema |
  | crushlinux         |
  | mysql            |
  | performance_schema |
  | test               |
  +--------------------+
  5 rows in set (0.00 sec)
  如果数据库中有大量的旧数据,可先将主数据库数据备份并传给从数据库导入:
  # mysqldump -uroot -p123456 --opt--all-databases >all-data.sql
  从数据库导入
  # mysql -uroot -p123456 testdrop database crushlinux;
  Query OK, 0 rows affected (0.00 sec)
  构建 LEMP 网站平台—安装 PHP 解析环境
[*]编译安装 PHP  # cd
  # tar xf php-7.2.4.tar.gz -C /usr/local/
  # cd /usr/local/php-7.2.4/
  # ./configure --prefix=/usr/local/php7 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php7 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib && make && make install
[*]PHP 安装后的调整  # ln -s /usr/local/php7/bin/ /usr/local/bin/
  # ln -s /usr/local/php7/sbin/ /usr/local/bin/
  # cp -f php.ini-development /usr/local/php7/php.ini
[*]安装 ZendGuardLoader  # tar xf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
  # cd ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/
  # cp ZendGuardLoader.so /usr/local/php7/lib/php/
  # vim /usr/local/php7/php.ini
  #追加下面两行:
  zend_extension=/usr/local/php7/lib/php/ZendGuardLoader.so
  zend_loader.enable=1
[*]启用 PHP-FPM 进程,配置 Nginx 支持 PHP  # cd /usr/local/php7/etc/
  # cp php-fpm.conf.default php-fpm.conf
  # vim php-fpm.conf
  
  pid = run/php-fpm.pid                #这行去掉注释
  include=/usr/local/php7/etc/php-fpm.d/*.conf #这行的文件才是正真配置文件
  # cd php-fpm.d/
  # ls
  www.conf.default
  # cp www.conf.default www.conf
  # vim www.conf
  
  user = nginx                  #默认是nobody
  group = nginx                #默认是nobody
  listen = 127.0.0.1:9000
  pm = dynamic
  pm.max_children = 50         #默认是5
  pm.start_servers = 20          #默认是2
  pm.min_spare_servers = 5      #默认是1
  pm.max_spare_servers = 35   #默认是3
  # /usr/local/php7/sbin/php-fpm
  Failed loading /usr/local/php7/lib/php/ZendGuardLoader.so:/usr/local/php7/lib/php/ZendGuardLoader.so: undefined symbol: zval_used_for_init
  # ls /usr/local/php7/lib/php/
  ArchiveConsoledoc         OS    pearcmd.phppeclcmd.phpSystem.phpXML
  build    data   extensionsPEARPEAR.php   Structures   test      ZendGuardLoader.so
  # netstat -anpt|grep php-fpm
  tcp      0      0 127.0.0.1:9000            0.0.0.0:*                   LISTEN      55633/php-fpm

[*]配置 Nginx 支持 PHP 解析  # vim /usr/local/nginx/conf/nginx.conf
  server {
  listen       80;
  server_namelocalhost;
  location / {
  root   html;
  index index.php index.html index.htm;    #这行添加index.php
  }
  error_page   500 502 503 504/50x.html;
  location = /50x.html {
  root   html;
  }
  #下面这几行去掉注释
  location ~ .php$ {
  root         html;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_indexindex.php;
  include      fastcgi.conf;   #这行修改
  }
  }
  }
  # /etc/init.d/nginx restart
  Nginx is stoping                                           [确定]
  Nginx is OK                                                [确定]
  7.安装mysqli.so扩展模块
  # cd /usr/local/php-7.2.4/ext/mysqli/
  # /usr/local/php7/bin/phpize
  # ./configure --with-php-config=/usr/local/php7/bin/php-config--with-mysqli=/usr/local/mysql/bin/mysql_config&& make && make install
  # ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
  mysqli.so
  # vim /usr/local/php7/php.ini
  extension=mysqli#这行注释去掉
  extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/" #这行注释去了,将上面模块的路径添加上。
  # netstat -anpt|grep php-fpm
  tcp      0      0 127.0.0.1:9000            0.0.0.0:
  # kill 2018
  # /usr/local/php7/sbin/php-fpm
  Failed loading /usr/local/php7/lib/php/ZendGuardLoader.so:/usr/
  # netstat -anpt|grep nginx
  tcp      0      0 0.0.0.0:80                  0.0.0.0:
  7.PHP 页面访问测试
  # vim /usr/local/nginx/html/index.php
  
  8.测试结果
  在 LEMP 平台中部署 web 应用

[*]部署程序代码  # cd
  # tar xf wordpress-4.9.1-zh_CN.tar.gz
  # mv wordpress /usr/local/nginx/html/word
  # chown -R nginx.nginx /usr/local/nginx/html/word/
[*]创建数据库  # mysql -uroot -p123456
  mysql> create database media;
  Query OK, 1 row affected (0.00 sec)

  mysql> grant all on media.* to 'media'@'localhost'>  Query OK, 0 rows affected (0.00 sec)

[*]安装 web 应用
  http_load 压测
  http_load简介:
  http_load 是基于 LINUX 平台的一种性能压力测试工具,以并行复用的方式运行,用以 测试 web 服务器的吞吐量,WEB 页面性能与负载,只能在 LINUX 平台上使用,且只能测试 web 服务,不能对数据库进行压力测试。
  http_load 同于大多数压力测试工具,它可以通过一个单一的进程运行,一般不会导致 web 服务器 down 机。也可以测试 HTTPS 类的网站请求。

[*]下载和安装 http_load  # tar xf http_load-09Mar2016.tar.gz
  # cd http_load-09Mar2016
  # make && make install
  # ls
  FILES      http_load.c      port.h    timers.h
  http_load    Makefile         README    timers.o
  http_load.1make_test_filestimers.cversion.h
[*]将要测试的地址写入文件中  # vim urlfile#一行一个URL,可以多些几个
  http://192.168.200.135/index.php
  http://192.168.200.135/word/index.php
[*]测试文件中的地址  参数说明:
  -parallel 简写-p :含义是并发访问的线程数
  -fetches 简写-f :含义是总计的访问次数
  -rate 简写-r :含义是每秒的访问频率
  -seconds 简写-s :含义是总计的访问时间
  # ./http_load -p 10 -s 5 urlfile
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  33 fetches, 10 max parallel, 1.46392e+06 bytes, in 5.00039 seconds
  44361.3 mean bytes/connection
  6.59949 fetches/sec, 292762 bytes/sec
  msecs/connect: 0.14103 mean, 0.885 max, 0.012 min
  msecs/first-response: 1254.21 mean, 4487.07 max, 1.352 min
  4 bad byte counts
  HTTP response codes:
  code 200 -- 23
  code 301 -- 10
[*]结果分析  33 fetches, 10 max parallel, 1.46392e+06 bytes, in 5.00039 seconds
  一共请求连接 33 次,最大并发线程 10 个,持续 5 秒内,总传输速率为 1.46392e+06 bytes
  44361.3 mean bytes/connection
  每次请求连接平均数据量(1.46392e+06/33)
  6.59949 fetches/sec, 292762 bytes/sec
  每秒的响应请求连接数为6.59949 个每秒传输的数据为 292762 bytes /毫秒
  msecs/connect: 0.14103 mean, 0.885 max, 0.012 min
  每次连接平均响应时间:0.14103 毫秒,最大时间: 0.885 毫秒,最小时间:0.012 毫秒
  msecs/first-response: 1254.21 mean, 4487.07 max, 1.352 min
  每次连接平均返回时间:1254.21 毫秒,最大时间:4487.07 毫秒,最小时间:1.352 毫秒
  4 bad byte counts
  同一个http请求不同结果的个数
  HTTP response codes:
  code 200 -- 23
  code 301 -- 10
  返回状态码200,23次
  返回状态码301,10次
[*]根据访问量,调整 Nginx_fastcgi 的性能参数  # cd /usr/local/nginx/conf/
  # cp nginx.conf{,.bak}
  # vim nginx.conf
  http {   #此行下内容为追加调优项,×××部分是一行。
  fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
  keys_zone=TEST:10m inactive=5m;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_cache_valid 200 302 1h;
  fastcgi_cache_valid 301 1d;
  fastcgi_cache_valid any 1m;
  # nginx -t
  nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  # /etc/init.d/nginx restart
  Nginx is stoping                                           [确定]
  Nginx is OK                                                [确定]
[*]调优参数说明:  fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;                     #缓存目录位置,目录结构等级,关键字区域实际和非活 动时间
  fastcgi_connect_timeout 300;       #连接到后端 fastcgi 超时时间
  fastcgi_send_timeout 300;         #向 fastcgi 请求超时时间(这个指定值已经完成两次握手后 向 fastcgi 传送请求的超时时间)
  fastcgi_rend_timeout 300;      #接收 fastcgi 应答超时时间,同理也是 2 次握手后 fastcgi_buffer_size 64k;         #读取 fastcgi 应答第一部分需要多大缓冲区,该值表示使 用 1 个 64kb 的缓冲区读取应答第一部分(应答头),可以设置为 fastcgi_buffers 选项缓冲区大 小
  fastcgi_buffers 4 64k;            #指定本地需要多少和多大的缓冲区来缓冲 fastcgi 应答请 求,假设一个 php 或 java 脚本所产生页面大小为 256kb,那么会为其分配 4 个 64kb 的缓冲 来缓存;若页面大于 256kb,那么大于的 256kb 的部分会缓存到 fastcgi_temp 指定路径中,这并非是个好办法,内存数据处理快于硬盘,一般该值应该为站点中 php/java 脚本所产生 页面大小中间值,如果站点大部分脚本所产生的页面大小为 256kb,那么可把值设置为 16 16k,4 64k 等
  fastcgi_busy_buffers_size 128k;#默认值是 fastcgi_buffer 的 2 倍 fastcgi_temp_file_write_size 128k;#写入缓存文件使用多大的数据块,默认值是 fastcgi_buffer 的 2 倍 fastcgi_cache TEST; #开启 fastcgi 缓存并为其指定为 TEST 名称,降低 cpu 负载, 防止 502 错误发生.对本次试验,发现开启后会导致网页打不开现象,故此处关闭处理!!!,
  fastcgi_cache_valid 200 302 1h;      #应答代码缓存时间,200 和 302 应答缓存为 1 个小时
  fastcgi_cache_valid 301 1d;         #301 一天
  fastcgi_cache_valid any 1m;         #其他 1 分钟
[*]调整后测试效果  # ./http_load -p 10 -s 5 urlfile
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  70 fetches, 10 max parallel, 3.18245e+06 bytes, in 5.00058 seconds
  45463.5 mean bytes/connection
  13.9984 fetches/sec, 636416 bytes/sec
  msecs/connect: 0.0684143 mean, 0.433 max, 0.011 min
  msecs/first-response: 547.67 mean, 1986.65 max, 1.228 min
  4 bad byte counts
  HTTP response codes:
  code 200 -- 50
  code 301 – 20
  通过 Xcache 加速 PHP 引擎解析
  关于 Xcache 的一些说明:
  Xcache 是一个国人开发的开源的,快速稳定的 PHP opcode 缓存器/优化器,这意味着 他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避 免重复的编译过程, 能够直接使用缓冲区已编译的代码从而提高速度. 通常能够提高您的页 面生成速率 2 到 5 倍, 降低服务器负载。
  Xcache 和 memcached 的区别:
  Xcache 和 memcached 是两个不同层面的缓存,不存在可比性。
  Xcache 是 php 底层的缓存,它将 PHP 程式编译成字节码(byte code),再通过在服务器 上安装对应的程式来执行 PHP 脚本。而 memcached 是应用层缓存,它通过在内存中缓存数 据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。
  Xcache 是不需要修改 PHP 程序的,只要安装了就可以自动为你的程序加速,而 memcached(预热数据) 则需要你修改程序的,需要你在操作数据库之前先询问下 memcached 有没有缓存数据,如果有且数据没有过期则不再访问数据库,以达到减少数据 库查询的目的
  XCache 工程由 mOo 领队,他也是 Lighttpd 的开发成员之一。Lighttpd 是最快的 Web 服 务器应用程序之一,并且超越 Apache 以及许多其他 Web 服务器。 XCache 努力达到类似的 效果。
  XCache 的特色功能:
  以高优化度编写的 opcode 缓存器。apc, ,eaccelerator 也是不错的 opcode 缓存器。
  使用代码生成器生成 C 代码。急剧减少人为错误 稳定支持 PHP_4_3/PHP_4_4/PHP_5_1/PHP_5_2 系列 支持所有 php cvs 分支的最新版本, 例如 PHP_4_3 PHP_4_4 PHP_5_0 PHP_5_1 PHP_5_2, 甚 至 HEAD (6.x) Alpha 级支持 "alpha 阶段 php6", 亦即支持 Unicode 支持"只读缓存器保护模式"避免共享内存被 php-核心/模块或者任何 XCache 以外的代码毁 坏 原子级 get/set/inc/dec api 操作, 提供给 php 程序员用于缓存数据 XCache 上线统计收集器 (仅服务器管理员手工启用), 能够显示 XCache 在不同 php 版本 上的稳定性(或者不稳定性) 编译/运行期间完整性自测功能, 自动并即时发现 php 引擎产生的 C 结构变化. 更容易跟 上与新的 php 版本. 参考 a real life example (注意第 18/19 号信息) 在发现缓冲数据出错/毁坏的情况下禁止 XCache 自己而避免整个 php 停止工作 优化器, 增加执行速度, 已在 2.0 测试阶段

[*]安装 Xcache 前要执行 phpize 配置  # cd
  # tar xf xcache-3.1.0.tar.gz -C /usr/local/
  # cd /usr/local/xcache-3.1.0/
  # /usr/local/php7/bin/phpize
  Configuring for:
  PHP Api Version:         20170718
  Zend Module Api No:      20170718
  Zend Extension Api No:   320170718
  # ./configure --prefix=/usr/local/php7/lib/php/extensions --with-php-config=/usr/local/php7/bin/php-config--enable-xcache && make && make install
  会出现下面报错。Xcache3.1支持PHP5.5,不支持7版本。
  AUTOCHECK ERROR: ==== dasm znode =================
  AUTOCHECK ERROR: ==== dasm zend_op_array =================
  make: ** 错误 1
  通过PHP7自带的opcache提高性能
  1.生成opcache模块并启用配置
  # cd /usr/local/php-7.2.4/ext/
  # cd opcache/
  # /usr/local/php7/bin/phpize
  Configuring for:
  PHP Api Version:         20170718
  Zend Module Api No:      20170718
  Zend Extension Api No:   320170718
  # ./configure --with-php-config=/usr/local/php7/bin/php-config--enable-opcache && make && make install
  # ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
  mysqli.somysql.soopcache.aopcache.so
  # vim /usr/local/php7/php.ini
  
  opcache.enable=1         #去掉这行注释,1表示启用,默认是0
  opcache.enable_cli=1      #去掉这行注释,1表示启用,默认是0
  opcache.memory_consumption=128   #去掉这行注释,默认值
  opcache.interned_strings_buffer=8       #去掉这行注释,默认值
  opcache.max_accelerated_files=10000    #去掉这行注释,默认值
  opcache.max_wasted_percentage=5      #去掉这行注释,默认值
  opcache.validate_timestamps=1          #去掉这行注释,默认值
  zend_extension=opcache.so             #追加这行
  # netstat -anpt|grep php-fpm
  tcp      0      0 127.0.0.1:9000            0.0.0.0:                   LISTEN      2088/php-fpm
  # kill 2088
  # netstat -anpt|grep php-fpm
  # /usr/local/php7/sbin/php-fpm
  Failed loading /usr/local/php7/lib/php/ZendGuardLoader.so:/usr/local/php7/lib/php/ZendGuardLoader.so: undefined symbol: zval_used_for_init
  # /etc/init.d/nginx restart
  Nginx is stoping                                           [确定]
  Nginx is OK                                                [确定]
  2.调整后测试效果
  # http_load -p 10 -s 5 urlfile
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  http://192.168.200.135/index.php: byte count wrong
  310 fetches, 10 max parallel, 1.09683e+07 bytes, in 5.00015 seconds
  35381.6 mean bytes/connection
  61.9981 fetches/sec, 2.19359e+06 bytes/sec
  msecs/connect: 0.0426806 mean, 0.259 max, 0.011 min
  msecs/first-response: 155.628 mean, 417.775 max, 0.902 min
  24 bad byte counts
  HTTP response codes:
  code 200 -- 160
  code 301 -- 150

页: [1]
查看完整版本: 《Nginx1.13+MySQL 5.7+PHP7.2 优化》