安装部署LNMP/大并发nginx优化/php性能加速 实战

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安装部署LNMP/大并发nginx优化/php性能加速 实战相关的知识,希望对你有一定的参考价值。

                安装部署LNMPnginx优化、php加速进行压力测试

部署LNMP环境:

主机

IP

主机名

Centos7.2

192.168.5.128

www.benet.com

部署步骤如下:

使用yum仓库安装Nginx依赖包

技术分享 

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

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

技术分享 

编译安装Nginx

技术分享 

./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服务 查看端口号是否开启

技术分享 

测试能不能访问到测试页

技术分享 

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

技术分享 

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

技术分享 

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

技术分享 

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

技术分享 

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

技术分享 

初始化mysql

技术分享 

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

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

技术分享 

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

技术分享 

登录mysql数据库

技术分享 

使用yum安装php依赖包

技术分享 

需要的安装包例如:

技术分享 

编译安装libmcrypt

技术分享 

编译安装PHP软件包

技术分享 

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

技术分享 

修改PHP配置文件

技术分享 

修改/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服务

技术分享 

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

技术分享 

创建PHP测试页

技术分享 

测试是否能访问php页面

技术分享 

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

技术分享 

然后设置域名www.benet.com

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

并且编写盗网页的编码

技术分享 

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

技术分享 

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

技术分享 

测试为成功 防盗链起效

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

技术分享 

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

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)

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

[[email protected] 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

技术分享 

技术分享 

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

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

技术分享 

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

技术分享 

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

技术分享 

重启php.ini服务

测试能否打开xcache

技术分享 

进行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)

 


本文出自 “centos7” 博客,请务必保留此出处http://12832314.blog.51cto.com/12822314/1950926

以上是关于安装部署LNMP/大并发nginx优化/php性能加速 实战的主要内容,如果未能解决你的问题,请参考以下文章

Nginx网站服务+LNMP架构及应用部署

LNMP平台部署及应用

搭建lnmp环境,部署php动态网站

LNMP架构部署——可跟做

LNMP平台搭建及优化

LNMP架构