You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EC2实例User Data脚本启动时未执行 求助排查配置问题

EC2 User Data脚本自动执行失败的排查与修复

从你提供的Cloud-init日志里,能直接定位到几个核心问题:

  • yum install nginx执行时触发了交互式确认(日志里的Is this ok [y/N]: Operation aborted.),而User Data是非交互式自动执行的,脚本会因为等待用户输入直接终止,导致nginx完全没安装成功
  • 后续启动nginx服务、写入index.html的操作,都因为nginx未安装、对应目录不存在而失败

修复后的User Data脚本

#!/bin/bash
yum update -y
# 加-y参数,非交互式自动确认所有安装操作
yum install nginx -y
# Amazon Linux 2023推荐用systemctl管理服务,替代旧的service命令
systemctl start nginx
# 配置nginx开机自启,避免实例重启后服务中断
systemctl enable nginx
echo '<h4> Welcome to AWS</h4>' >> /usr/share/nginx/html/index.html

关键修改说明

  1. 给yum install加-y参数:强制非交互式自动确认所有安装步骤,避免脚本卡在确认环节
  2. 用systemctl替代service命令:Amazon Linux 2023基于RHEL 9,systemctl是官方推荐的服务管理工具,兼容性和功能更完善
  3. 新增systemctl enable nginx:确保实例重启后nginx自动启动,无需手动操作

内容的提问来源于stack exchange,提问作者user18665270

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:19:55