《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 主机配置

  1. 配置主机名&关闭 Selinux 与 iptables 防火墙:
    [root@mobanji ~]# hostname master
    [root@mobanji ~]# bash
    [root@master ~]# echo "192.168.200.135 master" >>/etc/hosts
    [root@master ~]# vim /etc/sysconfig/network
    NETWORKING=yes

[root@master ~]# /etc/init.d/iptables stop
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
[root@master ~]# setenforce 0
setenforce: SELinux is disabled

  1. 配置 YUM 仓库:
    [root@master ~]# cd /etc/yum.repos.d/
    [root@master yum.repos.d]# ls
    bak CentOS-Base.repo CentOS-Media.repo epel.repo epel-testing.repo
    [root@master yum.repos.d]# mv o bak/
    [root@master yum.repos.d]# mv bak/
    M ./
    [root@master yum.repos.d]# vim CentOS-Media.repo
    [c6-media]
    name=CentOS- - Media
    baseurl=file:///media/cdrom/
    gpgcheck=1
    enabled=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    [root@master ~]# mount /dev/sr0 /media/cdrom/
    mount: block device /dev/sr0 is write-protected, mounting read-only
    编译及安装 Nginx
    1. 安装依赖软件并卸载 rpm 格式软件包
    [root@master ~]# yum install -y pcre-devel
    [root@master ~]# rpm -qa httpd mysql mysql-server php
    mysql-5.1.71-1.el6.x86_64
    httpd-2.2.15-29.el6.centos.x86_64
    [root@master ~]# rpm -e mysql-5.1.71-1.el6.x86_64 --nodeps
    [root@master ~]# rpm -e httpd-2.2.15-29.el6.centos.x86_64 --nodeps
    2. 创建程序用户和程序组
    [root@master ~]# useradd -M -s /sbin/nologin nginx
    3. 编译安装 Nginx
    [root@mobanji ~]# tar xf nginx-1.13.12.tar.gz -C /usr/local/
    [root@master ~]# cd /usr/local/nginx-1.13.12/
    [root@master nginx-1.13.12]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
    [root@master nginx-1.13.12]# ln -s /usr/local/nginx/sbin/
    /usr/local/bin/
    4.Nginx 的运行控制
    a.检查配置文件
    [root@master nginx-1.13.12]# 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
    [root@master nginx-1.13.12]# nginx
    [root@master nginx-1.13.12]# netstat -anpt|grep nginx
    tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 4493/nginx
    c.编写 nginx 服务器脚本
    [root@master nginx-1.13.12]# 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
    [root@master nginx-1.13.12]# chmod +X /etc/init.d/nginx
    [root@master nginx-1.13.12]# chkconfig --add nginx
    [root@master nginx-1.13.12]# chkconfig --list nginx
    nginx 0:关闭 1:关闭 2:启用 3:启用 4:关闭 5:启用 6:关闭
    5.配置文件 nginx.conf
    [root@master nginx-1.13.12]# cd /usr/local/nginx/conf/
    [root@master conf]# vim nginx.conf
    events {
    worker_connections 1024;
    use epoll; #添加这行
    }
    #去掉下面几行注释:
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log logs/access.log main;
    [root@master conf]# 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
    [root@master conf]# /etc/init.d/nginx restart
    Nginx is stoping [确定]
    Nginx is OK [确定]
    6.nginx 的访问测试

LEMP 架构及应用
构建 LEMP 网站平台—MySql 数据库

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

  1. MySQL Slave 主机安装前的准备
    [root@slave ~]# vim /etc/sysconfig/network
    [root@slave ~]# /etc/init.d/iptables stop
    iptables:将链设置为政策 ACCEPT:filter [确定]
    iptables:清除防火墙规则: [确定]
    iptables:正在卸载模块: [确定]
    [root@slave ~]# setenforce 0
    setenforce: SELinux is disabled
    [root@mobanji ~]# hostname slave
    [root@mobanji ~]# bash
    [root@slave ~]# vim /etc/sysconfig/network
    NETWORKING=yes
    HOSTNAME=slave
  2. 安装 MySQL 数据库
    [root@slave ~]# rpm -qa mysql
    mysql-5.1.71-1.el6.x86_64
    [root@slave ~]# rpm -e mysql-5.1.71-1.el6.x86_64 –nodeps
    [root@slave ~]# tar xf cmake-2.8.6.tar.gz -C /usr/local/
    [root@slave ~]# tar xf mysql-5.7.3-m13.tar.gz -C /usr/local/
    [root@slave ~]# cd /usr/local/cmake-2.8.6/
    [root@slave cmake-2.8.6]# ./configure && gmake && gmake install
    [root@slave cmake-2.8.6]# cd ../mysql-5.7.3-m13/
    [root@slave 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
  3. 安装后的调整和优化
    [root@slave mysql-5.7.3-m13]# cp -f support-files/my-default.cnf /etc/my.cnf
    cp:是否覆盖"/etc/my.cnf"? y
    [root@slave mysql-5.7.3-m13]# cp support-files/mysql.server /etc/init.d/mysqld
    [root@slave mysql-5.7.3-m13]# chmod +x /etc/init.d/mysqld
    [root@slave mysql-5.7.3-m13]# chkconfig --add mysqld
    [root@slave mysql-5.7.3-m13]# chkconfig --list mysqld
    mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
    [root@slave mysql-5.7.3-m13]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
  4. 初始化数据库
    [root@slave mysql-5.7.3-m13]# useradd -M -s /sbin/nologin mysql
    [root@slave mysql-5.7.3-m13]# chown -R mysql.mysql /usr/local/mysql
    [root@slave mysql-5.7.3-m13]# /usr/local/mysql/scripts/mysql_install_db --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --user=mysql
    5.启动 Mysql 服务
    [root@slave mysql-5.7.3-m13]# /etc/init.d/mysqld start
    Starting MySQL.. [确定]
    [root@slave mysql-5.7.3-m13]# netstat -anpt|grep mysqld
    tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 34802/mysqld
    [root@slave mysql-5.7.3-m13]# mysqladmin -uroot password "123456"
  5. 与主服务器同步时间
    [root@slave mysql-5.7.3-m13]# rpm -q ntpdate
    ntpdate-4.2.6p5-12.el6.centos.2.x86_64
    [root@slave mysql-5.7.3-m13]# ntpdate 192.168.200.135
    14 Apr 09:23:07 ntpdate[34851]: adjust time server 192.168.200.135 offset 0.011442 sec
  6. 配置从服务器 SLAVE 修改配置文件
    [root@slave mysql-5.7.3-m13]# vim /etc/my.cnf
    [mysqld]
    server_id = 2 #这行注释去掉,ID号唯一性,不和主相同就行
    relay-log=relay-log-bin #添加这行
    relay-log-index=slave-relay-bin.index #添加这行
    [root@slave mysql-5.7.3-m13]# /etc/init.d/mysqld restart
    Shutting down MySQL.. [确定]
    Starting MySQL. [确定]
    [root@slave mysql-5.7.3-m13]# 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-bin.000002
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 relay log; waiting for the slave I/O thread to update it
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)

  1. 验证主从复制效果:
    在 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)
如果数据库中有大量的旧数据,可先将主数据库数据备份并传给从数据库导入:
[root@master mysql-5.7.3-m13]# mysqldump -uroot -p123456 --opt --all-databases >all-data.sql
从数据库导入
[root@slave ~]# mysql -uroot -p123456 test <all-data.sql

  1. 删除验证库
    mysql> drop database crushlinux;
    Query OK, 0 rows affected (0.00 sec)
    构建 LEMP 网站平台—安装 PHP 解析环境
  2. 编译安装 PHP
    [root@master mysql-5.7.3-m13]# cd
    [root@master ~]# tar xf php-7.2.4.tar.gz -C /usr/local/
    [root@master ~]# cd /usr/local/php-7.2.4/
    [root@master 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
  3. PHP 安装后的调整
    [root@master php-7.2.4]# ln -s /usr/local/php7/bin/ /usr/local/bin/
    [root@master php-7.2.4]# ln -s /usr/local/php7/sbin/
    /usr/local/bin/
    [root@master php-7.2.4]# cp -f php.ini-development /usr/local/php7/php.ini
  4. 安装 ZendGuardLoader
    [root@master ~]# tar xf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
    [root@master ~]# cd ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/
    [root@master php-5.3.x]# cp ZendGuardLoader.so /usr/local/php7/lib/php/
    [root@master php-5.3.x]# vim /usr/local/php7/php.ini
    #追加下面两行:
    zend_extension=/usr/local/php7/lib/php/ZendGuardLoader.so
    zend_loader.enable=1
  5. 启用 PHP-FPM 进程,配置 Nginx 支持 PHP
    [root@master php-5.3.x]# cd /usr/local/php7/etc/
    [root@master etc]# cp php-fpm.conf.default php-fpm.conf
    [root@master etc]# vim php-fpm.conf
    [global]
    pid = run/php-fpm.pid #这行去掉注释
    include=/usr/local/php7/etc/php-fpm.d/*.conf #这行的文件才是正真配置文件
    [root@master etc]# cd php-fpm.d/
    [root@master php-fpm.d]# ls
    www.conf.default
    [root@master php-fpm.d]# cp www.conf.default www.conf
    [root@master php-fpm.d]# vim www.conf
    [www]
    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

[root@master php-fpm.d]# /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
[root@master php-fpm.d]# ls /usr/local/php7/lib/php/
Archive Console doc OS pearcmd.php peclcmd.php System.php XML
build data extensions PEAR PEAR.php Structures test ZendGuardLoader.so
[root@master php-fpm.d]# netstat -anpt|grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 55633/php-fpm

  1. 配置 Nginx 支持 PHP 解析
    [root@master php-fpm.d]# vim /usr/local/nginx/conf/nginx.conf
    server {
    listen 80;
    server_name localhost;
    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_index index.php;
    include fastcgi.conf; #这行修改
    }
    }
    }
    [root@master ~]# /etc/init.d/nginx restart
    Nginx is stoping [确定]
    Nginx is OK [确定]
    7.安装mysqli.so扩展模块
    [root@master mysqli]# cd /usr/local/php-7.2.4/ext/mysqli/
    [root@master mysqli]# /usr/local/php7/bin/phpize
    [root@master mysqli]# ./configure --with-php-config=/usr/local/php7/bin/php-config --with-mysqli=/usr/local/mysql/bin/mysql_config && make && make install
    [root@master mysqli]# ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
    mysqli.so
    [root@master mysqli]# vim /usr/local/php7/php.ini
    extension=mysqli #这行注释去掉
    extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/" #这行注释去了,将上面模块的路径添加上。
    [root@master mysqli]# netstat -anpt|grep php-fpm
    tcp 0 0 127.0.0.1:9000 0.0.0.0:
    [root@master mysqli]# kill 2018
    [root@master mysqli]# /usr/local/php7/sbin/php-fpm
    Failed loading /usr/local/php7/lib/php/ZendGuardLoader.so: /usr/
    [root@master mysqli]# netstat -anpt|grep nginx
    tcp 0 0 0.0.0.0:80 0.0.0.0:

    7.PHP 页面访问测试
    [root@master ~]# vim /usr/local/nginx/html/index.php
    <?php
    phpinfo ();
    ?>

8.测试结果

在 LEMP 平台中部署 web 应用

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

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

  1. 安装 web 应用

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

  1. 下载和安装 http_load
    [root@master ~]# tar xf http_load-09Mar2016.tar.gz
    [root@master ~]# cd http_load-09Mar2016
    [root@master http_load-09Mar2016]# make && make install
    [root@master http_load-09Mar2016]# ls
    FILES http_load.c port.h timers.h
    http_load Makefile README timers.o
    http_load.1 make_test_files timers.c version.h
  2. 将要测试的地址写入文件中
    [root@master http_load-09Mar2016]# vim urlfile #一行一个URL,可以多些几个
    http://192.168.200.135/index.php
    http://192.168.200.135/word/index.php
  3. 测试文件中的地址
    参数说明:
    -parallel 简写-p :含义是并发访问的线程数
    -fetches 简写-f :含义是总计的访问次数
    -rate 简写-r :含义是每秒的访问频率
    -seconds 简写-s :含义是总计的访问时间
    [root@master http_load-09Mar2016]# ./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
  4. 结果分析
    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次
  5. 根据访问量,调整 Nginx_fastcgi 的性能参数
    [root@master http_load-09Mar2016]# cd /usr/local/nginx/conf/
    [root@master conf]# cp nginx.conf{,.bak}
    [root@master conf]# 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;
    [root@master conf]# 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
    [root@master conf]# /etc/init.d/nginx restart
    Nginx is stoping [确定]
    Nginx is OK [确定]
  6. 调优参数说明:
    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 分钟
  7. 调整后测试效果
    [root@master http_load-09Mar2016]# ./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 测试阶段

  1. 安装 Xcache 前要执行 phpize 配置
    [root@master http_load-09Mar2016]# cd
    [root@master ~]# tar xf xcache-3.1.0.tar.gz -C /usr/local/
    [root@master ~]# cd /usr/local/xcache-3.1.0/
    [root@master 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
    [root@master xcache-3.1.0]# ./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: ** [processor.out.c] 错误 1
    通过PHP7自带的opcache提高性能
    1.生成opcache模块并启用配置
    [root@master ~]# cd /usr/local/php-7.2.4/ext/
    [root@master ext]# cd opcache/
    [root@master opcache]# /usr/local/php7/bin/phpize
    Configuring for:
    PHP Api Version: 20170718
    Zend Module Api No: 20170718
    Zend Extension Api No: 320170718
    [root@master opcache]# ./configure --with-php-config=/usr/local/php7/bin/php-config --enable-opcache && make && make install
    [root@master opcache]# ls /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/
    mysqli.so mysql.so opcache.a opcache.so
    [root@master opcache]# vim /usr/local/php7/php.ini
    [opcache]
    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 #追加这行
    [root@master opcache]# netstat -anpt|grep php-fpm
    tcp 0 0 127.0.0.1:9000 0.0.0.0:
    LISTEN 2088/php-fpm
    [root@master opcache]# kill 2088
    [root@master opcache]# netstat -anpt|grep php-fpm
    [root@master opcache]# /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
    [root@master opcache]# /etc/init.d/nginx restart
    Nginx is stoping [确定]
    Nginx is OK [确定]

2.调整后测试效果
[root@master http_load-09Mar2016]# 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

转载于:https://blog.51cto.com/qingfeng00/2105985

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值