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
- Profile Your Code: Use tools like XHProf or Blackfire to identify bottlenecks, focusing on frequently executed paths.
- Reduce System Calls: Avoid functions like
is_file()ortime()inside loops. Userequire/includeinstead ofrequire_once/include_onceif you can guarantee no duplicate loading. - 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.
- Configure OPcache: Enable and properly configure PHP's OPcache. In production, set
opcache.revalidate_freq=0oropcache.validate_timestamps=0to reduce filesystem checks. - Trim PHP Extensions: Recompile PHP or use your package manager to enable only necessary extensions (e.g., disable unused ones like gd, soap).
- Optimize php.ini:
- Set timezone (
date.timezone) and memory limits globally inphp.inito avoid runtimeini_set()calls. - In production, turn off
display_errorsand enablelog_errors.
- Set timezone (
PHP-FPM Configuration
- Process Management: Set
pm.max_childrenbased on server memory and load. Monitor with tools liketopto adjust. - Use Unix Socket: Configure PHP-FPM to listen via Unix Socket (
listen = /var/run/php/php-fpm.sock) to reduce network overhead vs. TCP. - Resource Limits: Ensure
rlimit_filesmatches the system's file descriptor limit.
Nginx Configuration
- Worker Processes: Set
worker_processesto the number of CPU cores. Increase slightly for I/O-heavy workloads. - Event Model: Use
epollon Linux orkqueueon FreeBSD. - Keep-alive: Enable and set a reasonable
keepalive_timeout(e.g., 65s) for connection reuse. - Gzip Compression: Enable
gzipand setgzip_min_length 1024to avoid compressing tiny files. - Log Optimization: Consider disabling
access_login production or writing to a memory-based filesystem. Use log rotation if needed. - Static File Handling:
- Enable
sendfile. - Set
expiresheaders for static resources to leverage browser caching.
- Enable
- File Descriptors: Set
worker_rlimit_nofileandworker_connectionsto match system limits.
System & Kernel Tuning
- Disable Unnecessary Services: Use
systemctlto disable unused services (e.g., sendmail, bluetooth). - Increase File Descriptor Limits: Add for nginx/php-fpm users in
/etc/security/limits.conf:* soft nofile 65535 * hard nofile 65535 - Disk Mount Options: Add
noatimeto/etc/fstabto reduce access time updates. - Local DNS Resolution: Add frequent domain/IP mappings to
/etc/hoststo reduce DNS lookup latency. - Kernel Network Parameters: Add/modify in
/etc/sysctl.conf, then runsysctl -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
enforcingmode 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.