Blog / Linux/ Complete LNMP Performance Optimization Guide: From Application to System Kernel

Complete LNMP Performance Optimization Guide: From Application to System Kernel

LNMP 环境全方位性能优化指南:从应用到系统内核

LNMP Performance Optimization Guide

This guide provides a comprehensive approach to optimizing a Linux, Nginx, MySQL, and PHP (LNMP) stack. It covers application code, service configuration, and system-level tuning. Always prioritize stability and security alongside performance gains.

PHP Application Optimization

  1. Profile Your Code: Use tools like XHProf or Blackfire to identify bottlenecks, focusing on frequently executed paths.
  2. Reduce System Calls: Avoid functions like is_file() or time() inside loops. Use require/include instead of require_once/include_once if you can guarantee no duplicate loading.
  3. Implement Caching:
    • Use Memcached or Redis for system configs, module data, and user sessions.
    • Enable MySQL slow query logging to analyze and optimize inefficient SQL.
  4. Configure OPcache: Enable and properly configure PHP's OPcache. In production, set opcache.revalidate_freq=0 or opcache.validate_timestamps=0 to reduce filesystem checks.
  5. Trim PHP Extensions: Recompile PHP or use your package manager to enable only necessary extensions (e.g., disable unused ones like gd, soap).
  6. Optimize php.ini:
    • Set timezone (date.timezone) and memory limits globally in php.ini to avoid runtime ini_set() calls.
    • In production, turn off display_errors and enable log_errors.

PHP-FPM Configuration

  1. Process Management: Set pm.max_children based on server memory and load. Monitor with tools like top to adjust.
  2. Use Unix Socket: Configure PHP-FPM to listen via Unix Socket (listen = /var/run/php/php-fpm.sock) to reduce network overhead vs. TCP.
  3. Resource Limits: Ensure rlimit_files matches the system's file descriptor limit.

Nginx Configuration

  1. Worker Processes: Set worker_processes to the number of CPU cores. Increase slightly for I/O-heavy workloads.
  2. Event Model: Use epoll on Linux or kqueue on FreeBSD.
  3. Keep-alive: Enable and set a reasonable keepalive_timeout (e.g., 65s) for connection reuse.
  4. Gzip Compression: Enable gzip and set gzip_min_length 1024 to avoid compressing tiny files.
  5. Log Optimization: Consider disabling access_log in production or writing to a memory-based filesystem. Use log rotation if needed.
  6. Static File Handling:
    • Enable sendfile.
    • Set expires headers for static resources to leverage browser caching.
  7. File Descriptors: Set worker_rlimit_nofile and worker_connections to match system limits.

System & Kernel Tuning

  1. Disable Unnecessary Services: Use systemctl to disable unused services (e.g., sendmail, bluetooth).
  2. Increase File Descriptor Limits: Add for nginx/php-fpm users in /etc/security/limits.conf:
    * soft nofile 65535
    * hard nofile 65535
  3. Disk Mount Options: Add noatime to /etc/fstab to reduce access time updates.
  4. Local DNS Resolution: Add frequent domain/IP mappings to /etc/hosts to reduce DNS lookup latency.
  5. Kernel Network Parameters: Add/modify in /etc/sysctl.conf, then run sysctl -p:
    net.ipv4.tcp_max_syn_backlog = 65536
    net.core.netdev_max_backlog = 32768
    net.core.somaxconn = 32768
    net.core.wmem_default = 8388608
    net.core.rmem_default = 8388608
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216
    net.ipv4.tcp_tw_reuse = 1
    # Note: net.ipv4.tcp_tw_recycle can cause issues with NAT; keep disabled.
    # net.ipv4.tcp_tw_recycle = 0
    net.ipv4.ip_local_port_range = 1024 65535

Security & Monitoring

  • Firewall: Do not disable firewalls (iptables/firewalld). Configure minimal necessary rules.
  • SELinux: Keep in enforcing mode in production. Adjust policy via audit logs instead of disabling.
  • Performance Monitoring: Conduct load tests (with ab, wrk, siege) before/after changes. Use monitoring (e.g., Prometheus+Grafana) to track TPS, response times, and resource usage.

Summary: Performance optimization is a systematic process involving code, service configuration, OS, and hardware. Always test thoroughly after changes to ensure system stability and functionality.

Post a Comment

Your email will not be published. Required fields are marked with *.