# 更新系统
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
# 配置防火墙
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
# 安装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
# 安装Nginx
sudo yum install -y nginx
# 启动并设置开机自启
sudo systemctl enable nginx
sudo systemctl start nginx
# 安装MariaDB
sudo yum install -y mariadb-server mariadb
# 启动并设置开机自启
sudo systemctl enable mariadb
sudo systemctl start mariadb
# 运行安全配置脚本
sudo mysql_secure_installation
# 安装PostgreSQL
sudo yum install -y postgresql-server postgresql-contrib
# 初始化数据库
sudo postgresql-setup initdb
# 启动并设置开机自启
sudo systemctl enable postgresql
sudo systemctl start postgresql
# 安装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
# 安装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
# 编辑/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>
# 编辑/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;
}
# 编辑/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
sudo yum install -y goaccess
# 生成HTML报告
goaccess /var/log/nginx/access.log -o /var/www/html/report.html --log-format=COMBINED
# 编辑/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
}
#!/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 {} \;
yum update
并关注安全公告bash
sudo setenforce 0 # 临时设置为宽容模式
sudo vim /etc/selinux/config # 永久设置
bash
sudo vim /etc/ssh/sshd_config
# 修改以下参数
PermitRootLogin no
PasswordAuthentication no # 推荐使用密钥认证
通过以上步骤和注意事项,您可以在CentOS 7上搭建一个安全、高效的Web服务器环境。根据实际需求,您可能需要调整某些配置参数以获得最佳性能。