如何在部署应用程序Elastic Beanstalk上修改NGINX配置 [英] How to modify NGINX configuration on Deploying app Elastic beanstalk

查看:131
本文介绍了如何在部署应用程序Elastic Beanstalk上修改NGINX配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在nginx.conf中添加一些位置,以便环境URL指向app.php.我已经使用vi修改了文件.重新启动NGINX就可以了.但是我需要在使用 eb deploy 时自动加载此配置.

I need to add some locations to nginx.conf so the environment URL points to app.php. I have modified the file using vi. Restarting NGINX it works. But I need this configuration to be load automatically when I use eb deploy.

我已阅读并尝试过: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

在单个实例上配置Elasticbeanstalk HTTPSPython:模板中不允许使用空值

如何为nginx位置指令配置.ebextensions?

Amazon Elastic Beanstalk ebextension

我有/.ebextensions/01_nginx.config

I have /.ebextensions/01_nginx.config

files:
"/etc/nginx/nginx.conf":
    mode: "0000644"
    owner: root
    group: root
    content: |
        My conf file

但是该配置不起作用.我尝试通过"/etc/nginx/my_nginx.conf"更改"/etc/nginx/nginx.conf":文件出现了!因此,我尝试用以下自定义文件替换默认文件:

But that config is not working. I have tried changing "/etc/nginx/nginx.conf": by "/etc/nginx/my_nginx.conf": and the file appeared! So I tried to replace default file by my custom file with:

container_commands:
    deleteConf:
        command: "sudo rm /etc/nginx/nginx.conf"
    changeConf:
        command: "sudo cp /etc/nginx/my_nginx.conf  /etc/nginx/nginx.conf"

放置在01_nginx.config中先前配置的下方.但是命令不起作用.nginx.conf并没有被我删除或替换.我在做什么错了?

Placed below previous config inside 01_nginx.config. But commands are not working. nginx.conf is not being deleted or replaced by mine. What I'm doing wrong?

我读过.ebextensions中的文件是按字母顺序求值的.我想知道在文件存在之前是否正在执行复制命令.因此,我创建了一个新文件/.ebextensions/02_copy.config并将其移至

I have read that files in .ebextensions are evaluated alphabetically. I was wondering if maybe the copy command was being executed before the file exists. So I created a new file /.ebextensions/02_copy.config and moved there

container_commands:
    deleteConf:
        command: "sudo rm /etc/nginx/nginx.conf"
    changeConf:
        command: "sudo cp /etc/nginx/my_nginx.conf  /etc/nginx/nginx.conf"

没有运气

推荐答案

花了整整一天的时间尝试修改部署中的 nginx.conf 之后,创建了一个自定义的 nginx.conf 通过 .ebextensions 中的 .config 文件直接在/etc/nginx/中对我不起作用,因为它会被系统地覆盖默认的.

After spending a whole day trying to modify nginx.conf on deployment, creating a custom nginx.conf directly in /etc/nginx/ via a .config file in .ebextensions didn't work for me as it would be systematically overwritten by the default one.

当我在/tmp 中创建自定义文件并编写 container_command 来替换/etc/nginx/nginx.conf 时,发生了同样的事情.>.

Same thing happened when I created the custom file in /tmp and wrote a container_command that would replace /etc/nginx/nginx.conf.

您实际上需要创建一个脚本,该脚本将在弹性beantalk写入所有文件之后部署后执行.

You actually need to create a script that will be executed after deployment, after elastic beanstalk has written all your files.

来源: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html

要在部署后执行任何脚本,请将以下代码放入 .ebextensions

To execute any script after deployment put the following code into your .config file at the root of .ebextensions

01_write_custom_ng_conf.config

  1. 创建一个目录来存储您的脚本.

commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true

  1. 创建修改后的 nginx.conf 文件并将其放置在/tmp 中.
  1. Create your modified nginx.conf file and place it in /tmp.

files:
  "/tmp/custom_nginx.conf":
     mode: "000644"
     owner: root
     group: root
     content: |
       # your .conf file

  1. 在第1步创建的/opt/elasticbeanstalk/hooks/appdeploy/post 中创建一个脚本,用您的自定义脚本替换原始的 nginx.conf .部署后将自动执行.
  1. Create a script that will replace the original nginx.conf by your custom one in /opt/elasticbeanstalk/hooks/appdeploy/post created at step 1. This should be automatically executed after deployment.

   "/opt/elasticbeanstalk/hooks/appdeploy/post/update_ng_conf.sh":
     mode: "000644"
     owner: root
     group: root
     content:
       #!/usr/bin/bash
       sudo mv /tmp/custom_nginx.conf /etc/nginx/nginx.conf 

如果您使用的是Amazon Linux 2环境:

来源: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

脚本现在从 .platform 中的子文件夹执行,您必须将其放置在项目的根目录中,如下所示:

Scripts are now executed from subfolders in .platform that you have to place at the root of your project, like so:

~/your-app/
|-- someFile.py
|-- Procfile
|-- readme.md
|-- .ebextensions/
|   |-- 01_write_custom_ng_conf.config        
`-- .platform/
    |-- hooks
        `-- postdeploy
            `-- 01_update_ng_conf.sh        # Executed post deployment

.ebextensions 的根目录下创建 .config 文件.

Create a .config file at the root of .ebextensions.

01_write_custom_ng_conf.config

创建修改后的 nginx.conf 文件并将其放置在/tmp 中.

Create your modified nginx.conf file and place it in /tmp.

files:
  "/tmp/custom_nginx.conf":
     mode: "000644"
     owner: root
     group: root
     content: |
       # your .conf file

.platform/hooks/postdeploy 中创建一个小脚本,并将权限更改为 755 .

Create a small script in .platform/hooks/postdeploy and change permissions to 755.

01_update_ng_conf.sh

#!/usr/bin/bash

# Replace the original nginx.conf by our custom one
sudo mv /tmp/custom_nginx.conf /etc/nginx/nginx.conf

# Restart nginx to apply modifications
sudo service nginx restart

Amazon Linux 2 Python 3.7环境中,这对我来说效果很好.不确定您的身份,但这应该是相同的.

This worked perfectly fine for me on a Amazon Linux 2 Python 3.7 environment. Not sure about yours but this should be the same.

请确保您的 .config 文件缩进是完美的,因为解析器并不总是能够检测到错误,并且代码也不会被执行.

Make sure that your .config files indentation is perfect as errors are not always detected by the parser and the code won't be executed.

这篇关于如何在部署应用程序Elastic Beanstalk上修改NGINX配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆