第一步:购买与选型
# 生成密钥对(本地操作)
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 上传公钥至云服务器控制台
# 登录服务器(替换为你的IP)
ssh -i ~/.ssh/private_key root@your_server_ip
- 禁用密码登录:修改
/etc/ssh/sshd_config
,设置PasswordAuthentication no
后重启服务。
第三步:系统初始化
-
更新软件源
# CentOS yum update -y && yum upgrade -y # Ubuntu apt update -y && apt upgrade -y
-
安装必备工具
# 通用工具包 yum install -y wget curl vim git unzip # CentOS apt install -y wget curl vim git unzip # Ubuntu
-
配置时区与时间同步
timedatectl set-timezone Asia/Shanghai systemctl enable chronyd && systemctl start chronyd # CentOS systemctl enable systemd-timesyncd && systemctl start systemd-timesyncd # Ubuntu
第四步:部署Web环境(以Nginx+PHP为例)
# CentOS
yum install -y nginx && systemctl start nginx
# Ubuntu
apt install -y nginx && systemctl start nginx
安装PHP 8.2
# CentOS yum install -y epel-release yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm yum-config-manager --enable remi-php82 yum install -y php php-fpm php-mysqlnd # Ubuntu apt install -y software-properties-common add-apt-repository ppa:ondrej/php -y apt update && apt install -y php8.2 php8.2-fpm php8.2-mysql
配置Nginx与PHP联动
- 编辑
/etc/nginx/conf.d/default.conf
,添加以下内容:location ~ .php$ { fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; }
- 重启服务:
systemctl restart nginx && systemctl restart php-fpm
第五步:安全加固
-
防火墙配置(firewalld/ufw)
# CentOS(firewalld) firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload # Ubuntu(ufw) ufw allow 80/tcp ufw allow 443/tcp ufw enable
-
Fail2ban防暴力破解
# 安装 yum install -y fail2ban # CentOS apt install -y fail2ban # Ubuntu # 配置SSH防护 cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local sed -i 's/bantime = 10m/bantime = 1h/g' /etc/fail2ban/jail.local systemctl restart fail2ban
-
定期备份与监控
- 快照功能:通过云平台控制台每周自动创建系统盘快照。
- 资源监控:安装云监控插件(如阿里云CloudMonitor),设置CPU、内存超阈值报警。
第六步:域名与HTTPS绑定
-
域名解析
在域名服务商处添加A记录,指向ECS服务器的公网IP。
# 安装Certbot
snap install –classic certbot
certbot –nginx
# 按提示选择域名并自动配置HTTPS
维护与优化建议
-
定期更新系统:
# 设置自动安全更新 yum install -y yum-cron && systemctl enable yum-cron # CentOS apt install -y unattended-upgrades && dpkg-reconfigure unattended-upgrades # Ubuntu
-
日志管理:
- 使用
logrotate
分割Nginx/PHP日志,避免磁盘占满。
- 使用
-
性能调优:
- 数据库独立部署:高负载场景建议使用云数据库(如RDS)。
- CDN加速:静态资源通过CDN分发,降低服务器压力。
引用说明
本文参考以下权威资料:
- 阿里云ECS官方文档:https://help.aliyun.com/product/25365.html
- 酷盾CVM最佳实践:https://cloud.tencent.com/document/product/213/2760
- Let’s Encrypt Certbot指南:https://certbot.eff.org/instructions
- Nginx官方配置手册:https://nginx.org/en/docs/
提示:操作涉及系统权限,建议在测试环境验证后再部署至生产服务器。