MySQL,PHP-FPM,APC,Varnish和Nginx优化WordPress / W3总caching?

我正在build立我的第一个VPS,它似乎运作良好。 在Ubuntu 12.04服务器上安装了Nginx,php-fpm(作为unix套接字),APC,Varnish和MySQL以及OnApp,并且一切正常,并且速度非常快,至less在我的最后。

Atm我有1个核心的VPS(Xeon(R)X5660是VPS使用的iirc),1.2GHz和768MB RAM,一切都受限于OnApp。 做一个abtesting,我得到这个:

ab -c 10 -n 1000 http://198.136.50.39/ This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 198.136.50.39 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: Server Hostname: 198.136.50.39 Server Port: 80 Document Path: / Document Length: 6482 bytes Concurrency Level: 10 Time taken for tests: 41.695 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 6952000 bytes HTML transferred: 6482000 bytes Requests per second: 23.98 [#/sec] (mean) Time per request: 416.946 [ms] (mean) Time per request: 41.695 [ms] (mean, across all concurrent requests) Transfer rate: 162.83 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 167 203 57.3 182 614 Processing: 173 212 82.9 189 2015 Waiting: 169 206 59.8 185 726 Total: 345 415 126.5 373 2419 Percentage of the requests served within a certain time (ms) 50% 373 66% 388 75% 410 80% 430 90% 504 95% 708 98% 866 99% 931 100% 2419 (longest request) 

当我做了testing的时候,我正在用htop来看看VPS的统计数据,而且好像在整个testing中没有使用超过230MB的内存,CPU的使用率保持在2〜4%,我猜。 但是似乎每秒的请求量还是比较低的。 你们有什么感想? 它看起来不错,我有设置,我是偏执狂,还是不好? 用默认的设置,我用loadimpact.com来看看,有25个用户(默认免费testing)和默认加载为130毫秒…后,我开始搞乱了设置,再次做了testing,它跳到250ms,所以我猜我在做错事。

我开始尝试为它优化MySQL,使用互联网上find的tutoriais低端的框和https://tools.percona.com/ 。 Percona给了我一些非常大的数字,所以我做了两个组合。

我还优化了php-fpm和Nginx,在互联网上阅读wiki和教程。 我将使用这个VPS的一个WordPress网站每天约5K的访问者和13〜15K的每日浏览量。 W3总caching设置为使用APC执行数据库和对象caching,并使用磁盘增强function缩小/页面caching…但是在将网站迁移到此服务器并使用它之前,我想优化所有内容并确保它快点。

我也使用MaxCDN(在VPS atm上不活跃),并将使用CloudFlare作为DNS服务器。 任何人都可以帮我优化一下吗?

我的MySQLconfiguration看起来像这样的atm:

 [mysqld_safe] open_files_limit = 8192 [mysqld] skip_external_locking skip_slave_start bind-address = 127.0.0.1 key_buffer = 64M join_buffer_size = 1M read_buffer_size = 1M sort_buffer_size = 2M max_allowed_packet = 16M max_connect_errors = 10 thread_stack = 192K myisam-recover = BACKUP max_connections = 400 table_cache = 1024 thread_cache_size = 286 interactive_timeout = 25 wait_timeout = 1000 query_cache_type = 1 query_cache_limit = 1M query_cache_size = 32M max_write_lock_count = 1 expire_logs_days = 10 max_binlog_size = 100M innodb_flush_method = O_DIRECT innodb_buffer_pool_size = 10M skip_name_resolve sql_mode = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY tmp_table_size = 16M max_heap_table_size = 16M [mysqldump] quick quote-names max_allowed_packet = 16M [mysql] #no-auto-rehash # faster start of mysql but no tab completition [isamchk] key_buffer = 16M 

我的Nginxconfiguration如下所示:

 worker_processes 1; events { worker_connections 1024; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 15; types_hash_max_size 2048; server_tokens off; open_file_cache max=1000 inactive=300s; open_file_cache_valid 360s; open_file_cache_min_uses 2; open_file_cache_errors off; client_body_buffer_size 8K; client_header_buffer_size 1k; client_max_body_size 2m; large_client_header_buffers 2 1k; client_body_timeout 10; client_header_timeout 10; send_timeout 10; include /etc/nginx/mime.types; default_type application/octet-stream; error_log /var/log/nginx/error.log; gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 9; gzip_buffers 16 8k; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 

我的网站的Nginxconfiguration如下所示:

 server { listen 8080; server_name www.ubuntubrsc.com ubuntubrsc.com; root /var/www; index index.php index.html index.htm; include /var/www/nginx.conf; error_log /var/log/nginx/blog.error_log; if ($host ~* ^[^.]+\.[^.]+$) { rewrite ^(.*)$ http://www.$host$1 permanent; } location / { try_files $uri $uri/ /index.php; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } location ~ /\. { deny all; access_log off; log_not_found off; } location ~* ^/wp-content/uploads/.*.php$ { deny all; access_log off; log_not_found off; } rewrite /wp-admin$ $scheme://$host$uri/ permanent; location ~ \.(css|js|htc)$ { root /var/www/ubuntu-online/; expires 31536000s; add_header Pragma "public"; add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; } location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ { root /var/www/ubuntu-online/; expires 3600s; add_header Pragma "public"; add_header Cache-Control "max-age=3600, public, must-revalidate, proxy-revalidate"; } location ~ \.(gif|gz|gzip|ico|jpg|jpeg|jpe|swf)$ { root /var/www/ubuntu-online/; expires 31536000s; add_header Pragma "public"; add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; } error_page 404 = @wordpress; log_not_found off; location @wordpress { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_param SCRIPT_NAME /index.php; fastcgi_param SCRIPT_FILENAME $document_root/index.php; } location ~ \.php$ { try_files $uri =404; include /etc/nginx/fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; if (-f $request_filename) { fastcgi_pass unix:/var/run/php5-fpm.sock; } } } 

ubuntu-online的caching部分是因为我不知道W3 Total Cache的设置是否适用于/ www / var /中的所有文件夹,所以我添加了“root / var / www / ubuntu-online /”当然。 我应该抹去它们吗?

在php.ini我编辑了一些东西,以提高安全性,如open_basedir和其他的东西,也启用了PHP内部caching编辑2行,但不记得是什么。

此外,这些是APC设置:

 [APC] apc.enabled = 1 apc.cache_by_default = 1 apc.stat = 1 apc.shm_segments = 1 apc.shm_size = 64 apc.ttl = 7200 

最后,我的php-fpm池:

 listen = /var/run/php5-fpm.sock listen.owner = www-data listen.group = www-data listen.mode = 0666 pm.max_children = 9 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 4 pm.max_requests = 200 

另外,我如何知道Varnish正在使用WordPress? 我知道我必须做一些特定的configuration清漆和WP发挥得很好,我确实按照这个https://github.com/nicolargo/varnish-nginx-wordpress ,但我怎么知道它的工作?

感谢先进的每个人(:

你有很好的负载设置。 20rps到PHP脚本足以支持每日20万次的浏览量。

为你调整的是: innodb_buffer_pool_size = 10M – 这是相当小的价值。 innodb中的所有活动数据都应该适合缓冲池,为事务日志留下足够的空间。

worker_connections 1024; – 你可以用这个很快的 我build议你使用stubstatus模块检查实际负载下的nginx连接,并在开始的几天内调整你的服务的运行。

你也可以增加php并发性pm.max_children = 9 – 使用额外的RAM和CPU。 如果你在完整的tcp backlog( ss -nl

如果你有很高的请求率和/或你的脚本足够慢,你可以点击max_children限制。 如果你的脚本是cpu-bound,增加最大工作线程/进程将会增加Load Average。

让我告诉你基本的和近似的math。 你有100ms的脚本,使用5ms的CPU时间(100ms是磁盘/networkingIO等待) – 平均。

如果每秒超过9 * 1000/100 = 90个请求,你的tcp积压将会开始增长,新的请求将会等待一段时间直到启动。

您的脚本将从单个CPU内核消耗90 * 10/1000 = 45%cpu用户时间。 不多,不是吗?

如果将max_children增加到15,那么每秒可能会有150个请求没有减速,但是脚本可能会消耗75%的单核CPU时间。

这很好,直到你有太多的负载。 如果您没有足够的CPU或RAM来处理所select的并发 – 您的服务器将达到Load average – 这意味着由于CPU拥塞,脚本将会减慢。 加载服务器平均值大约是CPU核心数量的2-4倍通常足够响应。 处理请求会稍微慢一些,但是您可以处理更高的请求率。 如果你没有足够的内存 – 服务器将开始交换,加载磁盘和限制CPU。

所以,max_children太less – 你不会处理高请求率。 太多了 – 你的服务器将在高负载下挂起。