Question:
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.
I have read and tried:
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html
How to configure .ebextensions for nginx location directive?
Amazon Elastic Beanstalk ebextension
I have
/.ebextensions/01_nginx.config
1 2 3 4 5 6 7 8 |
files: "/etc/nginx/nginx.conf": mode: "0000644" owner: root group: root content: | My conf file |
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:
1 2 3 4 5 6 |
container_commands: deleteConf: command: "sudo rm /etc/nginx/nginx.conf" changeConf: command: "sudo cp /etc/nginx/my_nginx.conf /etc/nginx/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?
Edit: 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
1 2 3 4 5 6 |
container_commands: deleteConf: command: "sudo rm /etc/nginx/nginx.conf" changeConf: command: "sudo cp /etc/nginx/my_nginx.conf /etc/nginx/nginx.conf" |
No luck with that
Answer:
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.
Same thing happened when I created the custom file in /tmp and wrote a container_command that would replace /etc/nginx/nginx.conf.
You actually need to create a script that will be executed after deployment, after elastic beanstalk has written all your files.
If you are using an Amazon Linux 1 environment:
Source: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html
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
- Create a directory that will store your script.
1 2 3 4 5 |
commands: create_post_dir: command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post" ignoreErrors: true |
- Create your modified nginx.conf file and place it in /tmp.
1 2 3 4 5 6 7 8 |
files: "/tmp/custom_nginx.conf": mode: "000644" owner: root group: root content: | # your .conf file |
- 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.
1 2 3 4 5 6 7 8 |
"/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 |
If you are using an Amazon Linux 2 environment:
Source: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html
Scripts are now executed from subfolders in .platform that you have to place at the root of your project, like so:
1 2 3 4 5 6 7 8 9 10 11 |
~/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 |
Create a .config file at the root of .ebextensions.
01_write_custom_ng_conf.config
Create your modified nginx.conf file and place it in /tmp.
1 2 3 4 5 6 7 8 |
files: "/tmp/custom_nginx.conf": mode: "000644" owner: root group: root content: | # your .conf file |
Create a small script in .platform/hooks/postdeploy and change permissions to 755.
01_update_ng_conf.sh
1 2 3 4 5 6 7 8 |
#!/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 |
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.
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.