在CentOS上搭建Web服务器需要合理规划硬件配置并安装必要的软件组件。以下是一份详细的指南:
基础型(个人博客/小型企业站)
中型站点(日均10万PV)
高负载站点(电商/大型门户)
基础栈
# LEMP环境安装示例
yum install -y epel-release
yum install -y nginx mariadb-server php-fpm php-mysqlnd
安全组件
yum install -y policycoreutils-python setroubleshoot
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
性能优化工具
yum install -y redis varnish
yum install -y nagios zabbix-agent
Web服务器选择
# 示例worker配置
worker_processes auto;
worker_rlimit_nofile 100000;
events {
worker_connections 4096;
use epoll;
}
<IfModule prefork.c>
StartServers 10
MinSpareServers 10
MaxSpareServers 20
ServerLimit 256
MaxClients 256
</IfModule>
数据库优化
# /etc/my.cnf.d/server.cnf
[mysqld]
innodb_buffer_pool_size = 4G
innodb_log_file_size = 256M
query_cache_size = 64M
安全加固:
# 检查开放端口
ss -tulnp
# 更新系统
yum update -y && reboot
压力测试:
ab -n 10000 -c 100 http://yourserver.com/
日志分析配置:
yum install -y goaccess
goaccess /var/log/nginx/access.log -o /var/www/html/report.html
阿里云/ AWS需要额外配置:
容器化部署建议:
# Podman示例
podman run -d -p 80:80 -v ./html:/usr/share/nginx/html nginx
建议根据实际业务需求进行调优,生产环境务必做好: 1. 自动化备份方案(如borgmatic) 2. 监控告警系统(Prometheus+Alertmanager) 3. CI/CD管道配置(Jenkins/GitLab CI)
需要更具体的配置方案可提供您的具体应用场景(如WordPress/Django等),我可以给出针对性建议。