使用 AWS CloudFormation 启动带有最新 Apache 和 PHP 的 AutoScaling 服务器?

Using AWS CloudFormation to launch AutoScaling servers with latest Apache and PHP?

我想通过 CloudFormation 模板启动带有 Autoscaled EC2 实例的 LAMP 堆栈。每当我尝试时,堆栈都会成功构建,但是当我访问网站时,它会给我一条消息,提示应用程序不支持旧版本 PHP.

现在,当我在网上搜索这个问题时,我找到了一个建议 的答案。我直接在 EC2 linux 终端上尝试了它并且成功了,我现在可以成功访问该网站。问题是,我希望将这些更新版本直接安装在模板中。为此,我在 AutoScaling::LaunchConfiguration:

中替换了这段代码
       "setup" : {
        "packages" : {
            "yum" : {
              "nfs-utils"       : [],
              "httpd"           : [],
              "php"             : [],
              "mysql"           : []
            }
        },

与:

       "setup" : {
        "packages" : {
            "yum" : {
              "nfs-utils"       : [],
              "httpd24"         : [],
              "php72"           : [],
              "mysql"           : []
            }
        },

这会导致堆栈无法创建 AutoScalingGroup,它具有 LaunchConfiguration 作为参数,错误说明“收到 0 个成功信号”。 这是我 LaunchConfiguration 的属性:

    "Properties": {
    "AssociatePublicIpAddress" : true,
    "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
              { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
    "InstanceType"   : { "Ref" : "InstanceType" },
    "SecurityGroups" : [ {"Ref" : "WebServerSecurityGroup"} ],
    "KeyName"        : { "Ref" : "KeyName" },
    "IamInstanceProfile" : { "Ref" : "CloudWatchPutMetricsInstanceProfile" },
    "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
            "#!/bin/bash -xe\n",
            "yum update -y aws-cfn-bootstrap\n",

            "/opt/aws/bin/cfn-init -v ",
            "         --stack ", { "Ref" : "AWS::StackName" },
            "         --resource LaunchConfig ",
            "         --configsets app_install ",
            "         --region ", { "Ref" : "AWS::Region" }, "\n",

            "/opt/aws/bin/cfn-signal -e $? ",
            "         --stack ", { "Ref" : "AWS::StackName" },
            "         --resource WebServerGroup ",
            "         --region ", { "Ref" : "AWS::Region" }, "\n"
    ]]}}
  }

知道为什么它不起作用吗?有什么建议可以直接通过模板实际安装最新的 httpd 和 php 吗?我是否应该删除 packages 部分并将用于安装 httpd 和 php 的脚本直接放在 UserData?

我设法自己解决了。我使用 Ubuntu 18.04 AMI 而不是 Linux,因为它支持最新版本的 PHP 和 Apache。

还必须稍微更改“UserData”:

        "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
        "#!/bin/bash -xe\n",
        "apt-get update -y\n",
        "apt-get install -y python-pip\n",
        "mkdir -p /opt/aws/bin\n",
        "python /usr/lib/python2.7/dist-packages/easy_install.py --script-dir /opt/aws/bin https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
        "ln -s /usr/local/lib/python2.7/dist-packages/aws_cfn_bootstrap-1.4-py2.7.egg/init/ubuntu/cfn-hup /etc/init.d/cfn-hup \n",
        "systemctl daemon-reload \n",

        "chmod 700 /etc/init.d/cfn-hup \n",
        "chown root:root /etc/init.d/cfn-hup \n",
        "update-rc.d cfn-hup defaults \n",
        "update-rc.d cfn-hup enable \n",

        "/opt/aws/bin/cfn-init -v ",
        "         --stack ", { "Ref" : "AWS::StackName" },
        "         --resource LaunchConfig ",
        "         --configsets app_install ",
        "         --region ", { "Ref" : "AWS::Region" }, "\n",

        "/opt/aws/bin/cfn-signal -e $? ",
        "         --stack ", { "Ref" : "AWS::StackName" },
        "         --resource WebServerGroup ",
        "         --region ", { "Ref" : "AWS::Region" }, "\n"
    ]]}}

并且因为 Ubuntu 使用 apt 而不是 yum,这就是我包含软件包的方式:

          "setup" : {
        "packages" : {
            "apt" : {
              "nfs-common"    : [],
              "apache2"       : [],
              "php"           : [],
              "php-mysql"     : [],
              "mysql-common"  : []
            }
        },