LNMP的部署优化及压测
1、安装php
(1)解决依赖关系
[root@www ~]# yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel
安装libmcrypt
[root@www ~]# tar zxf libmcrypt-2.5.7.tar.gz
[root@wwwr ~]# cd libmcrypt-2.5.7/
[root@www libmcrypt-2.5.7]# ./configure –prefix=/usr/local/libmcrypt && make && make install
(2)编译安装php
[root@www ~]# tar zxf php-5.6.27.tar.gz
[root@www ~]# cd php-5.6.27/
[root@www php-5.6.27]# ./configure –prefix=/usr/local/php5.6 –with-mysql=mysqlnd –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd –with-openssl –enable-fpm –enable-sockets –enable-sysvshm –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –with-mhash –with-mcrypt=/usr/local/libmcrypt –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-bz2–enable-maintainer-zts
[root@www php-5.6.27]# make&& make install
(3)提供php配置文件
[root@www php-5.6.27]# cp php.ini-production /etc /php.ini
(4)为php-fpm提供脚本
[root@www php-5.6.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@www php-5.6.27]# chmod +x /etc/init.d/php-fpm
[root@www php-5.6.27]# chkconfig –add php-fpm
[root@www php-5.6.27]# chkconfig php-fpm on
(5)提供php-fpm配置文件并编辑:
# cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
[root@www ~]# vi /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-fpm服务:
[root@www ~]# service php-fpm start
Starting php-fpm done
[root@www ~]# netstat -anpt | grep php-fpm
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 25456/php-fpm: mast
[root@www ~]# firewall-cmd –permanent –add-port=9000/tcp
success
[root@www ~]# firewall-cmd –reload
Success
在nginx.conf文件的server中添加下面内容支持php
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;
}
下面是nginx.conf的一个完整配置文件
user www www;
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服务
[root@www ~]# /usr/local/nginx1.10/sbin/nginx -s reload
三、验证、压力测试
(1)验证防盗链
使用apache做为一个测试站点,域名为www.test.com,在测试页上做一个超链接,链接nginx站点的一张图片
[root@centos1 ~]# cat /var/www/html/index.html
<a href=”http://www.benet.com/11.gif”>lianjie</a>
Nginx站点的网页目录结如下
tree /usr/local/nginx1.10/html/
/usr/local/nginx1.10/html/
├── 11.gif
├── 50x.html
├── img
│ └── nolink.jpg
├── index.html
├── test.php
在客户端浏览器中输入www.test.com (注意域名解析和防火墙)
点击页面链接
从上图可以看到防盗链设置生效了
(2)验证gzip功能
使用谷歌浏览器测试访问,如下图显示结果:(提示:在访问测试页之前按F12键)
用户访问test.php文件,在上图中content-encoding:gzip表明响应给用户的数据是压缩传输。
(3)压力测试
安装httpd-tools软件包
[root@www ~]# yum -y install httpd-tools
[root@www ~]# ab -c 500 -n 50000 http://www.benet.com/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 www.benet.com (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests
Server Software: IIS
Server Hostname: www.benet.com
Server Port: 80
Document Path: /index.html
Document Length: 612 bytes
Concurrency Level: 500
Time taken for tests: 5.734 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 41800000 bytes
HTML transferred: 30600000 bytes
Requests per second: 8719.82 [#/sec] (mean)
Time per request: 57.341 [ms] (mean)
Time per request: 0.115 [ms] (mean, across all concurrent requests)
Transfer rate: 7118.92 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 25 4.2 25 38
Processing: 7 32 5.5 31 47
Waiting: 4 24 6.8 21 39
Total: 40 57 3.9 57 71
float
Percentage of the requests served within a certain time (ms)
50% 57
66% 59
75% 59
80% 60
90% 61
95% 62
98% 63
99% 64
100% 71 (longest request)
第二次压力测试,比较两次的差异
[root@www ~]# ab -c 1000 -n 100000 http://www.benet.com/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 www.benet.com (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: IIS
Server Hostname: www.benet.com
Server Port: 80
Document Path: /index.html
Document Length: 612 bytes
Concurrency Level: 1000
Time taken for tests: 12.010 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 83600000 bytes
HTML transferred: 61200000 bytes
Requests per second: 8326.49 [#/sec] (mean)
Time per request: 120.099 [ms] (mean)
Time per request: 0.120 [ms] (mean, across all concurrent requests)
Transfer rate: 6797.80 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 53 8.9 53 82
Processing: 17 67 11.4 66 98
Waiting: 0 49 14.3 43 84
Total: 70 119 6.5 120 140
Percentage of the requests served within a certain time (ms)
50% 120
66% 122
75% 123
80% 124
90% 126
95% 128
98% 129
99% 130
100% 140 (longest request)
(5)xcache加速php
安装xcache
wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz #下载
[root@www ~]# tar zxf xcache-3.2.0.tar.gz #解压
[root@www ~]# cd xcache-3.2.0/ #进入安装目录
[root@www xcache-3.2.0]# /usr/local/php5.6/bin/phpize#用phpize生成configure配置文件
[root@www xcache-3.2.0]# ./configure –enable-xcache –enable-xcache-coverager –enable-xcache-optimizer –with-php-config=/usr/local/php5.6/bin/php-config #配置
[root@www xcache-3.2.0]# make && make install #编译、安装
Installing shared extensions:
/usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/
安装完成之后,出现下面的界面,记住以下路径,后面会用到
/usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/
2)创建xcache缓存文件
# touch /tmp/xcache
# chmod 777 /tmp/xcache
3)拷贝xcache后台管理程序到网站根目录
[root@www xcache-3.2.0]# cp -r htdocs/ /usr/local/nginx1.10/html/xcache
4)配置php支持xcache
vi / etc/php.ini #编辑配置文件,在最后一行添加以下内容
[xcache-common]
extension = /usr/local/php5.6/lib/php/extensions/no-debug-zts-20131226/xcache.so
[xcache.admin]
xcache.admin.enable_auth = Off
[xcache]
xcache.shm_scheme =”mmap”
xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=64M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache.readonly_protection = Off
xcache.mmap_path =”/tmp/xcache”
xcache.coredump_directory =””
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off
[xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =””
测试
service php-fpm restart #重启php-fpm
浏览器打开网站根目录下面的xcache
http://http://www.benet.com/xcache可以看到如下页面:
测试对php动态页面的压力测试
[root@www ~]# ab -c 1000 -n 100000 http://www.benet.com/test.php
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 www.benet.com (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: IIS
Server Hostname: www.benet.com
Server Port: 80
Document Path: /test.php
Document Length: 85102 bytes
Concurrency Level: 1000
Time taken for tests: 13.686 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 8527900000 bytes
HTML transferred: 8510200000 bytes
Requests per second: 7306.71 [#/sec] (mean)
Time per request: 136.861 [ms] (mean)
Time per request: 0.137 [ms] (mean, across all concurrent requests)
Transfer rate: 608504.46 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 17 5.5 17 81
Processing: 21 119 10.8 121 140
Waiting: 1 17 6.7 16 68
Total: 50 136 8.1 137 151
Percentage of the requests served within a certain time (ms)
50% 137
66% 139
75% 140
80% 141
90% 143
95% 144
98% 146
99% 148
100% 151 (longest request)
参考链接 :
http://www.unmin.club/?p=167