Troubleshooting and Fixing 100% CPU Utilization for Apache on CentOS
Apache Initial Configuration
Navigate to /usr/local/apache/conf/extra/httpd-default.conf and ensure KeepAlive is toggled to On to reduce repeated TCP handshakes for persistent connections.
Linux Kernel Network and Resource Tuning
Append the following entries to /etc/sysctl.conf to adjust TCP handling and system-level limits. After saving, apply changes with sysctl -p:
net.ipv4.tcp_max_tw_buckets = 22000
net.core.somaxconn = 65536
net.ipv4.tcp_max_syn_backlog = 270000
net.core.netdev_max_backlog = 32000
net.ipv4.tcp_tw_recycle = 0
fs.file-max = 7000000
net.netfilter.nf_conntrack_max = 2700000
Note that the netfilter parameter may throw an error if iptables/nftables connection tracking is disabled; this can be ignored safely.
Persistent File Descriptor Limits
Elevate the maximum open file handles per process and system-wide to resolve high-concurrency too many open files errors, which often spike Apache CPU usage as it struggles to manage connection overhead.
Temporary fix (current terminal only):
ulimit -HSn 1048576
User-session persistent fix (append to /etc/profile):
ulimit -HSn 1048576
System-wide permanent fix (requires reboot; add to /etc/security/limits.conf):
* soft nofile 1048576
* hard nofile 1048576
root soft nofile 1048576
root hard nofile 1048576
Basic MySQL Connection Tuning
High CPU can also stem from MySQL bottlenecks limiting Apache worker release. First check current connection limits:
SHOW VARIABLES LIKE 'max_conn%';
Dynamically adjust limits without restarting MySQL (values reset on reboot):
SET GLOBAL max_connections = 11000;
SET GLOBAL max_connect_errors = 5500;
For persistence, add these to my.cnf or my.ini under the [mysqld] section.