【发布时间】:2022-01-14 16:33:45
【问题描述】:
我无法在我的 EC2 实例中运行某些服务。我正在使用 Amazon Linux AMI,并希望在我的实例首次启动时将图形数据库 neo4j 作为服务启动。
但是,我一直无法启动任何东西,更不用说neo4j。我什至无法让亚马逊提供的基本tutorial user data script 工作。我复制并粘贴了以下代码:
#!/bin/bash
yum update -y
yum install -y httpd24 php56 mysql55-server php56-mysqlnd
service httpd start
chkconfig httpd on
进入我的 EC2 实例的用户数据部分,然后启动我的 EC2 实例。从理论上讲,由于httpd 已启动,我应该能够在浏览器中导航到我的实例的 IP 地址并查看默认的 Apache 网页(我已允许端口 80 的入站 HTTP 用于安全组设置)。但是,我收到 Connection refused 错误。
当我在通过 SSH 连接到我的实例后发出以下命令时,这一点得到了证实:
sudo su
service httpd status
响应是httpd is stopped,显然用户数据脚本应该启动它。亚马逊建议调试我检查/var/log/cloud-init-output.log 是否有错误消息,但我在检查日志时看不到任何错误消息:
Cloud-init v. 0.7.6 running 'init-local' at Sun, 12 Nov 2017 16:41:03 +0000. Up 5.73 seconds.
Cloud-init v. 0.7.6 running 'init' at Sun, 12 Nov 2017 16:41:03 +0000. Up 5.91 seconds.
ci-info: +++++++++++++++++++++++Net device info+++++++++++++++++++++++
ci-info: Device Up Address Mask Hw-Address
ci-info: lo True 127.0.0.1 255.0.0.0 .
ci-info: eth0 True 172.31.7.27 255.255.240.0 0a:8c:a2:67:1f:46
ci-info: ++++++++++++++++++++++++++++++Route info+++++++++++++++++++++++++++++++
ci-info: Route Destination Gateway Genmask Interface Flags
ci-info: 0 0.0.0.0 172.31.0.1 0.0.0.0 eth0 UG
ci-info: 1 169.254.169.254 0.0.0.0 255.255.255.255 eth0 UH
ci-info: 2 172.31.0.0 0.0.0.0 255.255.240.0 eth0 U
Generating public/private rsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
...
The key's randomart image is:
+---[RSA 2048]----+
[RANDOM ART]
+----[SHA256]-----+
Generating public/private dsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
...
The key's randomart image is:
+---[DSA 1024]----+
+----[SHA256]-----+
Generating public/private ecdsa key pair.
Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key.
Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub.
The key fingerprint is:
...
The key's randomart image is:
... [RANDOMART IMAGE]
Cloud-init v. 0.7.6 running 'modules:config' at Sun, 12 Nov 2017 16:41:04 +0000. Up 6.70 seconds.
Loaded plugins: priorities, update-motd, upgrade-helper
No packages needed for security; 0 packages available
No packages marked for update
Cloud-init v. 0.7.6 running 'modules:final' at Sun, 12 Nov 2017 16:41:08 +0000. Up 10.93 seconds.
Cloud-init v. 0.7.6 finished at Sun, 12 Nov 2017 16:41:08 +0000. Datasource DataSourceEc2. Up 11.04 seconds
Cloud-init v. 0.7.6 running 'init-local' at Sun, 12 Nov 2017 17:10:45 +0000. Up 5.85 seconds.
Cloud-init v. 0.7.6 running 'init' at Sun, 12 Nov 2017 17:10:46 +0000. Up 6.04 seconds.
ci-info: +++++++++++++++++++++++Net device info+++++++++++++++++++++++
ci-info: Device Up Address Mask Hw-Address
ci-info: lo True 127.0.0.1 255.0.0.0 .
ci-info: eth0 True 172.31.7.27 255.255.240.0 0a:8c:a2:67:1f:46
ci-info: ++++++++++++++++++++++++++++++Route info+++++++++++++++++++++++++++++++
ci-info: Route Destination Gateway Genmask Interface Flags
ci-info: 0 0.0.0.0 172.31.0.1 0.0.0.0 eth0 UG
ci-info: 1 169.254.169.254 0.0.0.0 255.255.255.255 eth0 UH
ci-info: 2 172.31.0.0 0.0.0.0 255.255.240.0 eth0 U
Cloud-init v. 0.7.6 running 'modules:config' at Sun, 12 Nov 2017 17:10:46 +0000. Up 6.61 seconds.
Cloud-init v. 0.7.6 running 'modules:final' at Sun, 12 Nov 2017 17:10:47 +0000. Up 7.03 seconds.
Cloud-init v. 0.7.6 finished at Sun, 12 Nov 2017 17:10:47 +0000. Datasource DataSourceEc2. Up 7.11 seconds
Cloud-init v. 0.7.6 running 'init-local' at Sun, 12 Nov 2017 17:18:25 +0000. Up 5.60 seconds.
Cloud-init v. 0.7.6 running 'init' at Sun, 12 Nov 2017 17:18:25 +0000. Up 5.79 seconds.
ci-info: +++++++++++++++++++++++Net device info+++++++++++++++++++++++
ci-info: Device Up Address Mask Hw-Address
ci-info: lo True 127.0.0.1 255.0.0.0 .
ci-info: eth0 True 172.31.7.27 255.255.240.0 0a:8c:a2:67:1f:46
ci-info: ++++++++++++++++++++++++++++++Route info+++++++++++++++++++++++++++++++
ci-info: Route Destination Gateway Genmask Interface Flags
ci-info: 0 0.0.0.0 172.31.0.1 0.0.0.0 eth0 UG
ci-info: 1 169.254.169.254 0.0.0.0 255.255.255.255 eth0 UH
ci-info: 2 172.31.0.0 0.0.0.0 255.255.240.0 eth0 U
Cloud-init v. 0.7.6 running 'modules:config' at Sun, 12 Nov 2017 17:18:26 +0000. Up 6.41 seconds.
Cloud-init v. 0.7.6 running 'modules:final' at Sun, 12 Nov 2017 17:18:26 +0000. Up 6.84 seconds.
Cloud-init v. 0.7.6 finished at Sun, 12 Nov 2017 17:18:26 +0000. Datasource DataSourceEc2. Up 6.93 seconds
我在 StackOverflow 上查找了类似的帖子,发现:user data script doesn't launch with ec2 instance。公认的答案是用户数据脚本仅在第一个启动周期执行 - 这是否意味着我需要终止我的实例并启动一个全新的实例才能执行我的脚本?在这种情况下,我觉得这违背了启动脚本的全部目的!
编辑:我添加了一些 echo 测试语句,以查看它们是否被打印在日志中 - 不!
但是,我确实在我的 EC2 实例中尝试了curl http://169.254.169.254/latest/meta-data,并得到了这个输出:
#!/bin/bash
echo "Testing 1"
yum update -y
echo "Testing 2"
yum install -y httpd24 php56 mysql55-server php56-mysqlnd
echo "Testing 3"
service httpd start
chkconfig httpd on
所以它肯定是在加载我的用户数据......只是没有执行它。
【问题讨论】:
-
这是您的完整日志吗?尝试添加更多
echo进行调试。 -
您是否也为 yum install 启用了端口?您可以在用户数据部分添加 echo "Test" 以查看是否正在调用用户数据部分。 #!/bin/bash yum update -y echo "test" yum install -y httpd24 php56 mysql55-server php56-mysqlnd echo "starting httpd service" service httpd start chkconfig httpd on
-
如果您的日志中没有
echo输出,这意味着您的脚本在您的实例启动期间没有运行。请记住,user-data script在第一次启动时只启动一次。您可以运行具有较小实例类型的其他实例来测试您的脚本。 -
不!第一次启动新实例。下次您停止然后重新启动实例时,它不会再次运行
user-data。这就是为什么你必须启动新实例来测试user-data脚本 -
@Yu Chen - 正确。您是否希望每次重新启动时都运行安装过程?如果您想在每次重新启动时启动一个进程,请查看 /etc/rc.local。 Userdata 仅在实例启动时运行。其目的是运行一次,以便在实例投入使用之前进行一次性服务器配置。
标签: linux bash apache amazon-web-services amazon-ec2