安装部署LNMPNginx优化、PHP加速进行压力测试

部署LNMP环境:

主机

IP

主机名

Centos7.2

192.168.5.128

www.benet.com

部署步骤如下:

使用yum仓库安装Nginx依赖包

wKiom1l3NxCylFPQAAAHUeu4dcs589.png 

yum -y install  gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel

创建Nginx用户 组解压Nginx软件包

wKioL1l3NxrhEHTMAAAKFr_LgJU750.png 

编译安装Nginx

wKioL1l3NyKxWvkaAAAQrlGeN8A549.png 

./configure --prefix=/usr/local/nginx1.10 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx && make &&make install

修改配置文件 全部配置文件内容如下:

user nginx nginx;

worker_processes  4;

worker_cpu_affinity 0001 0010 0100 1000;

error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

pid        logs/nginx.pid;

 

 

events {

use epoll;

    worker_connections  65535;

multi_accept on;

}

 

 

http {

include       mime.types;

    default_type  application/octet-stream;

 

    #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;

 

sendfile        on;

tcp_nopush     on;

    keepalive_timeout  65;

tcp_nodelay on;

client_header_buffer_size 4k;

open_file_cache max=102400 inactive=20s;

    open_file_cache_valid 30s;

    open_file_cache_min_uses 1;

    client_header_timeout 15;

    client_body_timeout 15;

reset_timedout_connection on;

    send_timeout 15;

server_tokens off;

client_max_body_size 10m;

 

    fastcgi_connect_timeout     600;

    fastcgi_send_timeout 600;

    fastcgi_read_timeout 600;

fastcgi_buffer_size 64k;

    fastcgi_buffers     4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

    fastcgi_temp_path /usr/local/nginx1.10/nginx_tmp;

fastcgi_intercept_errors on;

    fastcgi_cache_path /usr/local/nginx1.10/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;

 

gzip on;

    gzip_min_length  2k;

    gzip_buffers     4 32k;

    gzip_http_version 1.1;

    gzip_comp_level 6;

    gzip_types  text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;

gzip_vary on;

gzip_proxied any;

server {

listen       80;

        server_name  www.benet.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

location ~* ^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {

valid_referers none blocked  www.benet.com benet.com;

if ($invalid_referer) {

                #return 302  http://www.benet.com/img/nolink.jpg;

return 404;

break;

             }

access_log off;

        }

location / {

root   html;

index  index.php index.html index.htm;

        }

location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {

expires 30d;

            #log_not_found off;

access_log off;

        }

 

location ~* \.(js|css)$ {

expires 7d;

log_not_found off;

access_log off;

        }      

 

location = /(favicon.ico|roboots.txt) {

access_log off;

log_not_found off;

        }

location /status {

stub_status on;

        }

location ~ .*\.(php|php5)?$ {

root html;

            fastcgi_pass 127.0.0.1:9000;

            fastcgi_index index.php;

include fastcgi.conf;

  #          fastcgi_cache cache_fastcgi;

            fastcgi_cache_valid 200 302 1h;

            fastcgi_cache_valid 301 1d;

fastcgi_cache_valid any 1m;

            fastcgi_cache_min_uses 1;

fastcgi_cache_use_stale error timeout invalid_header http_500;

            fastcgi_cache_key http://$host$request_uri;

        }

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

location = /50x.html {

root   html;

        }

   }

}

建立一个软连接 启动Nginx服务 查看端口号是否开启

安装部署LNMP/大并发nginx优化/php性能加速 实战_第1张图片 

测试能不能访问到测试页

安装部署LNMP/大并发nginx优化/php性能加速 实战_第2张图片 

二进制安装mysql--解压mysql二进制包并且创建用户mysql

安装部署LNMP/大并发nginx优化/php性能加速 实战_第3张图片 

删除centos7中带的数据库避免发生冲突

wKiom1l3N1ChAT6bAAAE7FVtu0M606.png 

编写配置/etc/my.cnf文件

安装部署LNMP/大并发nginx优化/php性能加速 实战_第4张图片 

创建下面的数据文件和log日志文件

wKiom1l3N2Szx3DEAAAE1rEGxmQ042.png 

添加权限给data文件 并且授予mysql文件为属主属组 创建软连接

wKioL1l3N23jUFbyAAAJnztWuCM439.png 

初始化mysql

wKiom1l3N3eDgtoGAAAHT2p6lNg421.png 

bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

查看log日志中生成的密码 下面登录时会用到

安装部署LNMP/大并发nginx优化/php性能加速 实战_第5张图片 

复制启动文件到/etc/init.d/

wKioL1l3N4vSVU1rAAAK-br2YSQ801.png 

登录mysql数据库

安装部署LNMP/大并发nginx优化/php性能加速 实战_第6张图片 

使用yum安装php依赖包

wKioL1l3N57xf3y0AAAGu4aYGJE763.png 

需要的安装包例如:

安装部署LNMP/大并发nginx优化/php性能加速 实战_第7张图片 

编译安装libmcrypt

wKioL1l3N7iyy1HLAAAMW8SJY8w558.png 

编译安装PHP软件包

安装部署LNMP/大并发nginx优化/php性能加速 实战_第8张图片 

复制PHP配置文件及设置自启动PHP

wKioL1l3N8-gUTrxAAAM8WgQh7U361.png 

修改PHP配置文件

wKiom1l3N9iDo13SAAAHv-z72Qw315.png 

修改/usr/local/php5.6/etc/php.fpm.conf下面几项

pid = run/php-fpm.pid

listen = 0.0.0.0:9000

pm.max_children =300

pm.start_servers =20

pm.min_spare_servers = 20

pm.max_spare_servers = 100

启动PHP服务

wKioL1l3N-TjoxiHAAANOrt52B8735.png 

设置防火墙允许9000端口经过

wKiom1l3N_CD9aMPAAAHBw4Kzxc909.png 

创建PHP测试页

wKioL1l3OACxoc7zAAAK120UvEY723.png 

测试是否能访问php页面

安装部署LNMP/大并发nginx优化/php性能加速 实战_第9张图片 

测试防盗链 首先在被盗主机上的网页确保有图片 比如:

安装部署LNMP/大并发nginx优化/php性能加速 实战_第10张图片 

然后设置域名www.benet.com

设置另一台主机为www.test.com

并且编写盗网页的编码

wKiom1l3OCXhJ_9LAAAPfqGZdzY207.png 

访问网页看是否能盗 因为在被盗主机www.benet.com中设置了防盗链 这时不能盗

wKioL1l3OC2TjbPTAAALx-e1NzM416.png 

点击链接lianjie查看测试成功是否

安装部署LNMP/大并发nginx优化/php性能加速 实战_第11张图片 

测试为成功 防盗链起效

使用yum安装httpd-tools包进行压力测试

wKioL1l3OEaSKsWWAAAEPpOxCjY687.png 

测试压力 能够承受多大的压力

ab -c 600 -n 60000 http://192.168.5.128/index.html

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 192.168.5.128 (be patient)

Completed 6000 requests

Completed 12000 requests

Completed 18000 requests

Completed 24000 requests

Completed 30000 requests

Completed 36000 requests

Completed 42000 requests

Completed 48000 requests

Completed 54000 requests

Completed 60000 requests

Finished 60000 requests

 

 

Server Software:        nginx

Server Hostname:        192.168.5.128

Server Port:            80

 

Document Path:          /index.html

Document Length:        632 bytes

 

Concurrency Level:      600

Time taken for tests:   6.787 seconds

Complete requests:      60000

Failed requests:        0

Write errors:           0

Total transferred:      51480000 bytes

HTML transferred:       37920000 bytes

Requests per second:    8841.04 [#/sec] (mean)

Time per request:       67.865 [ms] (mean)

Time per request:       0.113 [ms] (mean, across all concurrent requests)

Transfer rate:          7407.83 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0   30  48.6     28    1039

Processing:     7   37   8.2     36      73

Waiting:        1   28   8.5     27      61

Total:         46   67  49.3     65    1073

 

Percentage of the requests served within a certain time (ms)

  50%     65

  66%     68

  75%     71

  80%     72

  90%     76

  95%     79

  98%     82

  99%     86

 100%   1073 (longest request)

进行第二次测试 查看变化情况

[root@www html]# ab -c 600 -n 60000 http://192.168.5.128/index.html

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 192.168.5.128 (be patient)

Completed 6000 requests

Completed 12000 requests

Completed 18000 requests

Completed 24000 requests

Completed 30000 requests

Completed 36000 requests

Completed 42000 requests

Completed 48000 requests

Completed 54000 requests

Completed 60000 requests

Finished 60000 requests

 

 

Server Software:        nginx

Server Hostname:        192.168.5.128

Server Port:            80

 

Document Path:          /index.html

Document Length:        632 bytes

 

Concurrency Level:      600

Time taken for tests:   6.640 seconds

Complete requests:      60000

Failed requests:        0

Write errors:           0

Total transferred:      51480000 bytes

HTML transferred:       37920000 bytes

Requests per second:    9035.60 [#/sec] (mean) ##此值越大代表带宽浪费的越少

Time per request:       66.404 [ms] (mean)

Time per request:       0.111 [ms] (mean, across all concurrent requests)

Transfer rate:          7570.85 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0   29  24.1     28    1030

Processing:     8   37   7.9     37     254

Waiting:        5   28   8.5     28     238

Total:         37   66  24.7     64    1074

 

Percentage of the requests served within a certain time (ms)

  50%     64

  66%     68

  75%     70

  80%     72

  90%     76

  95%     79

  98%     83

  99%     85

 100%   1074 (longest request)

编译安装xcache加速PHP

安装部署LNMP/大并发nginx优化/php性能加速 实战_第12张图片 

wKiom1l3OGKRQ8ZJAAANY5NdbgU065.png 

./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5.6/bin/php-config &&make &&make install

编译安装完(就在编译安装后的最后一行)后要记住下面×××字体

wKioL1l3OG3jlmabAAATGrgVvsQ188.png 

创建xcache缓存文件 把后台管理程序存放到网站根目录下 添加配置文件php.ini

wKioL1l3OHuQYhQ7AAALzSTj3V4900.png 

/etc/php.ini的最后面添加就可以

安装部署LNMP/大并发nginx优化/php性能加速 实战_第13张图片 

重启php.ini服务

测试能否打开xcache

安装部署LNMP/大并发nginx优化/php性能加速 实战_第14张图片 

进行php动态网页做压力测试

ab -c 1000 -n 100000 http://192.168.5.128/index.html

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 192.168.5.128 (be patient)

Completed 10000 requests

Completed 20000 requests

Completed 30000 requests

Completed 40000 requests

Completed 50000 requests

Completed 60000 requests

Completed 70000 requests

Completed 80000 requests

Completed 90000 requests

Completed 100000 requests

Finished 100000 requests

 

 

Server Software:        nginx

Server Hostname:        192.168.5.128

Server Port:            80

 

Document Path:          /index.html

Document Length:        632 bytes

 

Concurrency Level:      1000

Time taken for tests:   10.658 seconds

Complete requests:      100000

Failed requests:        0

Write errors:           0

Total transferred:      85800000 bytes

HTML transferred:       63200000 bytes

Requests per second:    9383.02 [#/sec] (mean)

Time per request:       106.576 [ms] (mean)

Time per request:       0.107 [ms] (mean, across all concurrent requests)

Transfer rate:          7861.94 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        4   53 109.1     42    1062

Processing:    11   53  15.4     54     216

Waiting:        1   40  14.5     39     210

Total:         26  105 110.8     99    1133

 

Percentage of the requests served within a certain time (ms)

  50%     99

  66%    103

  75%    106

  80%    108

  90%    113

  95%    117

  98%    124

  99%   1062

 100%   1133 (longest request)