分享一下面对百万次访问量怎么配置自己的服务器

系统层面

1、调整同时打开文件数量

1
2
3
4
vim /etc/security/limits.conf
##修改下列参数
* hard nofile 65536 //
* soft nofile 65536

2、其他

1
2
3
4
5
/etc/sysctl.conf
#系统所有进程一共可以打开的文件数量
fs.file-max = 6815744
#该参数设置系统的TIME_WAIT的数量,如果超过默认值则会被立即清除
net.ipv4.tcp_max_tw_buckets = 20000

nginx

1、nginx.conf

1
2
3
4
5
6
7
8
9
worker_processes  auto; //进程数 默认为1 调整为自动 或者调整为cpu的倍数
events {
worker_connections 20480; //每个进程允许的最多连接数, 根据服务器硬件配置调整
multi_accept on; //#nginx在已经得到一个新连接的通知时,接收尽可能多的连接
#use epoll;#使用epoll的I/O模型 具体看自己服务器的情况判断是否开启
}
http {
keepalive_timeout 65; 这里你也可以设置为0
}

2、负载均衡配置(即负载代理的站点)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
upstream elliot{
#ip_hash; //如果需要session建议开启
server 192.168.0.3:81 weight=4;
server 192.168.0.2:81 weight=4;
server 192.168.0.1:82 weight=2;
}
server{
listen 81;
server_name demo.com;
client_header_buffer_size 128k; //头部缓存
client_body_buffer_size 1m;
location / {
proxy_pass http://elliot;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X_Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffer_size 32k;
proxy_buffers 64 32k;
proxy_busy_buffers_size 1m;
proxy_temp_file_write_size 512k;
proxy_read_timeout 300; //响应时间
proxy_send_timeout 300;
}
}

3、站点.conf

1
2
3
4
5
6
7
8
9
10
11
12
server{
listen 82;
server_name _;
root /var/www/html/; //web根目录 即index.html或者index.php目录
client_header_buffer_size 128k;
client_body_buffer_size 1m;
location ~ \.php {
fastcgi_buffer_size 512k;
fastcgi_buffers 6 512k; //注意中间有空格 这里是2个参数
fastcgi_read_timeout 300;
}
}

MySQL配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
# Disabling symbolic-links is recommended to prevent assorted security risks
log-bin=/var/lib/mysql/mysql-bin //当主从时需要打开
symbolic-links=0

max_connections = 20480

max_connect_errors = 6000

table_open_cache = 614

external-locking = FALSE

binlog_format=ROW

max_allowed_packet =8M

sort_buffer_size = 1M

join_buffer_size = 1M

thread_cache_size = 800

query_cache_size = 2M

query_cache_limit = 1M

query_cache_min_res_unit = 2k

thread_stack = 192K

transaction_isolation = READ-COMMITTED

transaction_isolation = READ-COMMITTED

tmp_table_size = 2M

max_heap_table_size = 2M

long_query_time = 1

binlog_cache_size = 1M

max_binlog_cache_size = 1M

expire_logs_days = 7

key_buffer_size = 16M

read_buffer_size = 1M

read_rnd_buffer_size = 1M

bulk_insert_buffer_size = 1M

lower_case_table_names = 1

skip-name-resolve

slave-skip-errors = 1032,1062

replicate-ignore-db=mysql

server-id = 1 //主从配置时需要注意 同一局域网不要重复

innodb_additional_mem_pool_size = 4M

innodb_buffer_pool_size = 70G

innodb_file_io_threads = 4

innodb_thread_concurrency = 64

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 2M //根据实际情况配置

innodb_log_file_size = 4M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

PHP

1、php-fpm配置

1
2
3
4
5
[www] //配置前看好你的环境到底是谁来执行
pm = static;
pm.max_children = 8192; //最大进程数量 一般是1G=8
pm.max_requests = 40000;
rlimit_files = 10240; //这个和系统层面的ulimit一样

2、php.ini配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
post_max_size = 20M 

upload_max_filesize = 20M

max_execution_time = 300

memory_limit = -1

error_reporting = E_ALL

display_errors = On

log_errors = On

log_errors_max_len = 1024

error_log = /proc/self/fd/2

上述配置很多需要根据自己服务器来订