安裝部署LNMP/大並發nginx優化/php性能加速 實戰
安裝部署LNMP及Nginx優化、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性能加速 實戰