在 Alibaba Cloud Linux 3.2104 LTS 64位操作系统上部署应用或服务,是一个常见且稳定的选择。Alibaba Cloud Linux 3 是基于 CentOS/RHEL 生态构建的,兼容性好,性能优化针对阿里云基础设施进行了深度定制。
以下是通用部署指南,涵盖环境准备、常用软件安装、安全配置和最佳实践:
✅ 一、基础准备
1. 登录服务器
ssh root@<your-instance-ip>
2. 更新系统
yum update -y
reboot # 建议重启以应用内核更新
3. 安装常用工具
yum install -y vim wget curl git net-tools unzip tar htop
✅ 二、常见服务部署示例
📌 示例1:部署 Nginx + PHP(LNMP)
1. 安装 Nginx
yum install -y nginx
systemctl start nginx
systemctl enable nginx
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
2. 安装 PHP(推荐 PHP 8.x)
# Alibaba Cloud Linux 3 默认仓库可能较旧,建议使用 Remi 源或源码编译
yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
yum module reset php -y
yum module enable php:remi-8.2 -y
yum install -y php-fpm php-mysqlnd php-gd php-json php-mbstring php-xml php-zip
systemctl start php-fpm
systemctl enable php-fpm
3. 配置 Nginx 支持 PHP
编辑 /etc/nginx/conf.d/default.conf,添加或修改 location 块:
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
重启 Nginx:
systemctl restart nginx
📌 示例2:部署 MySQL / MariaDB
方式1:使用默认仓库(MariaDB)
yum install -y mariadb-server
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation # 设置 root 密码等
方式2:安装 MySQL 8.0(推荐)
# 下载 MySQL YUM 源
wget https://dev.mysql.com/get/mysql80-community-release-el8-5.noarch.rpm
rpm -ivh mysql80-community-release-el8-5.noarch.rpm
yum install -y mysql-community-server
systemctl start mysqld
systemctl enable mysqld
# 获取临时密码
grep 'temporary password' /var/log/mysqld.log
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourStrongPassword!';
📌 示例3:部署 Docker
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
docker --version
💡 Alibaba Cloud Linux 3 原生支持 Containerd,也可考虑直接使用
containerd替代 Docker。
📌 示例4:部署 Java 应用(Spring Boot)
1. 安装 JDK
yum install -y java-11-openjdk-devel
java -version
2. 上传并运行 Jar 包
scp your-app.jar root@<ip>:/opt/app/
cd /opt/app
nohup java -jar your-app.jar > app.log 2>&1 &
3. 配置防火墙开放端口
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
✅ 三、安全加固建议
1. 禁用 root 远程登录
编辑 /etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
重启 SSH:
systemctl restart sshd
2. 配置防火墙(firewalld)
仅开放必要端口:
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
3. 安装 fail2ban 防止暴力破解
yum install -y fail2ban
systemctl start fail2ban
systemctl enable fail2ban
4. 定期更新系统
yum update -y
✅ 四、监控与日志
1. 查看系统资源
htop
df -h
free -m
2. 查看服务日志
journalctl -u nginx -f
journalctl -u mysqld -f
tail -f /var/log/messages
3. 使用阿里云云监控 Agent(可选)
# 安装阿里云监控插件
wget http://update.aegis.aliyun.com/download/quartz/aliyun-monitor-agent-latest-linux-amd64.tar.gz
tar -zxvf aliyun-monitor-agent-latest-linux-amd64.tar.gz
cd aliyun-monitor-agent
./install.sh
✅ 五、备份与恢复
1. 数据库备份
mysqldump -u root -p your_database > backup_$(date +%F).sql
2. 系统快照
在阿里云控制台对 ECS 实例创建系统盘快照,便于快速回滚。
✅ 六、常见问题排查
| 问题 | 解决方案 |
|---|---|
| 服务无法启动 | systemctl status <service> 查看详细错误 |
| 端口不通 | 检查 firewall-cmd 和安全组规则 |
| PHP-FPM 连接失败 | 确认 listen = 127.0.0.1:9000 与 Nginx 配置一致 |
| SELinux 阻止访问 | setenforce 0 临时测试,长期建议配置正确策略 |
✅ 七、推荐架构(生产环境)
用户 → SLB(负载均衡) → Nginx(反向X_X) → Spring Boot/Django/Node.js → Redis/MongoDB/MySQL
↓
RDS(托管数据库)
↓
OSS(对象存储)
如需部署特定应用(如 WordPress、Kubernetes、Elasticsearch、Redis Cluster 等),请提供具体需求,我可以给出详细步骤。
PHPWP博客