设为首页 收藏本站
查看: 1245|回复: 0

[经验分享] 安装部署LNMP/大并发nginx优化/php性能加速 实战

[复制链接]

尚未签到

发表于 2018-11-11 14:21:31 | 显示全部楼层 |阅读模式
  安装部署LNMP及Nginx优化、PHP加速进行压力测试
  部署LNMP环境:
  主机
  IP
  主机名
  Centos7.2
  192.168.5.128
  www.benet.com
  部署步骤如下:
  使用yum仓库安装Nginx依赖包
DSC0000.png

  yum -y install  gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel
  创建Nginx用户 组解压Nginx软件包
DSC0001.png

  编译安装Nginx
DSC0002.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服务 查看端口号是否开启
DSC0003.png

  测试能不能访问到测试页
DSC0004.png

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

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

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

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

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

  初始化mysql
DSC00010.png

  bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
  查看log日志中生成的密码 下面登录时会用到
DSC00011.png

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

  登录mysql数据库
DSC00013.png

  使用yum安装php依赖包
DSC00014.png

  需要的安装包例如:
DSC00015.png

  编译安装libmcrypt
DSC00016.png

  编译安装PHP软件包
DSC00017.png

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

  修改PHP配置文件
DSC00019.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服务
DSC00020.png

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

  创建PHP测试页
DSC00022.png

  测试是否能访问php页面
DSC00023.png

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

  然后设置域名www.benet.com
  设置另一台主机为www.test.com
  并且编写盗网页的编码
DSC00025.png

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

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

  测试为成功 防盗链起效
  使用yum安装httpd-tools包进行压力测试
DSC00028.png

  测试压力 能够承受多大的压力
  ab -c 600 -n 60000 http://192.168.5.128/index.html
  This is ApacheBench, Version 2.3
  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
  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
DSC00029.png

DSC00030.png

  ./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5.6/bin/php-config &&make &&make install
  编译安装完(就在编译安装后的最后一行)后要记住下面黄色字体
DSC00031.png

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

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

  重启php.ini服务
  测试能否打开xcache
DSC00034.png

  进行php动态网页做压力测试
  ab -c 1000 -n 100000 http://192.168.5.128/index.html
  This is ApacheBench, Version 2.3
  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)


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633774-1-1.html 上篇帖子: CentOS 7.1编译安装Nginx1.8.1+MySQL5.6.19+PHP5.5.14-Arthur 下篇帖子: 树莓派+seafile+nginx+ngrok实现私有云同步盘
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表