操作系统选择
系统初始化设置
# CentOS yum update -y # Ubuntu apt update && apt upgrade -y
useradd your_username passwd your_username usermod -aG wheel your_username # CentOS usermod -aG sudo your_username # Ubuntu
安装必备组件
Nginx/Apache:
# Nginx(CentOS) yum install nginx -y systemctl start nginx systemctl enable nginx # Apache(Ubuntu) apt install apache2 -y systemctl start apache2 systemctl enable apache2
PHP环境:
# PHP 7.4(Ubuntu示例) apt install php7.4 php7.4-fpm php7.4-mysql -y
数据库(MySQL/MariaDB):
# MySQL(CentOS) yum install mysql-server -y systemctl start mysqld systemctl enable mysqld
配置域名与SSL证书
# 安装Certbot(以Nginx为例) yum install certbot python3-certbot-nginx -y # CentOS certbot --nginx -d yourdomain.com
防火墙配置
开放必要端口(如80、443、22):
# firewalld(CentOS) firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload # ufw(Ubuntu) ufw allow 22/tcp ufw allow 80,443/tcp ufw enable
禁用SSH密码登录:使用SSH密钥替代。
# 修改/etc/ssh/sshd_config PasswordAuthentication no PermitRootLogin no systemctl restart sshd
防攻击与监控
yum install fail2ban -y # CentOS apt install fail2ban -y # Ubuntu
服务配置调优
worker_processes auto; worker_connections 10240; keepalive_timeout 60; gzip on; # 启用压缩
opcache.enable=1 opcache.memory_consumption=128 opcache.max_accelerated_files=4000
innodb_buffer_pool_size = 1G # 根据内存调整
资源监控工具
yum install htop -y # CentOS apt install htop -y # Ubuntu
数据定期备份
tar -czvf /backup/web_$(date +%F).tar.gz /var/www/html
mysqldump -u root -p your_database > /backup/db_$(date +%F).sql
自动化备份方案
crontab -e # 每天凌晨3点备份 0 3 * * * /path/to/backup_script.sh
网站速度优化
结构化数据标记