简体   繁体   English

使用PHP与私有Composer存储库的AWS Elastic Beanstalk

[英]AWS Elastic Beanstalk using PHP with Private Composer Repositories

How do I utilize private composer repositories when deploying with Amazon AWS Elastic Beanstalk in a PHP environment? 在PHP环境中使用Amazon AWS Elastic Beanstalk进行部署时,如何使用私有编写器存储库? Specifically using GitHub (Q & A style, answer following) 特别使用GitHub(Q&A风格,回答如下)

We needed to use a private library for one of our PHP projects we were deploying via AWS's Elastic Beanstalk (EB). 我们需要为我们通过AWS的Elastic Beanstalk(EB)部署的PHP项目使用私有库。 This private library is hosted on GitHub, although similar git hosting (your own server, BitBucket, etc.) probably has similar authentication and could use this solution for deployment. 这个私有库托管在GitHub上,虽然类似的git托管(您自己的服务器,BitBucket等)可能具有类似的身份验证,并且可以使用此解决方案进行部署。

We used SSH credentials to get at the private git repository. 我们使用SSH凭据来获取私有git存储库。 Since we are using GitHub, we used GitHub's Deploy Keys ( https://help.github.com/articles/managing-deploy-keys#deploy-keys ) These keys allow read only access to a specific repository, which is perfect for our needs. 由于我们使用的是GitHub,我们使用了GitHub的Deploy Keyshttps://help.github.com/articles/managing-deploy-keys#deploy-keys )这些键允许对特定存储库进行只读访问,这对我们来说是完美的。需要。 Evaluate the best solution for your needs, GitHub has great pros and cons listed for each method. 根据您的需求评估最佳解决方案,GitHub针对每种方法列出了很多优缺点。

Our chosen solution embeds the deploy key in with the repository. 我们选择的解决方案将部署密钥嵌入到存储库中。 This is a bit of a security hole. 这有点安全漏洞。 We are dealing with all private repos, with (ideally) secure servers, but this still is a bit of a security risk. 我们正在处理所有私有回购,具有(理想情况下)安全服务器,但这仍然存在一定的安全风险。

All of this ended up being a bit of a hassle with the way the PHP stack is deployed with Elastic Beanstalk, composer.json was getting auto-run too early and the keys weren't in place beforehand. 所有这一切最终都与使用Elastic Beanstalk部署PHP堆栈的方式有点麻烦,composer.json过早地自动运行并且密钥未事先就位。 We found a workaround. 我们找到了解决方法。

This assumes you already have your deployment setup, but are just stuck at deploying keys. 这假设您已经进行了部署设置,但只是部署了密钥。 We used the eb cli tools provided by AWS (eb init, eb branch, eb start, etc.) to get things up and going, as well as the git hooks, git aws.push to deploy. 我们使用AWS提供的eb cli工具(eb init,eb branch,eb start等)来完成工作,以及git hooks,git aws.push进行部署。

Once we have our Deploy Keys, we can add our library to our composer.json file using the SSH address: 一旦我们有了Deploy Keys,我们就可以使用SSH地址将我们的库添加到composer.json文件中:

{
...
"require": {
        "repository/project": ">=1.0.0"
},
...
"repositories": [
    {
        "type": "git",
        "url":  "git@github.com:repository/project.git"
    }
]
}

Configure your .gitignore so the composer.lock file is committed and in your repository as well as the vendor folder without it's contents: 配置你的.gitignore,以便在你的存储库和没有它内容的vendor文件夹中提交composer.lock文件:

[remove composer.lock from file if it exists]
vendor/*

We prefer keeping the composer.lock file in the repository anyway as it locks in the version used in testing. 我们更喜欢将composer.lock文件保存在存储库中,因为它锁定了测试中使用的版本。 When we move to a production environment we ensure the application is running with the same libraries we tested against. 当我们迁移到生产环境时,我们确保应用程序运行时使用我们测试的相同库。 The vendor folder is required to trick EB into not auto-running the composer.phar install process. 必须使用vendor文件夹来欺骗EB,而不是自动运行composer.phar安装过程。 We need it to wait until we have the ssh keys in place. 我们需要它等到我们有ssh密钥到位。

Setting up the keys: I couldn't find a good way to affiliate the key and accept github.com as a known_host via scripting. 设置密钥:我找不到联系密钥的好方法,并通过脚本接受github.com作为known_host。 I ended up SSHing to the EB managed server with the software half deployed, added the id_rsa and id_rsa.pub key files to the ~root/.ssh/ (with 400 perms remember!) then trying ssh -T git@github.com (as github recommends) This will prompt to accept the host and add an entry to the ~root/.ssh/known_hosts file. 我最终通过半部署的软件SSH连接到EB托管服务器,将id_rsa和id_rsa.pub密钥文件添加到~root / .ssh /(记住400 perms!)然后尝试ssh -T git@github.com (如github推荐)这将提示接受主机并在~root / .ssh / known_hosts文件中添加一个条目。 Copy the contents of this file to where you are working on the project. 将此文件的内容复制到您正在处理项目的位置。

We are creating all of the setup scripts in the .ebextensions/ folder to configure the Linux server for deployment. 我们正在.ebextensions /文件夹中创建所有设置脚本,以配置Linux服务器以进行部署。 This folder is removed (from what I can tell) from the server after pre deployment stage. 在部署前阶段之后,从服务器中删除此文件夹(据我所知)。 We are using the PHP 5.5 64bit Amazon AMI solution. 我们正在使用PHP 5.5 64位Amazon AMI解决方案。 Move the id_rsa and id_rsa.pub keys into the the new .ebextensions folder. 将id_rsa和id_rsa.pub键移动到新的.ebextensions文件夹中。 Also add a file called known_hosts to the folder with the known_hosts contents we provided earlier. 还要将名为known_hosts的文件添加到具有我们之前提供的known_hosts内容的文件夹中。 Now that we have the 3 files we need, we need to create a final deployment instruction file : 01-github-deploy-keys.config (name the file however you like) 现在我们需要3个文件,我们需要创建一个最终部署指令文件 :01-github-deploy-keys.config(根据需要命名文件)

container_commands:
    11-move-priv-key:
        command: "mv ~root/.ssh/id_rsa ~root/.ssh/id_rsa.bak; cp .ebextensions/id_rsa ~root/.ssh/id_rsa; chmod 400 ~root/.ssh/id_rsa;"
    12-move-pub-key:
        command: "mv ~root/.ssh/id_rsa.pub ~root/.ssh/id_rsa.pub.bak; cp .ebextensions/id_rsa.pub ~root/.ssh/id_rsa.pub; chmod 400 ~root/.ssh/id_rsa.pub;"
    12-known-hosts:
        command: "mv ~root/.ssh/known_hosts ~root/.ssh/known_hosts.bak; cp .ebextensions/known_hosts ~root/.ssh/known_hosts; chmod 644 ~root/.ssh/known_hosts;"
    20-install-composer:
        command: "./composer.phar install;"

Remember YAML files uses 4 spaces, not tabs! 记住YAML文件使用4个空格,而不是标签! See the AWS documentation for how these container_commands work: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-commands They will run after the files are pulled from the repository. 有关这些container_commands如何工作的信息,请参阅AWS文档: http//docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-commands它们将在文件运行后运行从存储库中取出。 These commands in "container_commands" section have a working directory of your project, so local paths are preferred. “container_commands”部分中的这些命令具有项目的工作目录,因此首选本地路径。

Add all of these files need to be added and committed to the repository. 添加所有这些文件需要添加并提交到存储库。 Run your git aws.push to deploy. 运行你的git aws.push进行部署。

In order to test the setup properly you will need to remove the server from the EB solution stack and re-add it. 为了正确测试设置,您需要从EB解决方案堆栈中删除服务器并重新添加它。 I just go into the EC2 control panel and find the managed server for this project and terminate it. 我只是进入EC2控制面板,找到该项目的托管服务器并终止它。 EB will automatically create a new one for you and attach it once it is ready. EB会自动为您创建一个新的,并在准备好后附加它。 Double check your logs, specifically the /var/log/cfn-init.log section. 仔细检查您的日志,特别是/var/log/cfn-init.log部分。 It is probably best to turn off SSH access to the servers via security group at this point. 此时最好通过安全组关闭对服务器的SSH访问。 I believe EB restricts logins to root over SSH but just to be sure you may want to disable SSH access all together via firewall/security groups. 我认为EB通过SSH限制登录到root,但只是为了确保您可能希望通过防火墙/安全组一起禁用SSH访问。 You shouldn't need to ssh into individual boxes for configuration as they should be seen as volatile. 您不应该将ssh分成单独的框进行配置,因为它们应该被视为易失性。

This was written as a Q & A on 2014-02-20, please post any comments or fixes. 这是2014-02-20的问答,请发表任何评论或修正。

Thanks, - Seth 谢谢, - 赛斯

TLDR: Use ~/.composer/auth.json, github-oauth on composer.json, or create a custom script like the one below: TLDR:在composer.json上使用〜/ .composer / auth.json,github-oauth,或创建如下所示的自定义脚本:


This is my 02-github-deploy-keys.config file. 这是我的02-github-deploy-keys.config文件。 It's working right now. 它现在正在运作。 The only workaround was to disable StrictHostKeyChecking. 唯一的解决方法是禁用StrictHostKeyChecking。 But you can turn StrictHostKeyChecking on after this script runs, if you like. 但是,如果您愿意,可以在此脚本运行后打开StrictHostKeyChecking。

I added /vendor (without any file) to Git to stop AWS from auto-running Composer before the keys were OK. 我将/ vendor(没有任何文件)添加到Git,以便在密钥正常之前阻止AWS自动运行Composer。 To do so i created a .gitignore file inside /vendor, with this: 为此,我在/ vendor中创建了一个.gitignore文件,其中包含:

*
!.gitignore

I'm storing the keys (id_rsa) on a S3 bucket, where i allowed "Authorized" people to read the file, but you can put the file on your github repository. 我将密钥(id_rsa)存储在S3存储桶中,我允许“授权”人员读取该文件,但您可以将该文件放在您的github存储库中。 These keys were generated on a machine user ( https://developer.github.com/guides/managing-deploy-keys/#machine-users ). 这些密钥是在计算机用户上生成的( https://developer.github.com/guides/managing-deploy-keys/#machine-users )。

files:
    "/home/ec2-user/sshgit/composer.sh":
        mode: "00755"
        owner: ec2-user
        group: ec2-user
        encoding: plain
        content: |
            if [ ! -f /home/ec2-user/id_rsa ] ; then
              aws s3 cp s3://eb-files/id_rsa /home/ec2-user/id_rsa
              chmod 0400 /home/ec2-user/id_rsa
            fi

            eval `ssh-agent -s`
            ssh-add /home/ec2-user/id_rsa

            echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config

            export COMPOSER_HOME=/root
            COMPOSER_HOME=/root
            /opt/elasticbeanstalk/support/composer.phar install --no-interaction

container_commands:
    01-run-composer:
        command: "/home/ec2-user/sshgit/composer.sh"

Just wanted to point out that there's an easier (maybe riskier) way to do this by adding this to composer.json: 只是想指出通过将其添加到composer.json有一种更简单(可能更冒险)的方法:

"config": {
   "github-oauth": {
        "github.com": "YOUR-OAUTH-KEY"
    }
}

And there's a 3rd way which i did't test, but you can create a ~/.composer/auth.json, and composer will probably understand your tokens there. 还有第三种方法,我没有测试,但你可以创建一个〜/ .composer / auth.json,作曲家可能会理解你的令牌。

I struggled with this. 我为此苦苦挣扎。 I've got repos in AWS CodeCommit and was looking for the path of least resistance to solve it. 我在AWS CodeCommit中得到了回购,并且正在寻找解决它的阻力最小的路径。 I tried ~/.composer/auth.json but it appears that composer is run before I could get the file in place etc. etc. 我试过了〜/ .composer / auth.json,但看起来在我可以将文件放到原位等之前运行了composer等。

I went for an approach which includes the vendor directory in my repo (get rid of .git folders within so that it doesn't treate them as submodules) and the whole thing then gets published to Elastic Beanstalk via zip file including that folder. 我去了一个方法,其中包括我的repo中的供应商目录(摆脱.git文件夹,以便它不会将它们作为子模块处理),然后整个事物通过包含该文件夹的zip文件发布到Elastic Beanstalk。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM