且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在部署应用程序Elastic Beanstalk上修改NGINX配置

更新时间:2022-12-06 13:55:09

花了整整一天的时间尝试修改部署中的 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.