1、建立独立帐号并且授予的独立数据库的权限

mysql> create database bbs default charset utf8;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on bbs.* to bbs@'localhost' identified by '123';


2、配置nginx

设定网站根目录等
# vim nginx.conf
user  daemon;
worker_processes  4;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.upl.com;
       root   /www/www.upl.com/;
       index  index.html index.htm index.php;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ .*\.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
    }
}


启动
# php-fpm start
# ./nginx

 

3、解压论坛源代码到网站根目录
...
# mv upload/*   /www/www.upl.com/
# chown -R  daemon:daemon  /www/www.upl.com/


4、向导安装论坛

如果无法上外网,就必须把网关删除
# route del default gw 10.1.1.1

备注:无法连接数据库,也许是因为php.ini中的mysql的socket设定错误
# php-fpm reload

 

==================================

常用nginx的设定

# vim nginx.conf
user  daemon;
worker_processes  4;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';  ####


    server {
        listen       80;
        server_name  www.upl.com;
        root   /www/www.upl.com/;
 access_log  logs/www.upl.com.access.log  main; ####
        index  index.html index.htm index.php;
 error_page  404              /404.html;  ###
        error_page   500 502 503 504  /50x.html;
        location ~ .*\.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
            fastcgi_intercept_errors on;  # 让php也支持404报错
        }

        location = /status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            allow 10.1.1.22;
            deny all;
        } 

    }
}

 

=====================================================================
优化之前进行压力测试:


压力测试命令:
# ab -c 300 -n 6000 http://10.1.1.22/read.php?tid=1

# uptime

# iostat -x -d sda2 2
Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda2              0.00     6.50  0.00  7.00     0.00   108.00    15.43     0.01    1.21   0.14   0.10

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda2              0.00    32.50  0.00 90.00     0.00   980.00    10.89     0.04    0.39   0.39   3.55

总结:基本不会产生读IO,早已经被cache了
 偶尔会出现比较高的写IO,主要用来写访问日志。
  调优:把日志文件放在单独的分区,取消该分区的访问时间更新功能。

# vmstat 2

 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0  67576  56284 1420264    0    0   124    12 1019  999 27 16 57  0  0
 2  1      0  63360  56288 1420360    0    0     0    62 1698 3010 27  7 66  0  0
 2  1      0  61980  56304 1420604    0    0     0   657 2258 5190 55 14 29  1  0
 5  1      0  60756  56332 1420940    0    0     0  1979 2369 5158 56 15 28  1  0
 0  1      0  60168  56348 1421188    0    0     0  1756 2381 5200 61 16 22  2  0
 5  1      0  60324  56364 1421532    0    0     0  1062 2341 4790 71 17 11  1  0
10  1      0  59496  56380 1421732    0    0     0  1025 2187 4309 75 17  7  0  0
12  0      0  58844  56412 1422012    0    0     0   980 2273 4174 78 18  3  1  0
10  0      0  58732  56444 1422136    0    0     0  1059 2311 4004 79 18  2  1  0
 2  0      0  58484  56464 1422348    0    0     0   845 2038 3372 62 15 23  1  0
 0  0      0  58796  56480 1422624    0    0     0   173 1698 2287 24  7 69  0  0
 0  0      0  57732  56560 1422852    0    0    62  1531 1312 1116  9  3 76 11  0
 0  0      0  57920  56576 1422860    0    0     0   102 1051  390  1  1 98  0  0
 0  0      0  58092  56584 1422872    0    0     0     6 1029  286  0  0 100  0  0
 0  0      0  58492  56592 1422856    0    0     0    69 1205  602  3  1 97  0  0
 0  0      0  59052  56600 1422904    0    0     0    38 1133  454  1  0 98  0  0
 0  0      0  59432  56608 1422900    0    0     0     9 1108  490  2  0 98  0  0
 0  0      0  59760  56616 1422904    0    0     0    10 1043  278  0  0 99  0  0
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0  60496  56616 1422908    0    0     0   391 1176  470  1  0 97  1  0
 0  0      0  60668  56620 1422908    0    0     0    10 1020  299  1  0 99  0  0
 0  0      0  61136  56628 1422916    0    0     0    12 1025  405  2  0 98  0  0
 0  0      0  61532  56636 1422916    0    0     0     2 1074  398  1  0 99  0  0

 

# netstat -n | awk '/^tcp/{++S[$NF]}END{ for(a in S) print a,S[a]}'

 


http://10.1.1.22/status

Active connections: 301  《---
server accepts handled requests
  1726   1726    1900
Reading: 0 Writing: 301 Waiting: 0


压力测试的输出:
第一次:
Server Software:        nginx/0.8.54
Server Hostname:        10.1.1.22
Server Port:            80

Document Path:          /read.php?tid=1
Document Length:        87832 bytes

Concurrency Level:      300
Time taken for tests:   114.90295 seconds
Complete requests:      6000
Failed requests:        5773
   (Connect: 0, Length: 5773, Exceptions: 0)
Write errors:           0
Non-2xx responses:      338
Total transferred:      114082811 bytes
HTML transferred:       112229819 bytes
Requests per second:    52.59 [#/sec] (mean) ##
Time per request:       5704.515 [ms] (mean)
Time per request:       19.015 [ms] (mean, across all concurrent requests)
Transfer rate:          976.49 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   6.8      0     111
Processing:     2 2823 10846.5      5   93019
Waiting:        2 2815 10847.7      4   93018
Total:          2 2825 10849.2      5   93029

Percentage of the requests served within a certain time (ms)
  50%      5
  66%     44
  75%    141
  80%    277
  90%   3124
  95%  21033
  98%  45024
  99%  60020
 100%  93029 (longest request)

 

第二次:
Server Software:        nginx/0.8.54
Server Hostname:        10.1.1.22
Server Port:            80

Document Path:          /read.php?tid=1
Document Length:        87851 bytes

Concurrency Level:      300
Time taken for tests:   114.40618 seconds
Complete requests:      6000
Failed requests:        4739
   (Connect: 0, Length: 4739, Exceptions: 0)
Write errors:           0
Non-2xx responses:      445
Total transferred:      338417423 bytes
HTML transferred:       335611793 bytes
Requests per second:    52.61 [#/sec] (mean)  ##
Time per request:       5702.031 [ms] (mean)
Time per request:       19.007 [ms] (mean, across all concurrent requests) ##
Transfer rate:          2897.96 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.9      1      16
Processing:     2 2913 9564.1     39   93003
Waiting:        1 2901 9566.3     22   93002
Total:          2 2914 9565.6     40   93008

Percentage of the requests served within a certain time (ms)
  50%     40
  66%    106
  75%    185
  80%   3002
  90%   9007
  95%  21021
  98%  29798
  99%  53613
 100%  93008 (longest request)

 

 

nginx  0.7% * 2000 * 4 = 60M
php-cgi 0.4% * 2000 * 32 = 320M <---一般来说,如果运行比较复杂一点php代码,一个进程大约占用20M内存, 应该会好点 640M内存


把并发量调整大一些,并且多个客户端同时压力测试
# ab -c 1000 -n 6000 http://10.1.1.22/read.php?tid=1

 

Active connections: 10029
server accepts handled requests
 88404 88404 108453
Reading: 0 Writing: 10003 Waiting: 26

Active connections: 15320
server accepts handled requests
 164120 164120 190192
Reading: 0 Writing: 15261 Waiting: 59   《--反问已经慢了,偶尔会出现503报错


# vmstat 2
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 4  0      4  38508  75420 1184744    0    0   109    27  981  963 25 14 60  0  0
 8  0      4  38092  75432 1184924    0    0     0   574 2622 2462 63 23 14  0  0
 1  0      4  39500  75432 1183228    0    0     0   310 2206 2334 71 22  6  0  0
 2  0      4  39424  75432 1183648    0    0     0     0 2236 2351 69 23  8  0  0
13  0      4  39424  75440 1184024    0    0     0   464 2178 2457 67 21 12  0  0
 3  0      4  39952  75440 1184472    0    0     0     0 2102 2310 69 22  9  0  0
11  0      4  39952  75444 1184784    0    0     0   572 2364 2428 70 24  7  0  0
12  0      4  41180  75448 1183428    0    0     0     0 2285 2521 68 22 10  0  0


总结:高并发的时候,主要是cpu不足,都是消耗在php-cgi进程对代码的编译上。


===========================================
进行部分参数的调优:
user  daemon;
worker_processes  4;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
worker_rlimit_nofile 204800;
events {
    use epoll;
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 4k;
        large_client_header_buffers 8 4k;
        client_max_body_size 8m;
        sendfile on;
        tcp_nopush on;
        keepalive_timeout 3;
        open_file_cache max=204800 inactive=20s;
        open_file_cache_min_uses 1;
        open_file_cache_valid 30s;

        gzip on;  ### 高并发,打开了服务器更慢,处理请求更少,cpu更忙
        gzip_min_length 1k;
        gzip_buffers 16 4k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/x-javascript text/css application/xml;
        gzip_vary on;


 # 如果网站大多数的php页面都是不能缓存的页面,建议关闭下面的fastcgi的设定
        fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=2:2 keys_zone=phpcache:30m inactive=5m;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 4k;
        fastcgi_buffers 64 4k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 128k;
        fastcgi_cache phpcache;
        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;

 

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server {
        listen       80;
        server_name  www.upl.com;
        access_log  logs/www.upl.com.access.log  main;
        root   /www/www.upl.com/;
        index  index.html index.htm index.php;
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location ~ .*\.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
            fastcgi_intercept_errors on;
        }
        location = /status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            allow 10.1.1.22;
            deny all;
        }
                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)?$ {
                        expires 7d;
                }
                location ~ .*\.(js|css)?$ {
                        expires 1d;
                }
    }
}

 

内核参数的调优:
# vim /etc/sysctl.conf
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_max_syn_backlog = 262144 
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.tcp_window_scaling = 1

# sysctl -p


=====================================================================


Active connections: 23380
server accepts handled requests
 218720 218720 229577
Reading: 1 Writing: 23354 Waiting: 25

 

 

# ab -c 1800 -n 10000 http://10.1.1.22/test.php