Bài viết hướng dẫn tối ưu hóa máy chủ cho trang web có lưu lượng truy cập cao, để phát huy hết sức mạnh của máy chủ trước khi tính đến phương án nâng cấp.
Cấu hình này dành cho máy chủ có sức mạnh trung bình trở lên (từ 4 CPU và 8 GB RAM trở lên), các bạn có thể tự mình tinh chỉnh lại cho hợp lý hơn.
Tối ưu Nginx
Sửa tập tin /etc/nginx/nginx.conf
.
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; worker_rlimit_nofile 100000; events { worker_connections 4096; use epoll; multi_accept on; } http { 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 /var/log/nginx/access.log main; open_file_cache max=200000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 32; keepalive_requests 100000; reset_timedout_connection on; gzip on; gzip_vary on; gzip_disable "MSIE [1-6]\."; gzip_static on; gzip_min_length 1400; gzip_buffers 32 8k; gzip_http_version 1.0; gzip_comp_level 5; gzip_proxied any; gzip_types text/plain text/css text/xml application/javascript application/x-javascript application/xml application/xml+rss application/ecmascript application/json image/svg+xml; client_body_buffer_size 256k; client_body_in_file_only off; client_body_timeout 5s; client_header_timeout 5s; send_timeout 10s; client_header_buffer_size 64k; large_client_header_buffers 4 256k; client_max_body_size 128m; connection_pool_size 512; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; fastcgi_buffering on; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; }
Sau đó khởi động lại Nginx.
systemctl restart nginx.service
Tối ưu PHP
Sửa tập tin etc/php.ini
.
memory_limit = 8192M
Sửa tập tin /etc/php-fpm.d/www.conf
.
pm = dynamic pm.max_children = 12 pm.start_servers = 5 pm.min_spare_servers = 3 pm.max_spare_servers = 9
Khởi động lại PHP-FPM:
systemctl restart php-fpm.service
Tối ưu MariaDB
Sửa tập tin /etc/my.cnf
, thêm nội dung sau.
[mysqld] key_buffer = 2048M sort_buffer_size = 16M read_buffer_size = 16M join_buffer_size = 64M table_cache = 8000 read_rnd_buffer_size = 16M thread_concurrency = 16 read_buffer_size = 32M tmp_table_size = 1024M max_heap_table_size = 1024M myisam_sort_buffer_size = 256M max_connections = 5000 max_allowed_packet = 1024M query_cache_limit = 16M query_cache_size = 1024M query_cache_type = 1 interactive_timeout = 300 wait_timeout = 600 connect_timeout = 60 thread_cache_size = 128 innodb_buffer_pool_size = 2048M innodb_flush_log_at_trx_commit = 0 innodb_flush_method = O_DIRECT
Nhớ khởi động lại MariaDB:
systemctl restart mariadb.service
Memcached
Nếu bạn có sử dụng Memcached, sửa tập tin /etc/sysconfig/memcached
.
PORT="11211" USER="memcached" MAXCONN="163840" CACHESIZE="8192" OPTIONS="-l 127.0.0.1 -U 0"
Khởi động lại Memcached.
systemctl restart memcached.service
Chúc các bạn thành công!