Home >Operation and Maintenance >Linux Operation and Maintenance >Best Practices: Performance Tuning Guide for Building a Web Server on CentOS
Best Practices: Performance Tuning Guide for Building Web Servers on CentOS
Abstract: This article aims to provide some performance tuning best practices for users building web servers on CentOS, aiming to improve the performance of the server. and response speed. Some key tuning parameters and commonly used optimization methods will be introduced, and some sample codes will be provided to help readers better understand and apply these methods.
1. Turn off unnecessary services
When building a web server on CentOS, some unnecessary services will be started by default. These services will occupy system resources and have no obvious impact on the performance of the web server. promote. Therefore, we should shut down these unnecessary services to free up resources.
Use the following command to list the running services:
systemctl list-units --type=service --state=running
Choose to close unnecessary services according to the actual situation, such as turning off the mail service:
systemctl stop postfix systemctl disable postfix
2. Adjust the kernel parameters
Optimizing kernel parameters is an important step in improving server performance. Kernel parameters can be adjusted by modifying the /etc/sysctl.conf
file. The following are some commonly used kernel parameter tuning solutions:
# 增加系统最大文件句柄数 fs.file-max = 65535 # 增加每个进程可以打开的文件句柄数限制 ulimit -n 65535
# 增加系统的最大并发TCP连接数 net.ipv4.ip_local_port_range = 1024 65535 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_max_syn_backlog = 1024 net.core.somaxconn = 65535
# 消除群聊分片 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_sack = 0 net.ipv4.tcp_dsack = 0
# 提高TCP接收窗口缓冲区大小(单位:字节) net.ipv4.tcp_rmem = 4096 87380 4194304 # 提高TCP发送窗口缓冲区大小(单位:字节) net.ipv4.tcp_wmem = 4096 16384 4194304 # 提高系统的TCP连接跟踪表的大小 net.netfilter.nf_conntrack_max = 65536
After modifying the above parameters, use the following command to enable It takes effect:
sysctl -p
3. Use a high-performance web server
Choosing an appropriate web server also has an important impact on performance. On CentOS, Nginx and Apache are commonly used web servers.
Nginx is a high-performance HTTP and reverse proxy server that uses an asynchronous non-blocking event-driven architecture to easily handle large traffic requests.
Install Nginx:
yum install nginx
Configure Nginx:
Edit /etc/nginx/nginx.conf
File:
user www-data; worker_processes auto; worker_cpu_affinity auto; events { worker_connections 1024; use epoll; multi_accept on; } http { # 配置HTTP服务器 ... }
Apache is a feature-rich and widely used web server. Although its performance is slightly inferior to Nginx, it can still provide good performance in some specific scenarios.
Install Apache:
yum install httpd
Configure Apache:
Edit /etc/httpd/conf/httpd.conf
File:
ServerLimit 2048 MaxClients 2048
4. Use cache acceleration
Using caching technology can effectively improve the performance of the web server. The following two methods can be used for cache acceleration:
By setting the appropriate Cache-Control
and Expires
Response header allows the client to cache static resources, reduce the load on the server, and improve the user's access experience. The sample code is as follows:
location /static { expires 7d; }
Use reverse proxy cache to cache dynamic content and reduce requests to the backend server. You can use Nginx's proxy_cache
module to implement reverse proxy caching. The sample code is as follows:
proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m; server { ... location / { proxy_cache my_cache; proxy_cache_valid 200 301 302 5m; proxy_pass http://backend; } }
Conclusion
By closing unnecessary services, adjusting kernel parameters, using high-performance web servers and using cache acceleration, the web server built on CentOS can be better Good performance and responsiveness. I hope the performance tuning guide provided in this article will be helpful to you.
Reference link:
The above is the detailed content of Best Practices: Performance Tuning Guide for Building a Web Server on CentOS. For more information, please follow other related articles on the PHP Chinese website!