linux - 用于在未执行的 EC2 实例中启动服务的用户数据 bash 脚本

标签 linux bash apache amazon-web-services amazon-ec2

我很难在我的 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 服务 httpd 状态

响应是httpd 已停止,显然用户数据脚本应该启动它。 Amazon 建议调试我检查 /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

所以它确实在加载我的用户数据...只是没有执行它。

最佳答案

来自 Configuring Instances with User Data

When you specify user data, note the following:

  • User data is treated as opaque data: what you give is what you get back. It is up to the instance to be able to interpret it.
  • User data is limited to 16 KB. This limit applies to the data in raw form, not base64-encoded form.
  • User data must be base64-encoded. The Amazon EC2 console can perform the base64 encoding for you or accept base64-encoded input.
  • User data must be decoded when you retrieve it. The data is decoded when you retrieve it using instance metadata and the console.
  • User data is executed only at launch. If you stop an instance, modify the user data, and start the instance, the new user data is not executed automatically

用户数据仅在实例启动时运行。它的目的是运行一次,以允许在实例投入使用之前执行一次性服务器配置任务。

您是否希望每次重新启动时都运行软件安装过程?如果你想在每次重启时启动一个进程,那么查看/etc/rc.local。

关于linux - 用于在未执行的 EC2 实例中启动服务的用户数据 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47256439/

相关文章:

android - adb 安装失败,出现错误 'INSTALL_FAILED_INSUFFICIENT_STORAGE'

linux - tcpdump voip终结者ip地址

Bash 将文件转换为小写并排序

string - 如何使用grep匹配同一行的两个字符串

angular - 在 Tomcat Apache 生产服务器中部署 Angular 7 PWA 应用程序

linux - 编译内核模块时没有规则使target 'lib/sha256.c'错误

python - 为什么将 14 个字节的随机数据附加到原始以太网帧?

regex - 与 awk 一起使用的正则表达式中的量词表现出意外

apache - Hive中的子查询-获取错误 “only subquery expressions that are top level conjuncts are allowed”

macos - Mac OS Sierra上的Apache:非法指令:4 $ HTTPD