插件窝 干货文章 CentOS 7下搭建web服务器的最佳实践及注意事项

CentOS 7下搭建web服务器的最佳实践及注意事项

sudo systemctl Nginx 配置 163    来源:    2025-04-12

CentOS 7下搭建Web服务器最佳实践及注意事项

一、系统准备与安全配置

1. 系统更新与基础配置

# 更新系统
sudo yum update -y

# 安装常用工具
sudo yum install -y wget curl vim net-tools epel-release

# 设置时区
sudo timedatectl set-timezone Asia/Shanghai

# 关闭不必要的服务
sudo systemctl disable postfix

2. 安全加固

# 配置防火墙
sudo systemctl enable firewalld
sudo systemctl start firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

# 安装并配置fail2ban
sudo yum install -y fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

二、Web服务器选择与安装

1. Apache (httpd)

# 安装Apache
sudo yum install -y httpd

# 启动并设置开机自启
sudo systemctl enable httpd
sudo systemctl start httpd

# 配置目录权限
sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html

2. Nginx

# 安装Nginx
sudo yum install -y nginx

# 启动并设置开机自启
sudo systemctl enable nginx
sudo systemctl start nginx

三、数据库安装与配置

1. MySQL/MariaDB

# 安装MariaDB
sudo yum install -y mariadb-server mariadb

# 启动并设置开机自启
sudo systemctl enable mariadb
sudo systemctl start mariadb

# 运行安全配置脚本
sudo mysql_secure_installation

2. PostgreSQL

# 安装PostgreSQL
sudo yum install -y postgresql-server postgresql-contrib

# 初始化数据库
sudo postgresql-setup initdb

# 启动并设置开机自启
sudo systemctl enable postgresql
sudo systemctl start postgresql

四、PHP安装与配置

# 安装PHP及相关模块
sudo yum install -y php php-mysql php-fpm php-gd php-mbstring php-xml php-pear

# 配置PHP-FPM (如果使用Nginx)
sudo systemctl enable php-fpm
sudo systemctl start php-fpm

五、SSL证书配置

使用Let's Encrypt免费证书

# 安装certbot
sudo yum install -y certbot python2-certbot-nginx  # 对于Nginx
# 或
sudo yum install -y certbot python2-certbot-apache # 对于Apache

# 获取证书
sudo certbot --nginx   # 或 --apache
# 设置自动续期
echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" | sudo tee -a /etc/crontab > /dev/null

六、性能优化

1. Apache优化

# 编辑/etc/httpd/conf/httpd.conf
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

<IfModule prefork.c>
    StartServers        8
    MinSpareServers     5
    MaxSpareServers     20
    ServerLimit         256
    MaxClients          256
    MaxRequestsPerChild 4000
</IfModule>

2. Nginx优化

# 编辑/etc/nginx/nginx.conf
worker_processes auto;
worker_rlimit_nofile 100000;

events {
    worker_connections 4000;
    use epoll;
    multi_accept on;
}

http {
    keepalive_timeout 10;
    keepalive_requests 100000;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
}

3. PHP-FPM优化

# 编辑/etc/php-fpm.d/www.conf
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500

七、监控与日志管理

1. 安装并配置GoAccess (实时日志分析)

sudo yum install -y goaccess

# 生成HTML报告
goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED

2. 配置日志轮转

# 编辑/etc/logrotate.d/nginx
/var/log/nginx/*log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 640 nginx adm
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

八、备份策略

1. 创建备份脚本

#!/bin/bash
# 备份网站文件和数据库
DATE=$(date +%Y%m%d)
BACKUP_DIR="/backups"
WEB_DIR="/var/www/html"
DB_USER="root"
DB_PASS="yourpassword"

mkdir -p $BACKUP_DIR/$DATE

# 备份网站文件
tar -czf $BACKUP_DIR/$DATE/web_backup.tar.gz $WEB_DIR

# 备份MySQL数据库
mysqldump -u$DB_USER -p$DB_PASS --all-databases > $BACKUP_DIR/$DATE/db_backup.sql

# 保留最近7天的备份
find $BACKUP_DIR -type d -mtime +7 -exec rm -rf {} \;

九、注意事项

  1. 安全更新:定期执行yum update并关注安全公告
  2. 最小权限原则:Web应用运行用户应具有最小必要权限
  3. SELinux:了解基本操作或设置为宽容模式(不推荐生产环境) bash sudo setenforce 0 # 临时设置为宽容模式 sudo vim /etc/selinux/config # 永久设置
  4. 定期检查:监控服务器资源使用情况,检查异常进程
  5. 备份验证:定期验证备份的完整性和可恢复性
  6. 禁用root远程登录:修改SSH配置 bash sudo vim /etc/ssh/sshd_config # 修改以下参数 PermitRootLogin no PasswordAuthentication no # 推荐使用密钥认证
  7. 文件权限:确保网站目录权限设置合理,上传目录不可执行

通过以上步骤和注意事项,您可以在CentOS 7上搭建一个安全、高效的Web服务器环境。根据实际需求,您可能需要调整某些配置参数以获得最佳性能。