Best Practices: Performance Tuning Guide for Building a Web Server on CentOS

王林
Release: 2023-08-04 12:17:04
Original
1256 people have browsed it

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
Copy after login

Choose to close unnecessary services according to the actual situation, such as turning off the mail service:

systemctl stop postfix systemctl disable postfix
Copy after login

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.conffile. The following are some commonly used kernel parameter tuning solutions:

  1. Increase the limit on the number of file handles
# 增加系统最大文件句柄数 fs.file-max = 65535 # 增加每个进程可以打开的文件句柄数限制 ulimit -n 65535
Copy after login
  1. Increase the limit on the number of TCP connections
# 增加系统的最大并发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
Copy after login
  1. Eliminate group chat fragmentation
# 消除群聊分片 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_sack = 0 net.ipv4.tcp_dsack = 0
Copy after login
  1. Increase the system TCP buffer size
# 提高TCP接收窗口缓冲区大小(单位:字节) net.ipv4.tcp_rmem = 4096 87380 4194304 # 提高TCP发送窗口缓冲区大小(单位:字节) net.ipv4.tcp_wmem = 4096 16384 4194304 # 提高系统的TCP连接跟踪表的大小 net.netfilter.nf_conntrack_max = 65536
Copy after login

After modifying the above parameters, use the following command to enable It takes effect:

sysctl -p
Copy after login

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.

  1. Nginx

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
Copy after login

Configure Nginx:

Edit/etc/nginx/nginx.confFile:

user www-data; worker_processes auto; worker_cpu_affinity auto; events { worker_connections 1024; use epoll; multi_accept on; } http { # 配置HTTP服务器 ... }
Copy after login
  1. Apache

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
Copy after login

Configure Apache:

Edit/etc/httpd/conf/httpd.confFile:

ServerLimit 2048 MaxClients 2048
Copy after login

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:

  1. HTTP Cache

By setting the appropriateCache-ControlandExpiresResponse 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; }
Copy after login
  1. Reverse proxy cache

Use reverse proxy cache to cache dynamic content and reduce requests to the backend server. You can use Nginx'sproxy_cachemodule 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; } }
Copy after login

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:

  1. https://www.digitalocean.com/community/tutorials/5-tips-to-speed-up-your-nginx-web-server
  2. https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!